Refactor console rendering

This commit is contained in:
Dorian HAMDANI
2025-04-14 10:54:24 +02:00
parent 0eb7f1442e
commit c0517b8f1e
2 changed files with 13 additions and 21 deletions

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 < width; 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

@@ -160,20 +160,16 @@ public class GomokuGame {
this.board.expandBoard(cardinal); this.board.expandBoard(cardinal);
} }
} }
System.out.println(board.countMax(board.countAlignedCells(currentPlay)));
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;
} }