diff --git a/src/ConsoleRenderer.java b/src/ConsoleRenderer.java index ab7c802..ddb236d 100644 --- a/src/ConsoleRenderer.java +++ b/src/ConsoleRenderer.java @@ -13,6 +13,10 @@ public class ConsoleRenderer extends GomokuRenderer { @Override public void update() { // Print the board to the console + + //clear la console + System.out.print("\033[H\033[2J"); + System.out.flush(); String[] board = game.getBoard().toString().split("\n"); String horizontalLine = getHorizontalLine(); @@ -21,7 +25,7 @@ public class ConsoleRenderer extends GomokuRenderer { // Print a separator line, followed by the game infos System.out.println(horizontalLine); - System.out.println("|" + String.format(" %-" + (width - 1) + "s", "Gomoku Game!") + "|"); + System.out.println("|" + String.format(" %-" + (width*4 - 2) + "s", "Gomoku Game!") + "|"); // System.out.println("Current player: " + game.getCurrentPlayer().getName()); // System.out.println("Number of tokens left: " + game.getCurrentPlayer().getTokensLeft()); @@ -29,17 +33,28 @@ public class ConsoleRenderer extends GomokuRenderer { System.out.println(horizontalLine); for (int i = 0; i < height; i++) { System.out.print("|"); - System.out.print(board[i]); - System.out.println("|"); + for (int j=0; j neighbour; - /** - * The state of the cell represented by the enumerate Color, Nil is empty, white - * and black for the color of a token - */ - private Color state; +public class GomokuCell{ + /** Enumerate neighbor with key in Cardinal direction and key representing the cell in that direction. */ + private EnumMap neighbour; + /** The state of the cell represented by the enumerate Color, Nil is empty, white and black for the color of a token */ + private Color state; - // ------------------Constructors-------------------------- + //------------------Constructors-------------------------- /** * The constructor of the cell. - * * @param state The state by default of the cell. */ - public GomokuCell(Color state) { + public GomokuCell(Color state){ this.neighbour = new EnumMap<>(Cardinal.class); this.state = state; } - // ------------------Gets-------------------------- + //------------------Gets-------------------------- /** * Return one neighbour. - * * @param car The Cardinal direction. * @return The GomokuCell at the direction. */ - public GomokuCell getNeighbour(Cardinal car) { + public GomokuCell getNeighbour(Cardinal car){ return this.neighbour.get(car); } - + /** * Return neighbours. - * * @return The EnumMap of neighbours. */ - public EnumMap getAllNeighbour() { + public EnumMap getAllNeighbour(){ return neighbour; } /** - * Return the number of same colored neighbours in all direction. - * - * @return The Map of neighbours. - */ - public Map getSameColorNeighbour() { + * Return the number of same colored neighbours in all direction. + * @return The Map of neighbours. + */ + public Map getSameColorNeighbour(){ Map map = new HashMap<>(); map.put(Cardinal.N, 0); - map.put(Cardinal.W, 0); + map.put(Cardinal.W,0); map.put(Cardinal.NW, 0); map.put(Cardinal.SW, 0); map.put(Cardinal.S, 0); @@ -70,94 +60,87 @@ public class GomokuCell { for (Map.Entry entry : map.entrySet()) { GomokuCell actualcell = this; - while (this.getState() == actualcell.getNeighbour(entry.getKey()).getState()) { - entry.setValue(entry.getValue() + 1); - actualcell = actualcell.getNeighbour(entry.getKey()); + while(this.getState() == actualcell.getNeighbour(entry.getKey()).getState()) + { + entry.setValue(entry.getValue()+1); + actualcell=actualcell.getNeighbour(entry.getKey()); } } return map; } - + /** * Return the state. - * * @return The state of the current cell with one Color. */ - public Color getState() { + public Color getState(){ return this.state; } - // ------------------Sets-------------------------- - + //------------------Sets-------------------------- + /** * Change state of the current cell. - * * @param c The color of the token played. * @throws IllegalStateException If the cell is not playable. */ - public void setState(Color c) { + public void setState(Color c){ this.state = c; } + - // ------------------Booleans-------------------------- + //------------------Booleans-------------------------- /** * This method returns if the current cell is empty - * * @return True if is empty, False if is not. */ - public boolean isEmpty() { + public boolean isEmpty(){ return this.state == Color.NIL; } - + /** * This method returns if the cell has already played. - * * @return True if the cell is already played, False if is not. */ - public boolean isPlayed() { + public boolean isPlayed(){ return !this.isEmpty(); } - + /** * Return if the cell is playable. - * - * @return True if the current cell can be played with the condition of the - * gomoku. + * @return True if the current cell can be played with the condition of the gomoku. */ - public boolean isPlayable() { - if (this.state != Color.NIL) - return false; + public boolean isPlayable(){ for (Cardinal c : Cardinal.values()) { GomokuCell current = this.getNeighbour(c); - if (current != null && current.isPlayed()) - return true; + if (current != null && current.isPlayed()) return true; } return false; } - // ------------------Methods-------------------------- + + //------------------Methods-------------------------- /** * This method link the cell at the Cardinal position on the current cell. - * - * @param car The Cardinal direction of the cell to link. + * @param car The Cardinal direction of the cell to link. * @param cell The GomokuCell to link. */ - public void linkCell(Cardinal car, GomokuCell cell) { + public void linkCell(Cardinal car, GomokuCell cell){ this.neighbour.put(car, cell); } - // ------------------Statics-------------------------- + + //------------------Statics-------------------------- - public static void link(GomokuCell c1, GomokuCell c2, Cardinal c1Toc2) { - if (c1 == null || c2 == null) - return; + public static void link(GomokuCell c1, GomokuCell c2, Cardinal c1Toc2){ + if (c1 == null || c2 == null) return ; c1.linkCell(c1Toc2, c2); c2.linkCell(c1Toc2.inverse(), c1); } - // ------------------Overides-------------------------- + //------------------Overides-------------------------- @Override public String toString() { switch (this.getState()) { diff --git a/src/GomokuGame.java b/src/GomokuGame.java index 2d311e4..636f875 100644 --- a/src/GomokuGame.java +++ b/src/GomokuGame.java @@ -84,11 +84,13 @@ public class GomokuGame { } } GomokuGame g = new GomokuGame(false);// metre true ou fals si in veut l'affichage ou non + g.renderer = new ConsoleRenderer(); + g.renderer.init(g); g.board = new GomokuBoard(sizeX, sizeY); g.board.get(sizeX / 2, sizeY / 2).setState(Color.BLACK); g.player1 = new Human("un", Color.WHITE, nbToken); g.player2 = new GomokuAI("deux", Color.BLACK, nbToken - 1); - System.out.println(g.board); + //System.out.println(g.board); // g.renderer.update(); g.startGame(); @@ -139,6 +141,7 @@ public class GomokuGame { * } */ this.currentPlayer = player1; + renderer.update(); while (this.player1.tokens > 0 || this.player2.tokens > 0) { GomokuCell currentPlay = null; while (currentPlay == null) { @@ -152,7 +155,8 @@ public class GomokuGame { } this.currentPlayer.tokens -= 1; this.currentPlayer = this.nextPlayer(); - System.out.println(this.board); + renderer.update(); + //System.out.println(this.board); } // this.renderer.updateStatus("Match nul, il ne reste plus de jeton.");