meilleur affichage

This commit is contained in:
Cyprien111
2025-04-14 09:52:29 +02:00
parent 26da142dc9
commit dd3104e823
3 changed files with 57 additions and 65 deletions

View File

@@ -14,6 +14,10 @@ public class ConsoleRenderer extends GomokuRenderer {
public void update() { public void update() {
// Print the board to the console // Print the board to the console
//clear la console
System.out.print("\033[H\033[2J");
System.out.flush();
String[] board = game.getBoard().toString().split("\n"); String[] board = game.getBoard().toString().split("\n");
String horizontalLine = getHorizontalLine(); String horizontalLine = getHorizontalLine();
int width = game.getBoard().getWidth(); int width = game.getBoard().getWidth();
@@ -21,7 +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 - 1) + "s", "Gomoku Game!") + "|"); System.out.println("|" + String.format(" %-" + (width*4 - 2) + "s", "Gomoku Game!") + "|");
// System.out.println("Current player: " + game.getCurrentPlayer().getName()); // System.out.println("Current player: " + game.getCurrentPlayer().getName());
// System.out.println("Number of tokens left: " + game.getCurrentPlayer().getTokensLeft()); // System.out.println("Number of tokens left: " + game.getCurrentPlayer().getTokensLeft());
@@ -43,6 +47,7 @@ public class ConsoleRenderer extends GomokuRenderer {
System.out.print(" "+i+" "); System.out.print(" "+i+" ");
} }
} }
System.out.println("");
} }

View File

@@ -6,22 +6,15 @@ import java.util.Map;
* This class is cell of the board of the Gomoku game. * This class is cell of the board of the Gomoku game.
*/ */
public class GomokuCell{ public class GomokuCell{
/** /** Enumerate neighbor with key in Cardinal direction and key representing the cell in that direction. */
* Enumerate neighbor with key in Cardinal direction and key representing the
* cell in that direction.
*/
private EnumMap<Cardinal,GomokuCell> neighbour; private EnumMap<Cardinal,GomokuCell> neighbour;
/** /** The state of the cell represented by the enumerate Color, Nil is empty, white and black for the color of a token */
* The state of the cell represented by the enumerate Color, Nil is empty, white
* and black for the color of a token
*/
private Color state; private Color state;
//------------------Constructors-------------------------- //------------------Constructors--------------------------
/** /**
* The constructor of the cell. * The constructor of the cell.
*
* @param state The state by default of the cell. * @param state The state by default of the cell.
*/ */
public GomokuCell(Color state){ public GomokuCell(Color state){
@@ -33,7 +26,6 @@ public class GomokuCell {
/** /**
* Return one neighbour. * Return one neighbour.
*
* @param car The Cardinal direction. * @param car The Cardinal direction.
* @return The GomokuCell at the direction. * @return The GomokuCell at the direction.
*/ */
@@ -43,7 +35,6 @@ public class GomokuCell {
/** /**
* Return neighbours. * Return neighbours.
*
* @return The EnumMap of neighbours. * @return The EnumMap of neighbours.
*/ */
public EnumMap<Cardinal, GomokuCell> getAllNeighbour(){ public EnumMap<Cardinal, GomokuCell> getAllNeighbour(){
@@ -52,7 +43,6 @@ public class GomokuCell {
/** /**
* Return the number of same colored neighbours in all direction. * Return the number of same colored neighbours in all direction.
*
* @return The Map of neighbours. * @return The Map of neighbours.
*/ */
public Map<Cardinal, Integer> getSameColorNeighbour(){ public Map<Cardinal, Integer> getSameColorNeighbour(){
@@ -70,7 +60,8 @@ public class GomokuCell {
for (Map.Entry<Cardinal, Integer> entry : map.entrySet()) { for (Map.Entry<Cardinal, Integer> entry : map.entrySet()) {
GomokuCell actualcell = this; GomokuCell actualcell = this;
while (this.getState() == actualcell.getNeighbour(entry.getKey()).getState()) { while(this.getState() == actualcell.getNeighbour(entry.getKey()).getState())
{
entry.setValue(entry.getValue()+1); entry.setValue(entry.getValue()+1);
actualcell=actualcell.getNeighbour(entry.getKey()); actualcell=actualcell.getNeighbour(entry.getKey());
} }
@@ -80,7 +71,6 @@ public class GomokuCell {
/** /**
* Return the state. * Return the state.
*
* @return The state of the current cell with one Color. * @return The state of the current cell with one Color.
*/ */
public Color getState(){ public Color getState(){
@@ -91,7 +81,6 @@ public class GomokuCell {
/** /**
* Change state of the current cell. * Change state of the current cell.
*
* @param c The color of the token played. * @param c The color of the token played.
* @throws IllegalStateException If the cell is not playable. * @throws IllegalStateException If the cell is not playable.
*/ */
@@ -99,11 +88,11 @@ public class GomokuCell {
this.state = c; this.state = c;
} }
//------------------Booleans-------------------------- //------------------Booleans--------------------------
/** /**
* This method returns if the current cell is empty * This method returns if the current cell is empty
*
* @return True if is empty, False if is not. * @return True if is empty, False if is not.
*/ */
public boolean isEmpty(){ public boolean isEmpty(){
@@ -112,7 +101,6 @@ public class GomokuCell {
/** /**
* This method returns if the cell has already played. * This method returns if the cell has already played.
*
* @return True if the cell is already played, False if is not. * @return True if the cell is already played, False if is not.
*/ */
public boolean isPlayed(){ public boolean isPlayed(){
@@ -121,26 +109,21 @@ public class GomokuCell {
/** /**
* Return if the cell is playable. * Return if the cell is playable.
* * @return True if the current cell can be played with the condition of the gomoku.
* @return True if the current cell can be played with the condition of the
* gomoku.
*/ */
public boolean isPlayable(){ public boolean isPlayable(){
if (this.state != Color.NIL)
return false;
for (Cardinal c : Cardinal.values()) { for (Cardinal c : Cardinal.values()) {
GomokuCell current = this.getNeighbour(c); GomokuCell current = this.getNeighbour(c);
if (current != null && current.isPlayed()) if (current != null && current.isPlayed()) return true;
return true;
} }
return false; return false;
} }
//------------------Methods-------------------------- //------------------Methods--------------------------
/** /**
* This method link the cell at the Cardinal position on the current cell. * This method link the cell at the Cardinal position on the current cell.
*
* @param car The Cardinal direction of the cell to link. * @param car The Cardinal direction of the cell to link.
* @param cell The GomokuCell to link. * @param cell The GomokuCell to link.
*/ */
@@ -148,11 +131,11 @@ public class GomokuCell {
this.neighbour.put(car, cell); this.neighbour.put(car, cell);
} }
//------------------Statics-------------------------- //------------------Statics--------------------------
public static void link(GomokuCell c1, GomokuCell c2, Cardinal c1Toc2){ public static void link(GomokuCell c1, GomokuCell c2, Cardinal c1Toc2){
if (c1 == null || c2 == null) if (c1 == null || c2 == null) return ;
return;
c1.linkCell(c1Toc2, c2); c1.linkCell(c1Toc2, c2);
c2.linkCell(c1Toc2.inverse(), c1); c2.linkCell(c1Toc2.inverse(), c1);
} }
@@ -164,9 +147,9 @@ public class GomokuCell {
case Color.NIL: case Color.NIL:
return "."; return ".";
case Color.BLACK: case Color.BLACK:
return "\u274C"; return "X";
case Color.WHITE: case Color.WHITE:
return "\uD83D"; return "O";
default: default:
throw new IllegalStateException("Unexpected value: " + this.getState()); throw new IllegalStateException("Unexpected value: " + this.getState());
} }

View File

@@ -84,11 +84,13 @@ public class GomokuGame {
} }
} }
GomokuGame g = new GomokuGame(false);// metre true ou fals si in veut l'affichage ou non GomokuGame g = new GomokuGame(false);// metre true ou fals si in veut l'affichage ou non
g.renderer = new ConsoleRenderer();
g.renderer.init(g);
g.board = new GomokuBoard(sizeX, sizeY); g.board = new GomokuBoard(sizeX, sizeY);
g.board.get(sizeX / 2, sizeY / 2).setState(Color.BLACK); g.board.get(sizeX / 2, sizeY / 2).setState(Color.BLACK);
g.player1 = new Human("un", Color.WHITE, nbToken); g.player1 = new Human("un", Color.WHITE, nbToken);
g.player2 = new GomokuAI("deux", Color.BLACK, nbToken - 1); g.player2 = new GomokuAI("deux", Color.BLACK, nbToken - 1);
System.out.println(g.board); //System.out.println(g.board);
// g.renderer.update(); // g.renderer.update();
g.startGame(); g.startGame();
@@ -139,6 +141,7 @@ public class GomokuGame {
* } * }
*/ */
this.currentPlayer = player1; this.currentPlayer = player1;
renderer.update();
while (this.player1.tokens > 0 || this.player2.tokens > 0) { while (this.player1.tokens > 0 || this.player2.tokens > 0) {
GomokuCell currentPlay = null; GomokuCell currentPlay = null;
while (currentPlay == null) { while (currentPlay == null) {
@@ -151,7 +154,8 @@ public class GomokuGame {
} }
this.currentPlayer.tokens -= 1; this.currentPlayer.tokens -= 1;
this.currentPlayer = this.nextPlayer(); this.currentPlayer = this.nextPlayer();
System.out.println(this.board); 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 jeton.");