This commit is contained in:
Cyprien111
2025-04-07 10:39:13 +02:00
84 changed files with 191 additions and 8925 deletions

View File

@@ -45,4 +45,10 @@ public class ConsoleRenderer extends GomokuRenderer {
sb.append("+");
return sb.toString();
}
@Override
public void updateStatus(String status) {
// Print the status to the console
System.out.println("Status: " + status);
}
}

View File

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

View File

@@ -129,6 +129,8 @@ public class GomokuBoard{
* @return GomokuCell in the position.
*/
public GomokuCell get(int x, int y){
if (x < 0 || x >= this.boardWidth) return null;
if (y < 0 || y >= this.boardHeight) return null;
int i = 0, j = 0;
GomokuCell act = this.firstCell;
while (i != x || j != y){

View File

@@ -15,14 +15,17 @@ public class GomokuGame {
private Player player1;
private Player player2;
private Player currentPlayer;
private GomokuBoard board;
private GomokuRenderer renderer;
private boolean playRenderer= true;
Color colorP1;
int currP;
int nbTokens1, nbTokens2;
int currentPlayerInt;
Coordinate cellCoor = null;
public static void main(String[] args) {
@@ -83,37 +86,52 @@ public class GomokuGame {
}
public void newGame(boolean bot, String name1, String name2) {
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);
this.player1 = new Human(name1, colorPlayer1, tokens);
if (bot) {
this.player2 = new GomokuAI(name2, colorPlayer2);
this.player2 = new GomokuAI(name2, colorPlayer2, tokens);
} else {
this.player2 = new Human(name2, colorPlayer2);
this.player2 = new Human(name2, colorPlayer2, tokens);
}
this.board = new GomokuBoard(15, 15);
this.colorP1 = colorPlayer1;
this.currP = this.player1.color == Color.WHITE ? 1 : 2;
this.nbTokens1 = 60;
this.nbTokens2 = 60;
currentPlayer = this.player1.color == Color.WHITE ? this.player1: this.player2;
this.currentPlayerInt = this.player1.color == Color.WHITE ? 1 : 2;
this.renderer = new SwingRenderer();
}
public void startGame() {
/*
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 !");
}
*/
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)))
{
this.renderer.updateStatus("Le joueur " + this.currentPlayer + "a gagné !");
return;
}
this.currentPlayer = this.nextPlayer();
}
this.renderer.updateStatus("Match nul, il ne reste plus de jeton.");
return;
}
/**
@@ -121,10 +139,30 @@ public class GomokuGame {
* @param Player get player to play the cell.
*/
public GomokuCell play(Player player) {
GomokuCell cellToPlay = player.chooseMove(board);
cellToPlay.setState(player.color);
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.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);
this.cellCoor = null;
}
else{
cellToPlay = player.chooseMove(board);
cellToPlay.setState(player.color);
}
return cellToPlay;
}
@@ -133,7 +171,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 currentPlayerInt nbTokens1 nbTokens2
// 3. Write the next h lines with the board
// 4. Close the file
// 5. Return true if successful, false otherwise
@@ -149,21 +187,21 @@ public class GomokuGame {
this.getBoard().getWidth(),
this.getBoard().getHeight(),
colorP1,
currP,
nbTokens1,
nbTokens2
currentPlayerInt,
this.player1.tokens,
this.player2.tokens
));
// Write the board
for (int i = 0; i < this.getBoard().getHeight(); ++i) {
for (int j = 0; j < this.getBoard().getWidth(); ++j) {
char c = ' ';
char c;
switch (board.get(i, j).getState()) {
case BLACK:
c = 'X';
break;
case WHITE:
c = 'O';
break;
case NIL:
c = '.';
@@ -191,7 +229,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 currentPlayerInt nbTokens1 nbTokens2
// 3. Read the next h lines to get the board
// 4. Close the file
// 5. Initialize the board with the read values
@@ -212,9 +250,9 @@ public class GomokuGame {
w = Integer.parseInt(parts[0]);
h = Integer.parseInt(parts[1]);
colorP1 = Color.valueOf(parts[2]);
currP = Integer.parseInt(parts[3]);
nbTokens1 = Integer.parseInt(parts[4]);
nbTokens2 = Integer.parseInt(parts[5]);
currentPlayerInt = Integer.parseInt(parts[3]);
this.player1.tokens = Integer.parseInt(parts[4]);
this.player2.tokens = Integer.parseInt(parts[5]);
if (w <= 0 || h <= 0) {
throw new IllegalArgumentException("Invalid board dimensions");
@@ -269,6 +307,13 @@ public class GomokuGame {
return true;
}
private Player nextPlayer(){
if (this.currentPlayer == this.player1){
return this.player2;
}
return player1;
}
public GomokuBoard getBoard() {
return this.board;
}

View File

@@ -6,4 +6,5 @@ public abstract class GomokuRenderer {
public abstract void init(GomokuGame game);
public abstract void update();
public abstract void updateStatus(String status);
}

View File

@@ -11,8 +11,8 @@ public class Human extends Player {
* @param name The name of the player.
* @param color The color of the player.
*/
public Human(String name, Color color) {
super(name, color);
public Human(String name, Color color, int tokens) {
super(name, color, tokens);
}
/**

View File

@@ -13,6 +13,8 @@ abstract class Player{
/** The color of the Player */
protected Color color;
int tokens;
//------------------Constructors--------------------------
@@ -21,9 +23,10 @@ abstract class Player{
* @param name The name of the player.
* @param color The color of the player.
*/
public Player(String name,Color color){
public Player(String name,Color color, int tokens){
this.name = name;
this.color = color;
this.tokens= tokens;
}
//------------------Gets--------------------------

View File

@@ -9,7 +9,6 @@ class RenderBoard {
public int renderHeight;
private int cellSize;
private int borderThickness;
// private int[][] boardState;
public RenderBoard(int width, int height, int renderWidth, int renderHeight) {
this.boardWidth = width;
@@ -74,7 +73,9 @@ class RenderBoard {
this.cellSize - 2 * this.borderThickness
);
// Draw the cell
this.drawToken(g, x, y, Color.BLUE);
if (i == boardWidth / 2 && j == boardHeight / 2) {
this.drawToken(g, cx, cy, Color.BLUE);
}
cx += this.cellSize;
}
cy += this.cellSize;
@@ -85,8 +86,8 @@ class RenderBoard {
// Draw the token at the specified position
g.setColor(color);
g.fillRect(
x + 2 * this.borderThickness,
y + 2 * this.borderThickness,
x + borderThickness,
y + borderThickness,
cellSize - 2 * borderThickness,
cellSize - 2 * borderThickness
);

View File

@@ -4,8 +4,11 @@ import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class SwingRenderer extends GomokuRenderer {
@@ -22,17 +25,38 @@ public class SwingRenderer extends GomokuRenderer {
@Override
public void init(GomokuGame game) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
mainFrame = new JFrame("Gomoku! - Projet L2");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(960, 960);
canvas = new RenderCanvas();
mainFrame.add(canvas);
mainFrame.pack();
mainFrame.setVisible(true);
}
});
mainFrame = new JFrame("Gomoku! - Projet L2");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(960, 960);
mainFrame.setResizable(false);
canvas = new RenderCanvas();
mainFrame.add(canvas);
mainFrame.pack();
mainFrame.setVisible(true);
mainFrame.addMouseListener(canvas);
// add a label component for the title
JLabel titleLabel = new JLabel("Gomoku Game!", JLabel.CENTER);
titleLabel.setVerticalAlignment(JLabel.CENTER);
titleLabel.setBackground(Color.WHITE);
titleLabel.setOpaque(true);
titleLabel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
// set the font size and color
titleLabel.setFont(titleLabel.getFont().deriveFont(24f));
mainFrame.add(titleLabel, "North");
mainFrame.pack();
// add a bottom label for the status
JLabel statusLabel = new JLabel("Status: Waiting for player...", JLabel.CENTER);
statusLabel.setVerticalAlignment(JLabel.CENTER);
statusLabel.setBackground(Color.WHITE);
statusLabel.setOpaque(true);
statusLabel.setBorder(BorderFactory.createEmptyBorder());
// set the font size and color
statusLabel.setFont(statusLabel.getFont().deriveFont(18f));
mainFrame.add(statusLabel, "South");
mainFrame.pack();
}
@Override
@@ -41,14 +65,21 @@ public class SwingRenderer extends GomokuRenderer {
// This method should be called whenever the game state changes
canvas.draw(game);
}
@Override
public void updateStatus(String status) {
// Update the status label
JLabel statusLabel = (JLabel) mainFrame.getContentPane().getComponent(1);
statusLabel.setText("Status: " + status);
}
}
class RenderCanvas extends JPanel {
class RenderCanvas extends JPanel implements MouseListener {
public RenderBoard board;
public RenderCanvas() {
setBorder(BorderFactory.createLineBorder(Color.black));
setBorder(BorderFactory.createEmptyBorder());
setBackground(Color.white);
board = new RenderBoard(15, 15, 600, 600);
}
@@ -72,4 +103,26 @@ class RenderCanvas extends JPanel {
board.update(game);
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
// Handle mouse click events
int x = e.getX();
int y = e.getY();
System.out.println("Mouse clicked at: " + x + ", " + y);
}
@Override
public void mousePressed(MouseEvent e) {
// Handle mouse press events
int x = e.getX();
int y = e.getY();
System.out.println("Mouse pressed at: " + x + ", " + y);
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}