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

This commit is contained in:
Dorian HAMDANI
2025-04-07 08:58:32 +02:00
5 changed files with 159 additions and 84 deletions

View File

@@ -1,12 +1,12 @@
import java.nio.file.Path;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Random;
import java.io.BufferedWriter;
public class GomokuGame {
public static final int DEFAULT_BOARD_WIDTH = 15; // largeur du plateau
public static final int DEFAULT_BOARD_WIDTH = 15; // largeur du plateau
public static final int DEFAULT_BOARD_HEIGHT = 15; // hauteur du plateau
public static final int DEFAULT_TOKENS_COUNT = 60; // nb de jetons
@@ -32,7 +32,26 @@ public class GomokuGame {
g.renderer.update();
}
public void newGame() {
public void newGame(boolean bot, String name1, String name2) {
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);
if (bot) {
this.player2 = new GomokuAI(name2, colorPlayer2);
} else {
this.player2 = new Human(name2, colorPlayer2);
}
this.board = new GomokuBoard(15, 15);
this.colorP1 = colorPlayer1;
this.currP = this.player1.color == Color.WHITE ? 1: 2;
this.nbTokens1 = 60;
this.nbTokens2 = 60;
}
@@ -44,7 +63,7 @@ public class GomokuGame {
// save the game state to the file
// 1. Open the file
// 2. Write the first line with:
// w h colorP1 currP nbTokens1 nbTokens2
// w h colorP1 currP nbTokens1 nbTokens2
// 3. Write the next h lines with the board
// 4. Close the file
// 5. Return true if successful, false otherwise
@@ -70,12 +89,19 @@ public class GomokuGame {
for (int j = 0; j < this.getBoard().getWidth(); ++j) {
char c;
switch (board.get(i, j).getState()) {
case BLACK: c = 'X'; break;
case WHITE: c = 'O'; break;
case NIL: c = '.'; break;
default: throw new IllegalStateException(
String.format("Unexpected value at cell (%d, %d): %s", i, j, board.get(i, j).getState())
);
case BLACK:
c = 'X';
break;
case WHITE:
break;
case NIL:
c = '.';
break;
default:
throw new IllegalStateException(
String.format("Unexpected value at cell (%d, %d): %s", i, j,
board.get(i, j).getState()));
}
writer.write(c);
}
@@ -95,7 +121,7 @@ public class GomokuGame {
// load the game state from the file
// 1. Open the file
// 2. Read the first line to get:
// w h colorP1 currP nbTokens1 nbTokens2
// w h colorP1 currP nbTokens1 nbTokens2
// 3. Read the next h lines to get the board
// 4. Close the file
// 5. Initialize the board with the read values
@@ -138,9 +164,15 @@ public class GomokuGame {
for (int j = 0; j < w; ++j) {
switch (line.charAt(j)) {
case 'X': colors[i][j] = Color.BLACK; break;
case 'O': colors[i][j] = Color.WHITE; break;
case '.': colors[i][j] = Color.NIL; break;
case 'X':
colors[i][j] = Color.BLACK;
break;
case 'O':
colors[i][j] = Color.WHITE;
break;
case '.':
colors[i][j] = Color.NIL;
break;
default:
throw new IllegalArgumentException("Invalid color: " + line.charAt(j));
}