This commit is contained in:
Aubin DORIVAL
2025-04-07 09:54:14 +02:00
parent 853ddaf47a
commit 871c6dbc49

View File

@@ -15,15 +15,19 @@ public class GomokuGame {
private Player player1; private Player player1;
private Player player2; private Player player2;
private Player currentPlayer;
private GomokuBoard board; private GomokuBoard board;
private GomokuRenderer renderer; private GomokuRenderer renderer;
private boolean playRenderer= true;
Color colorP1; Color colorP1;
int currP; int currentPlayerInt;
int nbTokens1, nbTokens2; int nbTokens1, nbTokens2;
Coordinate cellCoor = null;
public static void main(String[] args) { public static void main(String[] args) {
// Test // Test
GomokuGame g = new GomokuGame(); GomokuGame g = new GomokuGame();
@@ -52,16 +56,28 @@ public class GomokuGame {
this.board = new GomokuBoard(15, 15); this.board = new GomokuBoard(15, 15);
this.colorP1 = colorPlayer1; this.colorP1 = colorPlayer1;
this.currP = this.player1.color == Color.WHITE ? 1 : 2; currentPlayer = this.player1.color == Color.WHITE ? this.player1: this.player2;
this.currentPlayerInt = this.player1.color == Color.WHITE ? 1 : 2;
this.nbTokens1 = 60; this.nbTokens1 = 60;
this.nbTokens2 = 60; this.nbTokens2 = 60;
} }
public void startGame() { public void startGame() {
/*
GomokuCell actualPlayedCell = play(player1); GomokuCell actualPlayedCell = play(player1);
if( NB_CELL_PLAY >= board.countMax(board.countAlignedCells(actualPlayedCell))) if( NB_CELL_PLAY <= board.countMax(board.countAlignedCells(actualPlayedCell)))
{
System.out.println("c'est gangée !");
}
*/
GomokuCell currentPlay = null;
while (currentPlay == null) {
currentPlay = this.play(this.currentPlayer);
}
this.currentPlayer = this.nextPlayer();
if( NB_CELL_PLAY <= board.countMax(board.countAlignedCells(currentPlay)))
{ {
System.out.println("c'est gangée !"); System.out.println("c'est gangée !");
} }
@@ -73,10 +89,30 @@ public class GomokuGame {
* @param Player get player to play the cell. * @param Player get player to play the cell.
*/ */
public GomokuCell play(Player player) { public GomokuCell play(Player player) {
GomokuCell cellToPlay = null;
GomokuCell cellToPlay = player.chooseMove(board); if (this.playRenderer){ // If we play the game with the renderer.
while (this.cellCoor == null) {
try{
wait(16);
}
catch(InterruptedException e){
this.save(null);
System.out.println(e);
}
}
cellToPlay= this.board.get(this.cellCoor.x, this.cellCoor.y);
if (!cellToPlay.isPlayable()) { // If the cell is not playable we return null to not play.
return null;
}
cellToPlay.setState(player.color); cellToPlay.setState(player.color);
this.cellCoor = null;
}
else{
cellToPlay = player.chooseMove(board);
cellToPlay.setState(player.color);
}
return cellToPlay; return cellToPlay;
} }
@@ -85,7 +121,7 @@ public class GomokuGame {
// save the game state to the file // save the game state to the file
// 1. Open the file // 1. Open the file
// 2. Write the first line with: // 2. Write the first line with:
// w h colorP1 currP nbTokens1 nbTokens2 // w h colorP1 currentPlayerInt nbTokens1 nbTokens2
// 3. Write the next h lines with the board // 3. Write the next h lines with the board
// 4. Close the file // 4. Close the file
// 5. Return true if successful, false otherwise // 5. Return true if successful, false otherwise
@@ -101,7 +137,7 @@ public class GomokuGame {
this.getBoard().getWidth(), this.getBoard().getWidth(),
this.getBoard().getHeight(), this.getBoard().getHeight(),
colorP1, colorP1,
currP, currentPlayerInt,
nbTokens1, nbTokens1,
nbTokens2 nbTokens2
)); ));
@@ -143,7 +179,7 @@ public class GomokuGame {
// load the game state from the file // load the game state from the file
// 1. Open the file // 1. Open the file
// 2. Read the first line to get: // 2. Read the first line to get:
// w h colorP1 currP nbTokens1 nbTokens2 // w h colorP1 currentPlayerInt nbTokens1 nbTokens2
// 3. Read the next h lines to get the board // 3. Read the next h lines to get the board
// 4. Close the file // 4. Close the file
// 5. Initialize the board with the read values // 5. Initialize the board with the read values
@@ -164,7 +200,7 @@ public class GomokuGame {
w = Integer.parseInt(parts[0]); w = Integer.parseInt(parts[0]);
h = Integer.parseInt(parts[1]); h = Integer.parseInt(parts[1]);
colorP1 = Color.valueOf(parts[2]); colorP1 = Color.valueOf(parts[2]);
currP = Integer.parseInt(parts[3]); currentPlayerInt = Integer.parseInt(parts[3]);
nbTokens1 = Integer.parseInt(parts[4]); nbTokens1 = Integer.parseInt(parts[4]);
nbTokens2 = Integer.parseInt(parts[5]); nbTokens2 = Integer.parseInt(parts[5]);
@@ -221,6 +257,13 @@ public class GomokuGame {
return true; return true;
} }
private Player nextPlayer(){
if (this.currentPlayer == this.player1){
return this.player2;
}
return player1;
}
public GomokuBoard getBoard() { public GomokuBoard getBoard() {
return this.board; return this.board;
} }