This commit is contained in:
Aubin DORIVAL
2025-04-07 09:08:43 +02:00
6 changed files with 203 additions and 162 deletions

View File

@@ -1,10 +1,10 @@
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.IOException;
import java.io.BufferedReader;
public class GomokuGame {
@@ -15,10 +15,10 @@ public class GomokuGame {
private Player player1;
private Player player2;
private int boardWidth;
private int boardHeight;
private GomokuBoard board;
private GomokuRenderer renderer;
Color colorP1;
int currP;
int nbTokens1, nbTokens2;
@@ -27,15 +27,11 @@ public class GomokuGame {
// Test
GomokuGame g = new GomokuGame();
System.out.println(g.load(Path.of(".cache/test.dat")));
System.out.println(g.getBoard());
System.out.println(g.save(Path.of(".cache/save.dat")));
for (int i = 0; i < g.boardHeight; ++i) {
for (int j = 0; j < g.boardWidth; ++j) {
System.out.print(g.getBoard().get(i, j));
}
System.out.println();
}
g.renderer = new ConsoleRenderer();
g.renderer.init(g);
g.renderer.update();
g.board.expandBoard(Cardinal.SE);
g.renderer.update();
}
public void newGame(boolean bot, String name1, String name2) {
@@ -64,6 +60,29 @@ public class GomokuGame {
public void startGame() {
}
/**
* Place the token on the cell where the player play.
* @param turn the turn of the player.
*/
public void play(int turn) {
GomokuCell cellToPlay = null;
switch (turn) {
case 1:
cellToPlay = player1.chooseMove(board);
break;
case 2:
cellToPlay = player2.chooseMove(board);
break;
default:
throw new IllegalArgumentException("Invalid turn: " + turn);
}
cellToPlay.setState(colorP1);
}
public boolean save(Path filename) {
// save the game state to the file
// 1. Open the file
@@ -81,12 +100,18 @@ public class GomokuGame {
try (BufferedWriter writer = Files.newBufferedWriter(filename)) {
// Write the first line
writer.write(String.format("%d %d %s %d %d %d%n",
this.boardWidth, this.boardHeight, colorP1, currP, nbTokens1, nbTokens2));
this.getBoard().getWidth(),
this.getBoard().getHeight(),
colorP1,
currP,
nbTokens1,
nbTokens2
));
// Write the board
for (int i = 0; i < boardHeight; ++i) {
for (int j = 0; j < boardWidth; ++j) {
char c;
for (int i = 0; i < this.getBoard().getHeight(); ++i) {
for (int j = 0; j < this.getBoard().getWidth(); ++j) {
char c = ' ';
switch (board.get(i, j).getState()) {
case BLACK:
c = 'X';
@@ -193,8 +218,6 @@ public class GomokuGame {
}
// Initialize the board with the read values
this.boardWidth = w;
this.boardHeight = h;
this.board = new GomokuBoard(w, h, colors);
return true;