diff --git a/src/Color.java b/src/Color.java index 86b6b62..af9eeaa 100644 --- a/src/Color.java +++ b/src/Color.java @@ -1,7 +1,28 @@ - public enum Color { - NIL, - WHITE, - BLACK; + NIL(-1), + WHITE(0), + BLACK(1); + + private final int val; + + Color(int i) { + this.val = i; + } + + public Color fromInt(int i) { + for (Color c : Color.values()) { + if (c.val == val) { + return c; + } + } + throw new IllegalArgumentException("Invalid value: " + this); + + } + + public Color inverse() { + if (this.val == -1) + throw new IllegalArgumentException("No inverse of NIL"); + return fromInt((this.val + 1) % 2); + } } diff --git a/src/GomokuAI.java b/src/GomokuAI.java index f7d9ceb..33fceb8 100644 --- a/src/GomokuAI.java +++ b/src/GomokuAI.java @@ -1,52 +1,55 @@ -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; +import java.util.HashMap; -public class GomokuAI extends Player{ - +public class GomokuAI extends Player { - //------------------Constructors-------------------------- + // ------------------Constructors-------------------------- /** * The constructor of the Human. - * @param name The name of the player. + * + * @param name The name of the player. * @param color The color of the player. */ - public GomokuAI(String name,Color color){ - super(name,color); + public GomokuAI(String name, Color color) { + super(name, color); } /** - * This class is an extends of the class Player, this allows the GomokuAI to choose his move. - */ + * This class is an extends of the class Player, this allows the GomokuAI to + * choose his move. + */ /** The random initialization */ Random random = new Random(); /** The difficulty of the GomokuAI */ int difficulty = 0; - //------------------Methods-------------------------- + // ------------------Methods-------------------------- /** - * Return the coordinate of the move played by the Gomoku AI. - * @param board The actual Gomoku board. - * @return The Cell of the move played. - */ - public GomokuCell chooseMove(GomokuBoard board){ - + * Return the coordinate of the move played by the Gomoku AI. + * + * @param board The actual Gomoku board. + * @return The Cell of the move played. + */ + @Override + public GomokuCell chooseMove(GomokuBoard board) { + List playableCell = board.getPlayableCells(); GomokuCell playCell; - int x=0,y=0; - + int x = 0, y = 0; + switch (difficulty) { case 0: playCell = playableCell.get(random.nextInt(playableCell.size())); break; case 1: Map map = GetCellPoint(board); - int max=0; + int max = 0; for (Map.Entry entry : map.entrySet()) { - if(entry.getValue() > max){ + if (entry.getValue() > max) { max = entry.getValue(); playCell = entry.getKey(); } @@ -55,25 +58,25 @@ public class GomokuAI extends Player{ throw new AssertionError(); } - System.out.println("L'IA à choisi : "+ x+", "+ y); + System.out.println("L'IA à choisi : " + x + ", " + y); - return playCell; } /** - * Return a Map of all Cell playable, and their point. - * @param board The actual Gomoku board. - * @return the Map of all Cell playable, and their point. - */ - public Map GetCellPoint(GomokuBoard board){ - + * Return a Map of all Cell playable, and their point. + * + * @param board The actual Gomoku board. + * @return the Map of all Cell playable, and their point. + */ + public Map GetCellPoint(GomokuBoard board) { + List playableCell = board.getPlayableCells(); Map map = new HashMap<>(); for (GomokuCell gomokuCell : playableCell) { - switch(this.color){ + switch (this.color) { case Color.WHITE: gomokuCell.setState(Color.BLACK); @@ -93,6 +96,4 @@ public class GomokuAI extends Player{ return map; } - - } diff --git a/src/GomokuGame.java b/src/GomokuGame.java index 10eee43..94d5b01 100644 --- a/src/GomokuGame.java +++ b/src/GomokuGame.java @@ -1,12 +1,12 @@ -import java.nio.file.Path; -import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Random; +import java.io.BufferedWriter; public class GomokuGame { - public static final int DEFAULT_BOARD_WIDTH = 15; // largeur du plateau + public static final int DEFAULT_BOARD_WIDTH = 15; // largeur du plateau public static final int DEFAULT_BOARD_HEIGHT = 15; // hauteur du plateau public static final int DEFAULT_TOKENS_COUNT = 60; // nb de jetons @@ -30,13 +30,32 @@ public class GomokuGame { for (int i = 0; i < g.boardHeight; ++i) { for (int j = 0; j < g.boardWidth; ++j) { - System.out.print(g.getBoard().get(i ,j)); + System.out.print(g.getBoard().get(i, j)); } System.out.println(); } } - public void newGame() { + public void newGame(boolean bot, String name1, String name2) { + Color[] possible = { Color.WHITE, Color.BLACK }; + int rnd = new Random().nextInt(possible.length); + Color colorPlayer1 = possible[rnd]; + Color colorPlayer2 = colorPlayer1.inverse(); + + this.player1 = new Human(name1, colorPlayer1); + + if (bot) { + this.player2 = new GomokuAI(name2, colorPlayer2); + + } else { + this.player2 = new Human(name2, colorPlayer2); + } + + this.board = new GomokuBoard(15, 15); + this.colorP1 = colorPlayer1; + this.currP = this.player1.color == Color.WHITE ? 1: 2; + this.nbTokens1 = 60; + this.nbTokens2 = 60; } @@ -48,7 +67,7 @@ public class GomokuGame { // save the game state to the file // 1. Open the file // 2. Write the first line with: - // w h colorP1 currP nbTokens1 nbTokens2 + // w h colorP1 currP nbTokens1 nbTokens2 // 3. Write the next h lines with the board // 4. Close the file // 5. Return true if successful, false otherwise @@ -68,12 +87,19 @@ public class GomokuGame { for (int j = 0; j < boardWidth; ++j) { char c; switch (board.get(i, j).getState()) { - case BLACK: c = 'X'; break; - case WHITE: c = 'O'; break; - case NIL: c = '.'; break; - default: throw new IllegalStateException( - String.format("Unexpected value at cell (%d, %d): %s", i, j, board.get(i, j).getState()) - ); + case BLACK: + c = 'X'; + break; + case WHITE: + + break; + case NIL: + c = '.'; + break; + default: + throw new IllegalStateException( + String.format("Unexpected value at cell (%d, %d): %s", i, j, + board.get(i, j).getState())); } writer.write(c); } @@ -93,7 +119,7 @@ public class GomokuGame { // load the game state from the file // 1. Open the file // 2. Read the first line to get: - // w h colorP1 currP nbTokens1 nbTokens2 + // w h colorP1 currP nbTokens1 nbTokens2 // 3. Read the next h lines to get the board // 4. Close the file // 5. Initialize the board with the read values @@ -136,9 +162,15 @@ public class GomokuGame { for (int j = 0; j < w; ++j) { switch (line.charAt(j)) { - case 'X': colors[i][j] = Color.BLACK; break; - case 'O': colors[i][j] = Color.WHITE; break; - case '.': colors[i][j] = Color.NIL; break; + case 'X': + colors[i][j] = Color.BLACK; + break; + case 'O': + colors[i][j] = Color.WHITE; + break; + case '.': + colors[i][j] = Color.NIL; + break; default: throw new IllegalArgumentException("Invalid color: " + line.charAt(j)); } @@ -160,7 +192,7 @@ public class GomokuGame { } // Initialize the board with the read values - this.boardWidth = w; + this.boardWidth = w; this.boardHeight = h; this.board = new GomokuBoard(w, h, colors); diff --git a/src/Human.java b/src/Human.java index 8ebf06d..12ae898 100644 --- a/src/Human.java +++ b/src/Human.java @@ -1,78 +1,74 @@ import java.io.Console; -public class Human extends Player{ +public class Human extends Player { - - - //------------------Constructors-------------------------- + // ------------------Constructors-------------------------- /** * The constructor of the Human. - * @param name The name of the player. + * + * @param name The name of the player. * @param color The color of the player. */ - public Human(String name,Color color){ - super(name,color); + public Human(String name, Color color) { + super(name, color); } /** - * This class is an extends of the class Player, this allows the player to choose his move. + * This class is an extends of the class Player, this allows the player to + * choose his move. */ - //------------------Methods-------------------------- + // ------------------Methods-------------------------- /** - * Return the coordinate of the move played by the player. - * @param board The actual Gomoku board. - * @return The cell of the move played. - */ - public GomokuCell chooseMove(GomokuBoard board){ - + * Return the coordinate of the move played by the player. + * + * @param board The actual Gomoku board. + * @return The cell of the move played. + */ + @Override + public GomokuCell chooseMove(GomokuBoard board) { + Console cons = System.console(); - int x=0,y=0; + int x = 0, y = 0; boolean pass = false; - do{ + do { - do{ + do { try { System.out.println("Veuillez saisir le X"); - x = Integer.parseInt( cons.readLine()); - pass=true; - + x = Integer.parseInt(cons.readLine()); + pass = true; + } catch (NumberFormatException e) { System.out.println("Ce n'est pas un nombre !"); - pass=false; + pass = false; } - } - while(!pass); - - do{ + } while (!pass); + + do { try { System.out.println("Veuillez saisir le Y"); - y = Integer.parseInt( cons.readLine()); - pass=true; - + y = Integer.parseInt(cons.readLine()); + pass = true; + } catch (NumberFormatException e) { System.out.println("Ce n'est pas un nombre !"); - pass=false; + pass = false; } - } - while(!pass); + } while (!pass); - - pass =board.get(x,y).isPlayable(); - if(!pass) - { + pass = board.get(x, y).isPlayable(); + if (!pass) { System.out.println("Cette case n'est pas jouable !"); } - } - while(!pass); - - System.out.println("Vous avez saisi : "+ x+", "+ y); + } while (!pass); - - return board.get(x,y); + System.out.println("Vous avez saisi : " + x + ", " + y); + + return board.get(x, y); } }