This commit is contained in:
Cyprien111
2025-04-14 10:58:50 +02:00
4 changed files with 33 additions and 24 deletions

View File

@@ -83,4 +83,7 @@ public enum Cardinal {
public Cardinal rotate90CCW() { public Cardinal rotate90CCW() {
return rotate(-2); return rotate(-2);
} }
// North, South, East, West cardinals array
public static final Cardinal[] NS_EW = {N, S, E, W};
} }

View File

@@ -14,6 +14,7 @@ public class ConsoleRenderer extends GomokuRenderer {
public void update() { public void update() {
// Print the board to the console // Print the board to the console
// Clear the console
System.out.print("\033[H\033[2J"); System.out.print("\033[H\033[2J");
System.out.flush(); System.out.flush();
@@ -24,9 +25,7 @@ public class ConsoleRenderer extends GomokuRenderer {
// Print a separator line, followed by the game infos // Print a separator line, followed by the game infos
System.out.println(horizontalLine); System.out.println(horizontalLine);
System.out.println("|" + String.format(" %-" + (width*4 - 2) + "s", "Gomoku Game!") + "|"+" y"); 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());
// Print the board // Print the board
System.out.println(horizontalLine); System.out.println(horizontalLine);
@@ -34,27 +33,24 @@ public class ConsoleRenderer extends GomokuRenderer {
System.out.print("|"); System.out.print("|");
for (int j=0; j<width; j++) { for (int j=0; j<width; j++) {
switch (board[i].charAt(j)) { char c = board[i].charAt(j);
switch (c) {
case 'X': case 'X':
System.out.print(" \u001B[31m" + board[i].charAt(j) + "\u001B[0m |"); System.out.print(" \u001B[31m" + c + "\u001B[0m |");
break; break;
case 'O': case 'O':
System.out.print(" \u001B[32m" + board[i].charAt(j) + "\u001B[0m |"); System.out.print(" \u001B[32m" + c + "\u001B[0m |");
break; break;
default: default:
System.out.print(" " + board[i].charAt(j) + " |"); System.out.print(" " + c + " |");
} }
} }
System.out.println(" "+i); System.out.println(" " + i);
System.out.println(horizontalLine); System.out.println(horizontalLine);
} }
System.out.print("x"); System.out.print("X");
for (int i = 0; i < height; i++) { for (int i = 0; i < width; i++) {
if(i<10) { System.out.print(String.format(" %-2d ", i));
System.out.print(" "+i+" ");
} else {
System.out.print(" "+i+" ");
}
} }
System.out.println(""); System.out.println("");

View File

@@ -116,6 +116,7 @@ public class GomokuCell{
* @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(){ public boolean isPlayable(){
if (!this.isEmpty()) return false;
for (Cardinal c : Cardinal.values()) { for (Cardinal c : Cardinal.values()) {
GomokuCell current = this.getNeighbour(c); GomokuCell current = this.getNeighbour(c);
if (current != null && current.isPlayed()) return true; if (current != null && current.isPlayed()) return true;

View File

@@ -11,7 +11,7 @@ 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_BOARD_HEIGHT = 15; // hauteur du plateau
public static final int DEFAULT_TOKENS_COUNT = 60; // nb de jetons 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 player1;
private Player player2; private Player player2;
@@ -83,14 +83,19 @@ public class GomokuGame {
break; 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 GomokuGame g = new GomokuGame(false);// metre true ou fals si in veut l'affichage ou non
g.renderer = new ConsoleRenderer(); g.renderer = new ConsoleRenderer();
g.NB_CELL_PLAY = nbJetonsAligne;
g.renderer.init(g); g.renderer.init(g);
g.board = new GomokuBoard(sizeX, sizeY); g.board = new GomokuBoard(sizeX, sizeY);
g.board.get(sizeX / 2, sizeY / 2).setState(Color.BLACK); g.board.get(sizeX / 2, sizeY / 2).setState(Color.BLACK);
g.player1 = new Human("un", Color.WHITE, nbToken); g.player1 = new Human("un", Color.WHITE, nbToken);
g.player2 = new GomokuAI("deux", Color.BLACK, nbToken - 1); g.player2 = new GomokuAI("deux", Color.BLACK, nbToken - 1);
//System.out.println(g.board); // System.out.println(g.board);
// g.renderer.update(); // g.renderer.update();
g.startGame(); g.startGame();
@@ -147,20 +152,24 @@ public class GomokuGame {
while (currentPlay == null) { while (currentPlay == null) {
currentPlay = this.play(this.currentPlayer); currentPlay = this.play(this.currentPlayer);
} }
System.out.println(board.countMax(board.countAlignedCells(currentPlay))); // 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);
}
}
if (NB_CELL_PLAY <= board.countMax(board.countAlignedCells(currentPlay))) { if (NB_CELL_PLAY <= board.countMax(board.countAlignedCells(currentPlay))) {
System.out.println("Le joueur " + this.currentPlayer + " a gagné !"); this.renderer.updateStatus("Le joueur " + this.currentPlayer + "a gagné !");
// this.renderer.updateStatus("Le joueur " + this.currentPlayer + "a gagné !");
return; return;
} }
this.currentPlayer.tokens -= 1; this.currentPlayer.tokens -= 1;
this.currentPlayer = this.nextPlayer(); this.currentPlayer = this.nextPlayer();
renderer.update(); renderer.update();
//System.out.println(this.board);
} }
// this.renderer.updateStatus("Match nul, il ne reste plus de jeton."); this.renderer.updateStatus("Match nul, il ne reste plus de jetons.");
System.out.println("Match nul, il ne reste plus de jeton");
return; return;
} }