Merge branch 'master' of gitlab.isima.fr:audorival/gomoku

This commit is contained in:
Dorian HAMDANI
2025-05-06 16:59:12 +02:00
2 changed files with 69 additions and 81 deletions

View File

@@ -13,8 +13,9 @@ public class GomokuAI extends Player {
* @param name The name of the player. * @param name The name of the player.
* @param color The color of the player. * @param color The color of the player.
*/ */
public GomokuAI(String name, Color color, int tokens) { public GomokuAI(String name, Color color, int tokens, int difficulty) {
super(name, color, tokens); super(name, color, tokens);
this.difficulty = difficulty;
} }
/** /**

View File

@@ -1,10 +1,11 @@
import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Random; import java.util.Random;
import java.io.BufferedReader; import java.util.Random;
public class GomokuGame { public class GomokuGame {
@@ -20,15 +21,36 @@ public class GomokuGame {
private GomokuBoard board; private GomokuBoard board;
private GomokuRenderer renderer; private GomokuRenderer renderer;
private boolean playRenderer = true;
Color colorP1; Color colorP1;
int currentPlayerInt; int currentPlayerInt;
Coordinate cellCoor = null; Coordinate cellCoor = null;
GomokuGame(boolean renderer) { GomokuGame(int nbToken, int jtToWin, int x, int y, boolean pvp, int difficulty) {
this.playRenderer = renderer; Random r = new Random();
this.renderer = new ConsoleRenderer();
this.NB_CELL_PLAY = jtToWin;
this.renderer.init(this);
this.board = new GomokuBoard(x, y);
this.board.get(x / 2, y / 2).setState(Color.BLACK);
if (r.nextInt() % 2 == 0) {
this.player1 = new Human("Premier joueur", Color.WHITE, nbToken);
if (pvp) {
this.player2 = new Human("Deuxieme joueur", Color.WHITE, nbToken);
}
this.player2 = new GomokuAI("deux", Color.BLACK, nbToken - 1, difficulty);
} else {
this.player2 = new Human("Premier joueur", Color.WHITE, nbToken);
if (pvp) {
this.player1 = new Human("Deuxieme joueur", Color.WHITE, nbToken);
}
this.player1 = new GomokuAI("deux", Color.BLACK, nbToken - 1, difficulty);
}
} }
public static void main(String[] args) { public static void main(String[] args) {
@@ -37,7 +59,8 @@ public class GomokuGame {
int sizeY = 15; int sizeY = 15;
int nbToken = 50; int nbToken = 50;
int nbJetonsAligne = 5; int nbJetonsAligne = 5;
boolean renderer = false; int difficulty = 2;
boolean pvp = true;
String path_a_load = ""; String path_a_load = "";
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
@@ -47,33 +70,55 @@ public class GomokuGame {
path_a_load = args[++i]; path_a_load = args[++i];
} }
break; break;
case "--pvp":
pvp = true;
break;
case "--size": case "--size":
if (i + 2 < args.length) { if (i + 2 < args.length) {
try {
sizeX = Integer.parseInt(args[++i]); sizeX = Integer.parseInt(args[++i]);
sizeY = Integer.parseInt(args[++i]); sizeY = Integer.parseInt(args[++i]);
} catch (Exception e) {
System.out.println("Arguments de --size invalides, config par défaut a été appliquée.");
}
} }
break; break;
case "--nbToken": case "--nbToken":
if (i + 1 < args.length) { if (i + 1 < args.length) {
try {
nbToken = Integer.parseInt(args[++i]); nbToken = Integer.parseInt(args[++i]);
} catch (Exception e) {
System.out.println("Argument de --nbToken invalide, config par défaut a été appliquée.");
}
}
break;
case "--difficulty":
if (i + 1 < args.length) {
try {
difficulty = Integer.parseInt(args[++i]);
if (difficulty > 3 || difficulty < 1) {
System.out.println(
"Argument de --difficluty invalide, config par défaut a été appliquée.");
difficulty = 2;
}
} catch (Exception e) {
System.out.println("Argument de --difficluty invalide, config par défaut a été appliquée.");
}
} }
break; break;
case "--nbTokenToWin": case "--nbTokenToWin":
if (i + 1 < args.length) { if (i + 1 < args.length) {
try {
nbJetonsAligne = Integer.parseInt(args[++i]); nbJetonsAligne = Integer.parseInt(args[++i]);
} } catch (Exception e) {
break; System.out
.println("Argument de --nbTokenToWin invalide, config par défaut a été appliquée.");
case "--renderer":
if (i + 1 < args.length) {
String bool = args[++i];
if (bool == "true") {
renderer = true;
} else {
renderer = false;
} }
} }
break; break;
@@ -87,51 +132,11 @@ public class GomokuGame {
System.out.println("Board is too small !"); System.out.println("Board is too small !");
return; return;
} }
GomokuGame g = new GomokuGame(false);// metre true ou fals si in veut l'affichage ou non GomokuGame g = new GomokuGame(nbToken, nbJetonsAligne, sizeX, sizeY, pvp, difficulty);
g.renderer = new ConsoleRenderer();
g.NB_CELL_PLAY = nbJetonsAligne;
g.renderer.init(g);
g.board = new GomokuBoard(sizeX, sizeY);
g.board.get(sizeX / 2, sizeY / 2).setState(Color.BLACK);
g.player1 = new Human("un", Color.WHITE, nbToken);
g.player2 = new GomokuAI("deux", Color.BLACK, nbToken - 1);
// System.out.println(g.board);
// g.renderer.update();
g.startGame(); 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.
* @param tokens Number of tokens for each player.
*/
public void newGame(boolean bot, String name1, String name2, int tokens) {
Color[] possible = { Color.WHITE, Color.BLACK };
int rnd = new Random().nextInt(possible.length);
Color colorPlayer1 = possible[rnd];
Color colorPlayer2 = colorPlayer1.inverse();
this.player1 = new Human(name1, colorPlayer1, tokens);
if (bot) {
this.player2 = new GomokuAI(name2, colorPlayer2, tokens);
} else {
this.player2 = new Human(name2, colorPlayer2, tokens);
}
this.board = new GomokuBoard(15, 15);
this.colorP1 = colorPlayer1;
currentPlayer = this.player1.color == Color.WHITE ? this.player1 : this.player2;
this.currentPlayerInt = this.player1.color == Color.WHITE ? 1 : 2;
}
/** /**
* This method is the main loop of the game * This method is the main loop of the game
*/ */
@@ -179,26 +184,8 @@ public class GomokuGame {
*/ */
public GomokuCell play(Player player) { public GomokuCell play(Player player) {
GomokuCell cellToPlay = null; GomokuCell cellToPlay = null;
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);
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(this.board); cellToPlay = player.chooseMove(this.board);
cellToPlay.setState(player.color); cellToPlay.setState(player.color);
}
return cellToPlay; return cellToPlay;
} }