Merge branch 'master' of gitlab.isima.fr:audorival/gomoku
This commit is contained in:
@@ -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());
|
||||||
|
|
||||||
@@ -29,17 +33,28 @@ public class ConsoleRenderer extends GomokuRenderer {
|
|||||||
System.out.println(horizontalLine);
|
System.out.println(horizontalLine);
|
||||||
for (int i = 0; i < height; i++) {
|
for (int i = 0; i < height; i++) {
|
||||||
System.out.print("|");
|
System.out.print("|");
|
||||||
System.out.print(board[i]);
|
for (int j=0; j<width; j++) {
|
||||||
System.out.println("|");
|
System.out.print(" "+board[i].charAt(j)+" |");
|
||||||
}
|
}
|
||||||
|
System.out.println(" "+i);
|
||||||
System.out.println(horizontalLine);
|
System.out.println(horizontalLine);
|
||||||
|
}
|
||||||
|
System.out.print(" ");
|
||||||
|
for (int i = 0; i < height; i++) {
|
||||||
|
if(i<10) {
|
||||||
|
System.out.print(" "+i+" ");
|
||||||
|
} else {
|
||||||
|
System.out.print(" "+i+" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getHorizontalLine() {
|
private String getHorizontalLine() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("+");
|
sb.append("+");
|
||||||
for (int i = 0; i < game.getBoard().getWidth(); i++) {
|
for (int i = 0; i < game.getBoard().getWidth()*4-1; i++) {
|
||||||
sb.append("-");
|
sb.append("-");
|
||||||
}
|
}
|
||||||
sb.append("+");
|
sb.append("+");
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {
|
||||||
@@ -152,7 +155,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.");
|
||||||
|
|||||||
Reference in New Issue
Block a user