From c17fcb9aaa9bff86f2415916f5a89a0c3d08ac24 Mon Sep 17 00:00:00 2001 From: Dorian HAMDANI Date: Fri, 9 May 2025 09:16:34 +0200 Subject: [PATCH] Document ConsoleRenderer class file and fix incorrect renderer constructors --- src/ConsoleRenderer.java | 152 +++++++++++++++++++++++++++++---------- src/GomokuGame.java | 4 +- src/GomokuRenderer.java | 11 +-- 3 files changed, 120 insertions(+), 47 deletions(-) diff --git a/src/ConsoleRenderer.java b/src/ConsoleRenderer.java index a3ee8ff..c0a0fdb 100644 --- a/src/ConsoleRenderer.java +++ b/src/ConsoleRenderer.java @@ -1,73 +1,104 @@ +/** + * Simple console renderer for the Gomoku game. + * + * This class is responsible for rendering the Gomoku game board and its status + * to the console. + * It uses ANSI escape codes for color formatting, which may not work on all + * terminals. + */ public class ConsoleRenderer extends GomokuRenderer { - // Color constants + // - Color Constants ---------------------------------- + + // ANSI escape codes for colors 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"; + // ANSI escape code to clear the console public static final String CLEAR_TERM = "\033[H\033[2J"; - public ConsoleRenderer() { + // - Constructors ------------------------------------- + + /** + * Constructor for the ConsoleRenderer class. + * + * This constructor initializes the console renderer with the specified + * Gomoku game instance. + * + * @param game The Gomoku game instance to be rendered. + * @throws IllegalArgumentException if the game instance is null. + */ + public ConsoleRenderer(GomokuGame game) { + super(game); } + // - Override Methods --------------------------------- + + /** + * Initializes the console renderer. + * + * This method is called to set up the renderer before it starts + * rendering the game. + */ @Override - public void init(GomokuGame game) { - this.game = game; + public void init() { + // Initialize the console renderer System.out.println("ConsoleRenderer initialized"); } + /** + * Renders the game board to the console. + * + * This method is called to update the console with the current state of the + * game board. It clears the console and prints the board with colors for + * the players' pieces, as well as the row and column numbers. + */ @Override public void update() { - // Print the board to the console - // Clear the console System.out.print(CLEAR_TERM); System.out.flush(); - String[] board = game.getBoard().toString().split("\n"); - String horizontalLine = getHorizontalLine(RESET); - String horizontalLineGray = getHorizontalLine(COLOR_GRAY); + // Set-up variables int width = game.getBoard().getWidth(); int height = game.getBoard().getHeight(); - // Print a separator line, followed by the game infos + String[] boardLines = game.getBoard().toString().split("\n"); + + String horizontalLine = getHorizontalLine(RESET); + String horizontalLineGray = getHorizontalLine(COLOR_GRAY); + + // Print the game title + System.out.println(horizontalLine); + System.out.println(String.format("| %-" + (width * 4 - 2) + "s| Y", "Gomoku Game!")); System.out.println(horizontalLine); - System.out.println("|" + String.format(" %-" + (width*4 - 2) + "s", "Gomoku Game!") + "| Y"); // Print the board - System.out.println(horizontalLine); for (int i = 0; i < height; i++) { - System.out.print("|"); - - for (int j=0; j COLOR_RED; + case 'O' -> COLOR_GREEN; + default -> COLOR_GRAY; + }; + // Print the character and a vertical separator if not the last column + System.out.print(" " + color + BOLD + c + RESET + " "); + if (i < width - 1) System.out.print(COLOR_GRAY + "|" + RESET); + } + System.out.print(RESET + "|"); + } + + /** + * Prints the column indices to the console. + * + * This method prints the column indices (1, 2, 3, ...) to the console + * below the board. + * + * @param width The width of the board. + */ + private void printColumnIndices(int width) { + System.out.print("X"); + for (int i = 1; i <= width; i++) { + System.out.print(String.format(" %-2d ", i)); + } + System.out.println(); + } + + /** + * Displays a message to the console. + * + * This method is called to display a message to the user in the console. + * + * @param message The message to be displayed. + */ @Override public void updateStatus(String status) { // Print the status to the console - System.out.println("Status: " + status); + System.out.println(COLOR_GRAY + BOLD + status + RESET); } } diff --git a/src/GomokuGame.java b/src/GomokuGame.java index 8938399..500ed2e 100644 --- a/src/GomokuGame.java +++ b/src/GomokuGame.java @@ -28,9 +28,9 @@ public class GomokuGame { GomokuGame(int nbToken, int jtToWin, int x, int y, boolean pvp, int difficulty) { Random r = new Random(); - this.renderer = new ConsoleRenderer(); + this.renderer = new ConsoleRenderer(this); this.NB_CELL_PLAY = jtToWin; - this.renderer.init(this); + this.renderer.init(); this.board = new GomokuBoard(x, y); this.board.get(x / 2, y / 2).setState(Color.BLACK); diff --git a/src/GomokuRenderer.java b/src/GomokuRenderer.java index 911f76b..ddf3320 100644 --- a/src/GomokuRenderer.java +++ b/src/GomokuRenderer.java @@ -28,20 +28,13 @@ public abstract class GomokuRenderer { * Initializes the Gomoku renderer with the game instance. * * @param game The Gomoku game instance to be rendered. + * @throws IllegalArgumentException if the game instance is null. */ public GomokuRenderer(GomokuGame game) { + if (game == null) throw new IllegalArgumentException("Game cannot be null"); this.game = game; } - /** - * Default constructor for the Gomoku renderer. - * - * This constructor is used when no game instance is provided. - */ - public GomokuRenderer() { - this(null); - } - /** * Initializes the Gomoku renderer. *