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() {
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() {
// 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<width; j++) {
switch (board[i].charAt(j)) {
char c = board[i].charAt(j);
switch (c) {
case 'X':
System.out.print(" \u001B[31m" + board[i].charAt(j) + "\u001B[0m |");
System.out.print(" \u001B[31m" + c + "\u001B[0m |");
break;
case 'O':
System.out.print(" \u001B[32m" + board[i].charAt(j) + "\u001B[0m |");
System.out.print(" \u001B[32m" + c + "\u001B[0m |");
break;
default:
System.out.print(" " + board[i].charAt(j) + " |");
System.out.print(" " + c + " |");
}
}
System.out.println(" " + i);
System.out.println(horizontalLine);
}
System.out.print("x");
for (int i = 0; i < height; i++) {
if(i<10) {
System.out.print(" "+i+" ");
} else {
System.out.print(" "+i+" ");
}
System.out.print("X");
for (int i = 0; i < width; i++) {
System.out.print(String.format(" %-2d ", i));
}
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.
*/
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;

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_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,8 +83,13 @@ 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);
@@ -147,20 +152,24 @@ public class GomokuGame {
while (currentPlay == null) {
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))) {
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;
}
this.currentPlayer.tokens -= 1;
this.currentPlayer = this.nextPlayer();
renderer.update();
//System.out.println(this.board);
}
// this.renderer.updateStatus("Match nul, il ne reste plus de jeton.");
System.out.println("Match nul, il ne reste plus de jeton");
this.renderer.updateStatus("Match nul, il ne reste plus de jetons.");
return;
}