Add proper console rendering

This commit is contained in:
Dorian HAMDANI
2025-05-06 14:31:17 +02:00
parent 6a5c6a4917
commit f69ce5064b

View File

@@ -1,6 +1,15 @@
public class ConsoleRenderer extends GomokuRenderer {
// Color constants
public static final String RESET = "\u001B[0m";
public static final String BOLD = "\u001B[1m";
public static final String COLOR_RED = "\u001B[31m";
public static final String COLOR_GREEN = "\u001B[32m";
public static final String COLOR_GRAY = "\u001B[38;5;240m";
public static final String CLEAR_TERM = "\033[H\033[2J";
public ConsoleRenderer() {
}
@@ -15,11 +24,12 @@ public class ConsoleRenderer extends GomokuRenderer {
// Print the board to the console
// Clear the console
System.out.print("\033[H\033[2J");
System.out.print(CLEAR_TERM);
System.out.flush();
String[] board = game.getBoard().toString().split("\n");
String horizontalLine = getHorizontalLine();
String horizontalLine = getHorizontalLine(RESET);
String horizontalLineGray = getHorizontalLine(COLOR_GRAY);
int width = game.getBoard().getWidth();
int height = game.getBoard().getHeight();
@@ -36,18 +46,20 @@ public class ConsoleRenderer extends GomokuRenderer {
char c = board[i].charAt(j);
switch (c) {
case 'X':
System.out.print(" \u001B[31m" + c + "\u001B[0m |");
System.out.print(" " + COLOR_RED + BOLD + c + RESET + " ");
break;
case 'O':
System.out.print(" \u001B[32m" + c + "\u001B[0m |");
System.out.print(" " + COLOR_GREEN + BOLD + c + RESET + " ");
break;
default:
System.out.print(" " + c + " |");
System.out.print(COLOR_GRAY + " " + c + " " + RESET);
}
if (j < width - 1) System.out.print(COLOR_GRAY + "|" + RESET);
}
System.out.println(RESET + "| " + (i + 1));
if (i < height - 1) System.out.println(horizontalLineGray);
}
System.out.println(" " + (i + 1));
System.out.println(horizontalLine);
}
System.out.print("X");
for (int i = 1; i <= width; i++) {
System.out.print(String.format(" %-2d ", i));
@@ -56,13 +68,13 @@ public class ConsoleRenderer extends GomokuRenderer {
}
private String getHorizontalLine() {
private String getHorizontalLine(String color) {
StringBuilder sb = new StringBuilder();
sb.append("+");
sb.append("+" + color);
for (int i = 0; i < game.getBoard().getWidth()*4-1; i++) {
sb.append("-");
}
sb.append("+");
sb.append(RESET + "+");
return sb.toString();
}