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

@@ -13,6 +13,10 @@ public class ConsoleRenderer extends GomokuRenderer {
@Override @Override
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();
@@ -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

@@ -5,61 +5,51 @@ 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 private EnumMap<Cardinal,GomokuCell> neighbour;
* cell in that direction. /** 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 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
*/
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){
this.neighbour = new EnumMap<>(Cardinal.class); this.neighbour = new EnumMap<>(Cardinal.class);
this.state = state; this.state = state;
} }
// ------------------Gets-------------------------- //------------------Gets--------------------------
/** /**
* 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.
*/ */
public GomokuCell getNeighbour(Cardinal car) { public GomokuCell getNeighbour(Cardinal car){
return this.neighbour.get(car); return this.neighbour.get(car);
} }
/** /**
* Return neighbours. * Return neighbours.
*
* @return The EnumMap of neighbours. * @return The EnumMap of neighbours.
*/ */
public EnumMap<Cardinal, GomokuCell> getAllNeighbour() { public EnumMap<Cardinal, GomokuCell> getAllNeighbour(){
return neighbour; return neighbour;
} }
/** /**
* 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() {
Map<Cardinal, Integer> map = new HashMap<>(); Map<Cardinal, Integer> map = new HashMap<>();
map.put(Cardinal.N, 0); map.put(Cardinal.N, 0);
map.put(Cardinal.W, 0); map.put(Cardinal.W,0);
map.put(Cardinal.NW, 0); map.put(Cardinal.NW, 0);
map.put(Cardinal.SW, 0); map.put(Cardinal.SW, 0);
map.put(Cardinal.S, 0); map.put(Cardinal.S, 0);
@@ -70,103 +60,96 @@ 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); {
actualcell = actualcell.getNeighbour(entry.getKey()); entry.setValue(entry.getValue()+1);
actualcell=actualcell.getNeighbour(entry.getKey());
} }
} }
return map; return map;
} }
/** /**
* 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(){
return this.state; return this.state;
} }
// ------------------Sets-------------------------- //------------------Sets--------------------------
/** /**
* 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.
*/ */
public void setState(Color c) { public void setState(Color c){
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(){
return this.state == Color.NIL; return this.state == Color.NIL;
} }
/** /**
* 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(){
return !this.isEmpty(); return !this.isEmpty();
} }
/** /**
* 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.
*/ */
public void linkCell(Cardinal car, GomokuCell cell) { public void linkCell(Cardinal car, GomokuCell cell){
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);
} }
// ------------------Overides-------------------------- //------------------Overides--------------------------
@Override @Override
public String toString() { public String toString() {
switch (this.getState()) { switch (this.getState()) {
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.");