Document ConsoleRenderer class file and fix incorrect renderer constructors
This commit is contained in:
@@ -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<width; j++) {
|
||||
char c = board[i].charAt(j);
|
||||
switch (c) {
|
||||
case 'X':
|
||||
System.out.print(" " + COLOR_RED + BOLD + c + RESET + " ");
|
||||
break;
|
||||
case 'O':
|
||||
System.out.print(" " + COLOR_GREEN + BOLD + c + RESET + " ");
|
||||
break;
|
||||
default:
|
||||
System.out.print(COLOR_GRAY + " " + c + " " + RESET);
|
||||
}
|
||||
if (j < width - 1) System.out.print(COLOR_GRAY + "|" + RESET);
|
||||
}
|
||||
System.out.println(RESET + "| " + (i + 1));
|
||||
printBoardLine(boardLines[i], width);
|
||||
System.out.println(" " + (i + 1));
|
||||
if (i < height - 1) System.out.println(horizontalLineGray);
|
||||
}
|
||||
System.out.println(horizontalLine);
|
||||
System.out.print("X");
|
||||
for (int i = 1; i <= width; i++) {
|
||||
System.out.print(String.format(" %-2d ", i));
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
System.out.println(horizontalLine);
|
||||
// Print the column indices
|
||||
printColumnIndices(width);
|
||||
System.out.println(horizontalLine);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a horizontal line separator for the board renderer.
|
||||
*
|
||||
* This method generates a horizontal line separator for the board renderer
|
||||
* with the specified color.
|
||||
*
|
||||
* @param color The ANSI escape code for the color of the line.
|
||||
* @return A string representing the horizontal line separator.
|
||||
*/
|
||||
private String getHorizontalLine(String color) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("+" + color);
|
||||
@@ -78,9 +109,58 @@ public class ConsoleRenderer extends GomokuRenderer {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints a line of the board to the console.
|
||||
*
|
||||
* This method prints a line of the board to the console. It uses ANSI escape
|
||||
* codes to color the tokens based on their type (X or O).
|
||||
*
|
||||
* @param line The line of the board to be printed.
|
||||
* @param width The width of the board.
|
||||
*/
|
||||
private void printBoardLine(String line, int width) {
|
||||
System.out.print("|");
|
||||
// Print each character in the line with the appropriate color
|
||||
for (int i = 0; i < width; i++) {
|
||||
char c = line.charAt(i);
|
||||
String color = switch (c) {
|
||||
case 'X' -> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user