Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.13 KB | None | 0 0
  1. package quoridor;
  2.  
  3. // import project
  4. //import quoridor.Pawn;
  5.  
  6. // import java
  7. import java.util.Scanner;
  8.  
  9.  
  10. public class HumanPlayer extends Player {
  11.     private Scanner scan;
  12.  
  13.     /**
  14.      * HumanPlayer constructor
  15.      * @param name
  16.      * @author
  17.      */
  18.     public HumanPlayer(Game game, String name, Board board, int initX, int initY, boolean terminal) {
  19.         super(game, name, board, initX, initY, terminal);
  20.         this.scan = new Scanner(System.in);
  21.     }
  22.  
  23.     public HumanPlayer(Game game, String name, Board board, boolean terminal) {
  24.         super(game, name, board, terminal);
  25.         this.scan = new Scanner(System.in);
  26.     }
  27.  
  28.     /**
  29.      * Let the player choose between if he wants to play a fence or moves its pawn
  30.      * @author
  31.      */
  32.     public void play() {
  33.         if(this.terminal) {
  34.             if(this.nbFences > 0) {
  35.                 String mode = this.askMode();
  36.  
  37.                 if (mode.equalsIgnoreCase("pawn")) {
  38.                     this.board.displayForPawn();
  39.                     this.playPawn();
  40.                 }
  41.  
  42.                 else if (mode.equalsIgnoreCase("fence")) {
  43.                     this.board.displayForFence();
  44.                     this.playFence();
  45.                     System.out.println("Il vous reste " + super.nbFences + " barrières !");
  46.                 }
  47.             }
  48.             else {
  49.                 System.out.println("Vous n'avez plus de murs disponibles !");
  50.  
  51.                 this.board.displayForPawn();
  52.                 this.playPawn();
  53.             }
  54.         }
  55.     }
  56.  
  57.     public void play(Square sq) {
  58.         if(!terminal) {
  59.             if(this.nbFences > 0) {
  60.                 if (sq.isPawn()) {
  61.                     this.playPawn(sq);
  62.                 }
  63.  
  64.                 else if (sq.isFence()) {
  65.                     this.playFence(sq);
  66.                     // TODO - Réactualiser le nombre de barrières en ayant accès à GameGUI
  67.                 }
  68.             }
  69.             else {
  70.                 this.playPawn(sq);
  71.             }
  72.         }
  73.     }
  74.  
  75.     private String askMode() {
  76.         System.out.println("Quelle pièce voulez-vous jouer ? \n"
  77.             + "Pion (p) ou Mur (m)");
  78.  
  79.         String ret = null;
  80.         this.scan = new Scanner(System.in);
  81.         String s = this.scan.nextLine();
  82.  
  83.  
  84.         while ((!s.equalsIgnoreCase("p")) && (!s.equalsIgnoreCase("pion")) &&
  85.             (!s.equalsIgnoreCase("mur")) && (!s.equalsIgnoreCase("m"))) {
  86.                 System.out.println("La chaîne de caractères est incorrecte !\n"
  87.                     + "Veuillez écrire la pièce que vous voulez jouer : Pion (p) ou Mur (m)");
  88.                 s = this.scan.nextLine();
  89.         }
  90.  
  91.         if ((s.equalsIgnoreCase("p")) || (s.equalsIgnoreCase("pion"))) {
  92.             ret = "pawn";
  93.         }
  94.         else {
  95.             ret = "fence";
  96.         }
  97.  
  98.         return ret;
  99.     }
  100.  
  101.     /**
  102.      * Places a fence on the desired emplacement
  103.      * The overlaping validity is checked by the square object by fenceStatus
  104.      * The path validity is checked by the checkExistingPath method
  105.      * @author
  106.      */
  107.     private void playFence() {
  108.         // TODO - implement HumanPlayer.playFence
  109.         this.listOfOldPositions.clear();
  110.  
  111.         System.out.println("Sur quelle case voulez-vous jouer ?");
  112.  
  113.         int x = this.askX(this.board.getSIZE() - 1);
  114.         int y = this.askY(this.board.getSIZE() - 1);
  115.         String dir = this.askDir();
  116.  
  117.         Square currentSquare = this.getCurrentSquare();
  118.  
  119.         while (!this.checkFencePossible(this.board.getGrid()[this.board.fenceCoord(x)][this.board.fenceCoord(y)], dir)
  120.             || (!this.checkExistingPath(this.game.getPlayer1(), x, y, dir)) || (!this.checkExistingPath(this.game.getPlayer2(), x, y, dir))) {
  121.             System.out.println("Vous ne pouvez pas jouer sur cette case. \n"
  122.                                 + "Veuillez en choisir une autre !");
  123.  
  124.             x = this.askX(this.board.getSIZE() - 1);
  125.             y = this.askY(this.board.getSIZE() - 1);
  126.             dir = this.askDir();
  127.  
  128.             this.setCurrentSquare(currentSquare);
  129.         }
  130.  
  131.         this.setCurrentSquare(currentSquare);
  132.  
  133.         this.board.setFence(this.board.fenceCoord(x), this.board.fenceCoord(y), dir, this);
  134.         this.setNbFences(this.nbFences - 1);
  135.     }
  136.  
  137.     private void playFence(Square sq) {
  138.         // TODO - implement HumanPlayer.playFence
  139.         this.listOfOldPositions.clear();
  140.  
  141.         Square currentSquare = this.getCurrentSquare();
  142.  
  143.         while (!this.checkFencePossible(this.board.getGrid()[this.board.fenceCoord(x)][this.board.fenceCoord(y)], dir)
  144.             || (!this.checkExistingPath(this.game.getPlayer1(), x, y, dir)) || (!this.checkExistingPath(this.game.getPlayer2(), x, y, dir))) {
  145.             System.out.println("Vous ne pouvez pas jouer sur cette case. \n"
  146.                                 + "Veuillez en choisir une autre !");
  147.  
  148.             x = this.askX(this.board.getSIZE() - 1);
  149.             y = this.askY(this.board.getSIZE() - 1);
  150.             dir = this.askDir();
  151.  
  152.             this.setCurrentSquare(currentSquare);
  153.         }
  154.  
  155.         this.setCurrentSquare(currentSquare);
  156.  
  157.         this.board.setFence(this.board.fenceCoord(x), this.board.fenceCoord(y), dir, this);
  158.         this.setNbFences(this.nbFences - 1);
  159.     }
  160.  
  161.     /**
  162.      * Moves the pawns to the desired direction.
  163.      * The validity is checked by the current square fenceStatus
  164.      * @author
  165.      */
  166.     private void playPawn() {
  167.         System.out.print("Vous pouvez jouer un pion sur les cases : ");
  168.  
  169.         this.board.printListOfPossibilitiesPawn(this);
  170.         this.game.getBoardGUI().addTmpPossibilities(this.board.listOfPossibilitiesPawn(this));
  171.  
  172.         System.out.println("Sur quelle case voulez-vous jouer ?");
  173.         int x = this.askX(this.board.getSIZE());
  174.         int y = this.askY(this.board.getSIZE());
  175.  
  176.         while (((this.board.pawnCoord(x) == this.currentSquare.getX()) && (this.board.pawnCoord(y) == this.currentSquare.getY()))
  177.                 || (this.board.listOfPossibilitiesPawn(this).contains(this.board.getGrid()[this.board.pawnCoord(x)][this.board.pawnCoord(y)]) == false)) {
  178.             System.out.println("Vous ne pouvez pas jouer sur cette case. \n"
  179.                                 + "Veuillez en choisir une autre !");
  180.  
  181.             x = this.askX(this.board.getSIZE());
  182.             y = this.askY(this.board.getSIZE());
  183.         }
  184.         super.movePawn(this.board.pawnCoord(x), this.board.pawnCoord(y));
  185.     }
  186.  
  187.  
  188.     // Méthode pour Pawn pour l'instant ...
  189.     private int askX(int maxSize) {
  190.         int x = 0;
  191.  
  192.         System.out.print("\nCoordonnée x : ");
  193.  
  194.         boolean isNumeric = false;
  195.         while (!isNumeric) {
  196.             try {
  197.                 x = this.scan.nextInt();
  198.                 isNumeric = true;
  199.             } catch (java.util.InputMismatchException e) {
  200.                 System.out.println("La chaîne de caractère est incorrecte !");
  201.                 this.scan.nextLine();
  202.                 System.out.print("Coordonnée x : ");
  203.             }
  204.         }
  205.  
  206.         while ((x < 0) || (x >= maxSize)) {
  207.             System.out.println("La coordonnée n'est pas valide. Veuillez en saisir une nouvelle !");
  208.             System.out.print("\nCoordonnée x : ");
  209.             isNumeric = false;
  210.             while (!isNumeric) {
  211.                 try {
  212.                     x = this.scan.nextInt();
  213.                     isNumeric = true;
  214.                 } catch (java.util.InputMismatchException e) {
  215.                     System.out.println("La chaîne de caractère est incorrecte !");
  216.                     this.scan.nextLine();
  217.                     System.out.print("Coordonnée x : ");
  218.                 }
  219.             }
  220.         }
  221.  
  222.         return x;
  223.     }
  224.  
  225.     // Méthode pour Pawn pour l'instant ...
  226.     private int askY(int maxSize) {
  227.         int y = 0;
  228.  
  229.         System.out.print("Coordonnée y : ");
  230.         boolean isNumeric = false;
  231.         while (!isNumeric) {
  232.             try {
  233.                 y = this.scan.nextInt();
  234.                 isNumeric = true;
  235.             } catch (java.util.InputMismatchException e) {
  236.                 System.out.println("La chaîne de caractère est incorrecte !");
  237.                 this.scan.nextLine();
  238.                 System.out.print("Coordonnée y : ");
  239.             }
  240.         }
  241.  
  242.         while((y < 0) || (y >= maxSize)) {
  243.             System.out.println("La coordonnée n'est pas valide. Veuillez en saisir une nouvelle !");
  244.             System.out.print("\nCoordonnée y : ");
  245.             isNumeric = false;
  246.             while (!isNumeric) {
  247.                 try {
  248.                     y = this.scan.nextInt();
  249.                     isNumeric = true;
  250.                 } catch (java.util.InputMismatchException e) {
  251.                     System.out.println("La chaîne de caractère est incorrecte !");
  252.                     this.scan.nextLine();
  253.                     System.out.print("Coordonnée y : ");
  254.                 }
  255.             }
  256.         }
  257.  
  258.         return y;
  259.     }
  260.  
  261.     private String askDir() {
  262.         String ret = null;
  263.  
  264.         System.out.println("Dans quelle direction voulez-vous mettre le mur ? \n"
  265.             +"'H' (Horizontal) ou 'V' (Vertical)");
  266.  
  267.         this.scan = new Scanner(System.in);
  268.         ret = this.scan.nextLine();
  269.  
  270.         while ((!ret.equalsIgnoreCase("h")) && (!ret.equalsIgnoreCase("horizontal")) &&
  271.             (!ret.equalsIgnoreCase("v")) && (!ret.equalsIgnoreCase("vertical"))) {
  272.                 System.out.println("La chaîne de caractères est incorrecte !\n"
  273.                     + "Veuillez écrire la direction du mur que vous voulez jouer : \n"
  274.                     + "'H' (Horizontal) ou 'V' (Vertical)");
  275.                 ret = this.scan.nextLine();
  276.         }
  277.  
  278.         if ((ret.equalsIgnoreCase("h")) || (ret.equalsIgnoreCase("horizontal"))) {
  279.             ret = "h";
  280.         }
  281.         else {
  282.             ret = "v";
  283.         }
  284.  
  285.         return ret;
  286.     }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement