From 77becb3492f246e665017c8acf2912e52dd72871 Mon Sep 17 00:00:00 2001 From: Dorian HAMDANI Date: Mon, 14 Apr 2025 10:24:23 +0200 Subject: [PATCH 1/4] Fix isPlayable --- src/GomokuCell.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GomokuCell.java b/src/GomokuCell.java index b00f055..ba144c3 100644 --- a/src/GomokuCell.java +++ b/src/GomokuCell.java @@ -116,6 +116,7 @@ public class GomokuCell{ * @return True if the current cell can be played with the condition of the gomoku. */ public boolean isPlayable(){ + if (!this.isEmpty()) return false; for (Cardinal c : Cardinal.values()) { GomokuCell current = this.getNeighbour(c); if (current != null && current.isPlayed()) return true; From 2959e4cafa6c420dc6c46aa26d51ff744557adaf Mon Sep 17 00:00:00 2001 From: Aubin DORIVAL Date: Mon, 14 Apr 2025 10:30:24 +0200 Subject: [PATCH 2/4] main --- src/GomokuGame.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/GomokuGame.java b/src/GomokuGame.java index 636f875..f2fe664 100644 --- a/src/GomokuGame.java +++ b/src/GomokuGame.java @@ -11,7 +11,7 @@ public class GomokuGame { 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 - public static final int NB_CELL_PLAY = 5; // nb de jetons + public int NB_CELL_PLAY = 5; // nb de jetons private Player player1; private Player player2; @@ -83,14 +83,19 @@ public class GomokuGame { break; } } + if (sizeY < 3 || sizeX < 3) { + System.out.println("Board is too small !"); + return; + } GomokuGame g = new GomokuGame(false);// metre true ou fals si in veut l'affichage ou non g.renderer = new ConsoleRenderer(); + g.NB_CELL_PLAY = nbJetonsAligne; 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(); @@ -156,7 +161,7 @@ public class GomokuGame { this.currentPlayer.tokens -= 1; this.currentPlayer = this.nextPlayer(); renderer.update(); - //System.out.println(this.board); + // System.out.println(this.board); } // this.renderer.updateStatus("Match nul, il ne reste plus de jeton."); From 207ba127141bbdef5cd5bab88fbeab0fc2aeecd4 Mon Sep 17 00:00:00 2001 From: Dorian HAMDANI Date: Mon, 14 Apr 2025 10:44:50 +0200 Subject: [PATCH 3/4] Add expandable board mechanic, and add static array in Cardinal --- src/Cardinal.java | 3 +++ src/ConsoleRenderer.java | 2 +- src/GomokuGame.java | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Cardinal.java b/src/Cardinal.java index 92696f7..ab787dd 100644 --- a/src/Cardinal.java +++ b/src/Cardinal.java @@ -83,4 +83,7 @@ public enum Cardinal { public Cardinal rotate90CCW() { return rotate(-2); } + + // North, South, East, West cardinals array + public static final Cardinal[] NS_EW = {N, S, E, W}; } diff --git a/src/ConsoleRenderer.java b/src/ConsoleRenderer.java index f9a613f..1231b4b 100644 --- a/src/ConsoleRenderer.java +++ b/src/ConsoleRenderer.java @@ -49,7 +49,7 @@ public class ConsoleRenderer extends GomokuRenderer { System.out.println(horizontalLine); } System.out.print("x"); - for (int i = 0; i < height; i++) { + for (int i = 0; i < width; i++) { if(i<10) { System.out.print(" "+i+" "); } else { diff --git a/src/GomokuGame.java b/src/GomokuGame.java index 636f875..c4565e7 100644 --- a/src/GomokuGame.java +++ b/src/GomokuGame.java @@ -147,6 +147,14 @@ public class GomokuGame { while (currentPlay == null) { currentPlay = this.play(this.currentPlayer); } + // Expand the board if one of the neighbours of the cell is null + // Only check for North, South, East and West neighbours + for (Cardinal cardinal : Cardinal.NS_EW) { + GomokuCell neighbour = currentPlay.getNeighbour(cardinal); + if (neighbour == null) { + this.board.expandBoard(cardinal); + } + } System.out.println(board.countMax(board.countAlignedCells(currentPlay))); if (NB_CELL_PLAY <= board.countMax(board.countAlignedCells(currentPlay))) { System.out.println("Le joueur " + this.currentPlayer + " a gagné !"); From c0517b8f1e91eabad31aae8453e5f2405249c35c Mon Sep 17 00:00:00 2001 From: Dorian HAMDANI Date: Mon, 14 Apr 2025 10:54:24 +0200 Subject: [PATCH 4/4] Refactor console rendering --- src/ConsoleRenderer.java | 26 +++++++++++--------------- src/GomokuGame.java | 8 ++------ 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/ConsoleRenderer.java b/src/ConsoleRenderer.java index 1231b4b..9596ca2 100644 --- a/src/ConsoleRenderer.java +++ b/src/ConsoleRenderer.java @@ -13,7 +13,8 @@ public class ConsoleRenderer extends GomokuRenderer { @Override public void update() { // Print the board to the console - + + // Clear the console System.out.print("\033[H\033[2J"); System.out.flush(); @@ -24,9 +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*4 - 2) + "s", "Gomoku Game!") + "|"+" y"); - // System.out.println("Current player: " + game.getCurrentPlayer().getName()); - // System.out.println("Number of tokens left: " + game.getCurrentPlayer().getTokensLeft()); + System.out.println("|" + String.format(" %-" + (width*4 - 2) + "s", "Gomoku Game!") + "| Y"); // Print the board System.out.println(horizontalLine); @@ -34,27 +33,24 @@ public class ConsoleRenderer extends GomokuRenderer { System.out.print("|"); for (int j=0; j