diff --git a/bin/game/Board.class b/bin/game/Board.class index 154bf93..3067fe9 100644 Binary files a/bin/game/Board.class and b/bin/game/Board.class differ diff --git a/bin/game/Command.class b/bin/game/Command.class index 4e0d40b..3c88771 100644 Binary files a/bin/game/Command.class and b/bin/game/Command.class differ diff --git a/bin/game/ComsJoueur.class b/bin/game/ComsJoueur.class index 26ff0e6..e2e8c3e 100644 Binary files a/bin/game/ComsJoueur.class and b/bin/game/ComsJoueur.class differ diff --git a/bin/game/Joueur.class b/bin/game/Joueur.class index 2a7ad1f..b37a273 100644 Binary files a/bin/game/Joueur.class and b/bin/game/Joueur.class differ diff --git a/bin/game/Match.class b/bin/game/Match.class index 20822ba..e79f9be 100644 Binary files a/bin/game/Match.class and b/bin/game/Match.class differ diff --git a/src/game/Board.java b/src/game/Board.java index e0724f8..2d1f94a 100644 --- a/src/game/Board.java +++ b/src/game/Board.java @@ -28,14 +28,4 @@ public class Board { return board[zone]; } - public void printDebugBoard() { - for(Card c : board) - { - if(c == null) System.out.print("X"); - else System.out.print("O"); - } - System.out.println(); - - } - } diff --git a/src/game/Command.java b/src/game/Command.java index a75f463..8685eac 100644 --- a/src/game/Command.java +++ b/src/game/Command.java @@ -5,7 +5,7 @@ package game; */ public final class Command { - private Command(){} // pas de contructor on ne veut pas instancier cette classe + private Command(){} // pas de contructor on ne veut pas instancier cette classe public static final String YOURTURN="yourturn"; // commande de debut de tours public static final String DRAW = "draw"; //demande a pioché public static final String GET_DECK = "getDeck"; // demande le deck @@ -26,10 +26,6 @@ public final class Command { public static final String DESTROY_CARD = "destroyself"; //demande au clien de retirer une carte du terrain public static final String DESTROY_ADV_CARD = "destroyadv"; //demande au client de retirer une carte adverse du terrain public static final String BATTLE = "battlephase"; - - - //inutilisé - public static final String SELECT_CARD = "selectCard"; - public static final String DEFEND = "defend"; - public static final String CAST_SPELL = "castSpell"; + public static final String SET_ENEMY_HP = "setenemhp"; + public static final String SET_HP = "sethp"; } diff --git a/src/game/Joueur.java b/src/game/Joueur.java index 7a27755..b4de8e6 100644 --- a/src/game/Joueur.java +++ b/src/game/Joueur.java @@ -13,18 +13,20 @@ public class Joueur { private Hand hand; private Board board; private int pV; - private int currentMana=0; + // la quantité maximum de mana elle augment de 1 par tours + private int currentMana = 0; + // la mana actuele du joueur private int mana; - public Joueur(ComsJoueur connectionJoueur) { - coms=connectionJoueur; - pV =20; - this.board=new Board(); + coms = connectionJoueur; + pV = 20; + this.board = new Board(); } /** * cette fonction permet d'obtenir le deck du joueur et de préparer la main + * * @throws IOException */ public void requestDataEarlyGameData() throws IOException { @@ -35,16 +37,20 @@ public class Joueur { hand = new Hand(deck); coms.send(DeckSerializer.serializeDeck(deck)); draw(5); - + coms.send(Command.SET_HP); + coms.send(pV); + coms.send(Command.SET_ENEMY_HP); + coms.send(pV); + } /** * cette fonction permet de pioche une quantité donné + * * @param amount la quantié * @throws IOException */ - public void draw(int amount) throws IOException - { + public void draw(int amount) throws IOException { coms.send(Command.DRAW); coms.send(amount); hand.draw(amount); @@ -52,6 +58,7 @@ public class Joueur { /** * affiche sur l'ecran que c'est le tours de l'adversaire + * * @throws IOException */ public void debutTourEnemie() throws IOException { @@ -60,6 +67,7 @@ public class Joueur { /** * affiche un menu de victoire + * * @throws IOException */ public void win() throws IOException { @@ -68,6 +76,7 @@ public class Joueur { /** * affiche un menu de defaite + * * @throws IOException */ public void lose() throws IOException { @@ -75,102 +84,118 @@ public class Joueur { } /** - * cette commande doit être utilisé a chaque début de tour elle permet d'augmenter la mana du joueur + * cette commande doit être utilisé a chaque début de tour elle permet + * d'augmenter la mana du joueur + * * @throws IOException */ public void prepMana() throws IOException { - //on prepare la mana - if(currentMana < 10)currentMana++; - mana=currentMana; + // on prepare la mana + if (currentMana < 10) + currentMana++; + mana = currentMana; coms.send(Command.SETMANA); coms.send(mana); } - /** * premiére phase de jeu avant d'attacker + * * @param adversaire - * @param board + * @param board * @throws IOException */ - public void mainPhase1(Joueur adversaire) throws IOException { - boolean phaseActive=true; - //on retire le time out sur l'adversaire car sa peut causer des désyncronisatoin et des crash + public void mainPhase1(Joueur adversaire) throws IOException { + boolean phaseActive = true; + // on retire le time out sur l'adversaire car sa peut causer des + // désyncronisatoin et des crash adversaire.getComs().getSocket().setSoTimeout(Integer.MAX_VALUE); - //debut de l'écoude des action client - while(phaseActive) - { + // debut de l'écoude des action client + int alive = 0; + while (phaseActive) { coms.getSocket().setSoTimeout(500); String command = coms.recieve(); coms.getSocket().setSoTimeout(Integer.MAX_VALUE); - if(command != null && !command.equals("time out")) - { - System.out.println("GOT COMMAND :"+command); + if (command != null && !command.equals("time out")) { + alive = 0; + System.out.println("GOT COMMAND :" + command); switch (command) { - + case Command.PING: coms.send(Command.PONG); break; - + case Command.PASS_TURN: - phaseActive=false; + phaseActive = false; break; - + case Command.PUT_CARD: System.out.println("entering put card"); - putCard(adversaire,board); + putCard(adversaire, board); break; - + default: - System.err.println("unknown command "+command); + System.err.println("unknown command " + command); + } + + } else { + alive++; + // 3000xalive =300s = 5minutes + // si au bout de 5 minute le client ne repond pas on envoie une erreur + if (alive > 300000) { + throw new IOException("dead client"); + } + try { + Thread.sleep(100); + } catch (InterruptedException e) { } - } + } } - //cette mothode est utilisé l'orse qu'un joueur utilise Command.PLACECARD - //elle verifie si le joueur peut la jouer et refuse ou confirme en fonction + // cette mothode est utilisé l'orse qu'un joueur utilise Command.PLACECARD + // elle verifie si le joueur peut la jouer et refuse ou confirme en fonction private void putCard(Joueur adversaire, Board board) throws IOException { String info = coms.recieve(); - //si ce n'est pas un nombre - if(!isInt(info)) { - //alors on affiche l'inforamtion si la sortie d'erreur + // si ce n'est pas un nombre + if (!isInt(info)) { + // alors on affiche l'inforamtion si la sortie d'erreur System.err.println(info); - }else { - //on recupére la carte + } else { + // on recupére la carte int cardId = Integer.parseInt(info); - + Class cardClass = CardRegistery.get(cardId); System.out.println(cardClass.getName()); - - - int zone = Integer.parseInt(coms.recieve()); + int zone = Integer.parseInt(coms.recieve()); Card handCard = hand.contains(cardClass); - //si la carte n'est pas trouve ou si elle coute trop cher - if(handCard == null || mana < handCard.getCost()){ - //on refuse le placement + // si la carte n'est pas trouve ou si elle coute trop cher + if (handCard == null || mana < handCard.getCost()) { + // on refuse le placement System.out.println("no mana or no card"); coms.send(Command.NOK); - } - else { - if(board.isZoneAvaliable(zone)) - { + } else { + if (board.isZoneAvaliable(zone)) { hand.remove(handCard); board.setCard(zone, handCard); - adversaire.enemyPlay(cardId,zone); + adversaire.enemyPlay(cardId, zone); coms.send(Command.OK); - }else { + + coms.send(Command.SETMANA); + mana -= handCard.getCost(); + coms.send(mana); + } else { System.out.println("the zone is not avaliable"); coms.send(Command.NOK); } } } - + } private boolean isInt(String info) { @@ -179,11 +204,12 @@ public class Joueur { /** * cette fonctoin est appelé affin de placer une carte sur le plateur énemie + * * @param cardId * @param zone * @throws IOException */ - private void enemyPlay(int cardId,int zone) throws IOException { + private void enemyPlay(int cardId, int zone) throws IOException { coms.send(Command.PUT_ENEMY_CARD); coms.send(cardId); coms.send(zone); @@ -199,48 +225,47 @@ public class Joueur { public ComsJoueur getComs() { return coms; } - + public void setPV(int pV) { this.pV = pV; } - - public boolean isDead() - { + + public boolean isDead() { return pV == 0; } - - public boolean isAlive() - { + + public boolean isAlive() { return !isDead(); } /** * envoie le message donné qui aparaitera alors comme une toast + * * @param message * @throws IOException */ - public void sendMessage(String message) throws IOException - { + public void sendMessage(String message) throws IOException { coms.send(Command.POPUP); coms.send(message); } /** * affiche le debut de tours + * * @throws IOException */ public void yourturn() throws IOException { - //on affiche le debut de tour + // on affiche le debut de tour coms.send(Command.YOURTURN); } - public boolean isHandFull() { return hand.isFull(); } /** * retire le nombre de carte donné du deck + * * @param nbcard nombre de carte a supprimé * @throws IOException */ @@ -249,81 +274,124 @@ public class Joueur { coms.send(nbcard); deck.draw(1); } - - public void battlePhase(Joueur adversaire) throws IOException - { + + public void battlePhase(Joueur adversaire) throws IOException { coms.send(Command.BATTLE); - + boolean phaseActive = true; - + adversaire.getComs().getSocket().setSoTimeout(Integer.MAX_VALUE); - //debut de l'écoude des action client - - while(phaseActive) - { + // debut de l'écoude des action client + + while (phaseActive) { coms.getSocket().setSoTimeout(500); String command = coms.recieve(); coms.getSocket().setSoTimeout(Integer.MAX_VALUE); - if(command != null && !command.equals("time out")) - { - System.out.println("GOT COMMAND :"+command); + if (command != null && !command.equals("time out")) { + System.out.println("GOT COMMAND :" + command); switch (command) { - + case Command.PING: coms.send(Command.PONG); break; - + case Command.PASS_TURN: - phaseActive=false; + phaseActive = false; break; - + case Command.ATTACK: - //numero de la zone de la carte attaquante + // numero de la zone de la carte attaquante String carteAttaquante = coms.recieve(); - //numero de la zone de la carte ciblé + // numero de la zone de la carte ciblé String carteCible = coms.recieve(); - if(!isInt(carteAttaquante) || !isInt(carteCible))throw new RuntimeException("non number zone recived in attack phase"); + if (!isInt(carteAttaquante) || !isInt(carteCible)) + throw new RuntimeException("non number zone recived in attack phase"); Card attackingCard = board.getCardInZone(Integer.parseInt(carteAttaquante)); - Card attackedCard = adversaire.getBoard().getCardInZone(Integer.parseInt(carteCible)); - - if(attackedCard ==null || attackingCard == null) - { - board.printDebugBoard(); - throw new RuntimeException("error invalid carteCible:"+carteCible + " carteAttaquante : "+carteAttaquante); - } - - System.out.println("FIGHT " + "carteCible:"+carteCible + " carteAttaquante : "+carteAttaquante); - - boolean isDestroyed = attackedCard.takeDamage(attackingCard.getAttack()); - if(isDestroyed) - { - //on suprime la carte du terrain - board.setCard(Integer.parseInt(carteCible), null); - - //demande a l'adversaire de retirer la carte de son terrain - adversaire.getComs().send(Command.DESTROY_CARD); - adversaire.getComs().send(carteCible); - - //retire la carte au terrain adverse - this.getComs().send(Command.DESTROY_ADV_CARD); - this.getComs().send(carteCible); - - //appelle les effet de quand la carte est détruite - //NOTE : non implémenter dans les cartes individuelles par manque de temps - attackedCard.onCardDestroyed(); + + if (attackingCard == null) { + throw new RuntimeException("invalid attack"); + } else { + if (carteCible.equals("100")) { + boolean isadvDead = handleAttackAgainstPlayer(attackingCard, adversaire); + if(isadvDead) + { + phaseActive = false; + } + } else { + handleAttackAgainstCard(attackingCard, carteCible, adversaire); + } } break; - + default: - System.err.println("unknown command "+command); + System.err.println("unknown command " + command); } - + } } } + private boolean handleAttackAgainstPlayer(Card attackingCard, Joueur adversaire) throws IOException { + boolean result = adversaire.takeDamage(attackingCard.getAttack()); + if(!result) + { + adversaire.updateHp(this); + return false; + } + return true; + } + + private void updateHp(Joueur adversaire) throws IOException { + coms.send(Command.SET_HP); + coms.send(pV); + adversaire.coms.send(Command.SET_ENEMY_HP); + adversaire.coms.send(pV); + + } + + private void handleAttackAgainstCard(Card attackingCard, String carteCible, Joueur adversaire) throws IOException { + Card attackedCard = adversaire.getBoard().getCardInZone(Integer.parseInt(carteCible)); + + if (attackedCard == null) { + throw new RuntimeException( + "error invalid carteCible:" + carteCible + " carteAttaquante : " + attackingCard); + } + + boolean isDestroyed = attackedCard.takeDamage(attackingCard.getAttack()); + if (isDestroyed) { + // on suprime la carte du terrain + board.setCard(Integer.parseInt(carteCible), null); + + // demande a l'adversaire de retirer la carte de son terrain + adversaire.getComs().send(Command.DESTROY_CARD); + adversaire.getComs().send(carteCible); + + // retire la carte au terrain adverse + this.getComs().send(Command.DESTROY_ADV_CARD); + this.getComs().send(carteCible); + + // appelle les effet de quand la carte est détruite + // NOTE : non implémenter dans les cartes individuelles par manque de temps + attackedCard.onCardDestroyed(); + } + + } + private Board getBoard() { return board; } + + /** + * inflige des degats au joueur + * @param amount + * @return true si les pv du joueur on attein zero + */ + private boolean takeDamage(int amount) + { + pV -= amount; + pV = pV > 0 ? pV : 0; + return pV == 0; + + } } diff --git a/src/game/Match.java b/src/game/Match.java index 7c3335d..d89385c 100644 --- a/src/game/Match.java +++ b/src/game/Match.java @@ -30,30 +30,36 @@ public class Match extends Thread { // gameplay @Override public void run() { + //cette variable permet de decider le gagnant en cas d'erreur reseau + boolean isJ1Turn=true; while (joueur1.isAlive() && joueur2.isAlive()) { try { tour(joueur1); - tour(joueur2); + isJ1Turn=false; + if(joueur2.isAlive())tour(joueur2); + isJ1Turn=true; } catch (IOException e) { - endGameAfterIssue(); + endGameAfterIssue(isJ1Turn); return; } } try { endMatch(); } catch (IOException e) { - endGameAfterIssue(); + endGameAfterIssue(isJ1Turn); } } /** * si le jeu a un crash il faut que si un joueur ai perdu la connection l'autre gagne + * @param isJ1Turn */ - private void endGameAfterIssue() { + private void endGameAfterIssue(boolean isJ1Turn) { if(PlayerTesteur.playerTest(joueur1.getComs())) { try { - joueur1.win(); + if(isJ1Turn)joueur1.win(); + else joueur1.lose(); } catch (IOException e) { e.printStackTrace(); } @@ -61,7 +67,8 @@ public class Match extends Thread { if(PlayerTesteur.playerTest(joueur2.getComs())) { try { - joueur2.win(); + if(!isJ1Turn)joueur2.win(); + else joueur2.lose(); } catch (IOException e) { e.printStackTrace(); } @@ -114,7 +121,7 @@ public class Match extends Thread { joueur.mainPhase1(enemy); //Joue autant de carte qu'il veut/peut joueur.battlePhase(enemy);//lance des attaques à son adversaire qui peut répliquer //cette phases ne serira surment pas car nous manquon de temps pour implrementer les fonctionnalité specifique de certaine cartes - joueur.endPhase();//fin du tour du @joueur + if(enemy.isAlive())joueur.endPhase();//fin du tour du @joueur } }