test the game

This commit is contained in:
Aubin DORIVAL
2025-04-14 09:02:20 +02:00
parent 0cd528134d
commit fce5aa1b33
3 changed files with 211 additions and 158 deletions

View File

@@ -20,30 +20,34 @@ public class GomokuGame {
private GomokuBoard board;
private GomokuRenderer renderer;
private boolean playRenderer= true;
private boolean playRenderer = true;
Color colorP1;
int currentPlayerInt;
Coordinate cellCoor = null;
GomokuGame(boolean renderer){
GomokuGame(boolean renderer) {
this.playRenderer = renderer;
this.board = new GomokuBoard(15, 15);
this.board.get(7, 7).setState(Color.BLACK);
this.player1 = new Human("un", Color.WHITE, 60);
this.player2 = new GomokuAI("deux", Color.BLACK, 60);
}
public static void main(String[] args) {
int sizeX = 0;
int sizeY = 0;
int nbToken = 0;
int nbJetonsAligne = 0;
boolean renderer = false;
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "--save":
if (i + 1 < args.length) {
//load(g.load(Path.of(".cache/test.dat")));
// load(g.load(Path.of(".cache/test.dat")));
}
break;
@@ -69,11 +73,10 @@ public class GomokuGame {
case "--renderer":
if (i + 1 < args.length) {
String bool = args[++i];
if(bool=="true"){
if (bool == "true") {
renderer = true;
}
else {
renderer=false;
} else {
renderer = false;
}
}
break;
@@ -83,22 +86,21 @@ public class GomokuGame {
break;
}
}
// Test
GomokuGame g = new GomokuGame(renderer);//metre true ou fals si in veut l'affichage ou non
g.renderer = new ConsoleRenderer();
g.renderer.update();
g.board.expandBoard(Cardinal.SE);
g.renderer.update();
// Test
GomokuGame g = new GomokuGame(false);// metre true ou fals si in veut l'affichage ou non
System.out.println(g.board);
// g.renderer.update();
g.startGame();
}
/**
* This method init the game with these parameters.
* @param bot If the player want to play with a bot it's true.
* @param name1 Name of player one.
* @param name2 Name of player two.
* This method init the game with these parameters.
*
* @param bot If the player want to play with a bot it's true.
* @param name1 Name of player one.
* @param name2 Name of player two.
* @param tokens Number of tokens for each player.
*/
@@ -119,7 +121,7 @@ public class GomokuGame {
this.board = new GomokuBoard(15, 15);
this.colorP1 = colorPlayer1;
currentPlayer = this.player1.color == Color.WHITE ? this.player1: this.player2;
currentPlayer = this.player1.color == Color.WHITE ? this.player1 : this.player2;
this.currentPlayerInt = this.player1.color == Color.WHITE ? 1 : 2;
this.renderer = new SwingRenderer();
@@ -130,64 +132,66 @@ public class GomokuGame {
*/
public void startGame() {
/*
GomokuCell actualPlayedCell = play(player1);
if( NB_CELL_PLAY <= board.countMax(board.countAlignedCells(actualPlayedCell)))
{
System.out.println("c'est gangée !");
}
*/
while(this.player1.tokens > 0 || this.player2.tokens > 0){
* GomokuCell actualPlayedCell = play(player1);
* if( NB_CELL_PLAY <=
* board.countMax(board.countAlignedCells(actualPlayedCell)))
* {
* System.out.println("c'est gangée !");
* }
*/
this.currentPlayer = player1;
while (this.player1.tokens > 0 || this.player2.tokens > 0) {
GomokuCell currentPlay = null;
while (currentPlay == null) {
currentPlay = this.play(this.currentPlayer);
}
if( NB_CELL_PLAY <= board.countMax(board.countAlignedCells(currentPlay)))
{
if (NB_CELL_PLAY <= board.countMax(board.countAlignedCells(currentPlay))) {
this.renderer.updateStatus("Le joueur " + this.currentPlayer + "a gagné !");
return;
}
this.currentPlayer.tokens -= 1;
this.currentPlayer = this.nextPlayer();
System.out.println(this.board);
}
this.renderer.updateStatus("Match nul, il ne reste plus de jeton.");
return;
}
/**
* Place the token on the cell where the player play.
* @param Player get player to play the cell.
*/
* Place the token on the cell where the player play.
*
* @param Player get player to play the cell.
*/
public GomokuCell play(Player player) {
GomokuCell cellToPlay = null;
if (this.playRenderer){ // If we play the game with the renderer.
if (this.playRenderer) { // If we play the game with the renderer.
while (this.cellCoor == null) {
try{
wait(16);
}
catch(InterruptedException e){
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 == null || !cellToPlay.isPlayable()) { // If the cell is not playable we return null to not play.
cellToPlay = this.board.get(this.cellCoor);
if (cellToPlay == null || !cellToPlay.isPlayable()) { // If the cell is not playable we return null to not
// play.
return null;
}
cellToPlay.setState(player.color);
this.cellCoor = null;
}
else{
cellToPlay = player.chooseMove(board);
} else {
cellToPlay = player.chooseMove(this.board);
cellToPlay.setState(player.color);
}
return cellToPlay;
}
/**
* This method save the game on a file.
* This method save the game on a file.
*
* @param filename The file to save.
* @return True if successful and false if not.
*/
@@ -213,8 +217,7 @@ public class GomokuGame {
colorP1,
currentPlayerInt,
this.player1.tokens,
this.player2.tokens
));
this.player2.tokens));
// Write the board
for (int i = 0; i < this.getBoard().getHeight(); ++i) {
@@ -250,7 +253,8 @@ public class GomokuGame {
}
/**
* This method load a game on the file.
* This method load a game on the file.
*
* @param filename The file to load.
* @return True if successful and False if not.
*/
@@ -337,11 +341,12 @@ public class GomokuGame {
}
/**
* This method return the next player to play.
* This method return the next player to play.
*
* @return The next player.
*/
private Player nextPlayer(){
if (this.currentPlayer == this.player1){
private Player nextPlayer() {
if (this.currentPlayer == this.player1) {
return this.player2;
}
return player1;