doc and remove unused file dans method

This commit is contained in:
2025-05-02 17:50:32 +02:00
parent 6a5c6a4917
commit 88ff6675b1
7 changed files with 32 additions and 284 deletions

View File

@@ -1,22 +1,7 @@
# Gomoku
# Saving
## Legend
```
x,y -> coordonnée
N -> NIL
W -> White
B -> Black
C -> Color
T -> current player (1 or 2)
```
## Format
```
x,y,C,T
NNNNNNNNN...
NNNBBBWWW...
............
## how to build and run
```bash
javac -d build src/*java
java -cp src/GomokuGame
```

View File

@@ -1,7 +1,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.List;
public class GomokuAI extends Player {
@@ -9,6 +9,7 @@ public class GomokuAI extends Player {
/**
* The constructor of the GomokuAI.
*
* @param name The name of the player.
* @param color The color of the player.
*/
@@ -59,8 +60,7 @@ public class GomokuAI extends Player {
Color other = null;
if (this.color == Color.WHITE) {
other = Color.BLACK;
}
else{
} else {
other = Color.WHITE;
}
Map<GomokuCell, Integer> map3 = GetCellPoint(board, other);
@@ -81,7 +81,6 @@ public class GomokuAI extends Player {
}
}
break;
default:
throw new AssertionError();
@@ -124,11 +123,23 @@ public class GomokuAI extends Player {
return map;
}
// ------------------Getter--------------------------
/**
* Return the difficulty of the AI.
*
* @return 0: easy, 1: medium, 2: hard
*/
public int GetDifficulty() {
return difficulty;
}
// ------------------Setter--------------------------
/**
* Change Difficulty of the AI.
*
* @param difficulty 0: easy, 1: medium, 2: hard
*/
public void SetDifficulty(int difficulty) {
this.difficulty = difficulty;
}

View File

@@ -1,7 +1,7 @@
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.EnumMap;
import java.util.ArrayList;
/**
* The board of the game Gomoku.
@@ -14,32 +14,6 @@ public class GomokuBoard {
/** The height of the GomokuBoard. */
private int boardHeight;
public static void main(String[] args) {
Color[][] colors = { { Color.BLACK, Color.BLACK, Color.BLACK },
{ Color.BLACK, Color.BLACK, Color.BLACK },
{ Color.WHITE, Color.WHITE, Color.WHITE } };
GomokuBoard test = new GomokuBoard(3, 3, colors);
test.get(1, 1).setState(Color.BLACK);
System.out.println(test);
System.out.println(test.getPlayableCells());
System.out.println(Cardinal.NE.rotate90CW());
System.out.println(Cardinal.NE.rotate90CCW());
System.out.println(Cardinal.NE.rotate(1));
System.out.println(Cardinal.NE.rotate(-1));
System.out.println(Cardinal.NE.inverse());
System.out.println(test);
test.expandBoard(Cardinal.N);
System.out.println(test);
test.expandBoard(Cardinal.S);
System.out.println(test);
test.expandBoard(Cardinal.W);
System.out.println(test);
test.expandBoard(Cardinal.SE);
System.out.println(test);
}
// ------------------Constructors--------------------------
/**

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.BufferedReader;
public class GomokuGame {
@@ -129,7 +129,6 @@ public class GomokuGame {
this.colorP1 = colorPlayer1;
currentPlayer = this.player1.color == Color.WHITE ? this.player1 : this.player2;
this.currentPlayerInt = this.player1.color == Color.WHITE ? 1 : 2;
this.renderer = new SwingRenderer();
}

View File

@@ -1,10 +1,11 @@
public abstract class GomokuRenderer {
protected GomokuGame game;
public abstract void init(GomokuGame game);
public abstract void update();
public abstract void updateStatus(String status);
}

View File

@@ -1,95 +0,0 @@
import java.awt.Color;
import java.awt.Graphics;
class RenderBoard {
private int boardWidth;
private int boardHeight;
public int renderWidth;
public int renderHeight;
private int cellSize;
private int borderThickness;
public RenderBoard(int width, int height, int renderWidth, int renderHeight) {
this.boardWidth = width;
this.boardHeight = height;
this.renderWidth = renderWidth;
this.renderHeight = renderHeight;
this.borderThickness = 5;
int cellWidth = renderWidth / width;
int cellHeight = renderHeight / height;
this.cellSize = Math.min(cellWidth, cellHeight);
}
public RenderBoard(int renderWidth, int renderHeight) {
this(
GomokuGame.DEFAULT_BOARD_WIDTH,
GomokuGame.DEFAULT_BOARD_HEIGHT,
renderWidth,
renderHeight
);
}
public void update(GomokuGame game) {
// Update the board state based on the game
// This method should be called whenever the game state changes
}
public void drawBoard(Graphics g, int x, int y) {
// Draw the global border
g.setColor(Color.LIGHT_GRAY);
g.fillRect(
x,
y,
this.renderWidth + 2 * this.borderThickness,
this.renderHeight + 2 * this.borderThickness
);
g.setColor(Color.WHITE);
g.fillRect(
x + this.borderThickness,
y + this.borderThickness,
this.renderWidth,
this.renderHeight
);
int i, j;
int cy = y + this.borderThickness;
for (i = 0; i < this.boardHeight; i++) {
int cx = x + this.borderThickness;
for (j = 0; j < this.boardWidth; j++) {
// Draw the border
g.setColor(Color.LIGHT_GRAY);
g.fillRect(
cx,
cy,
this.cellSize,
this.cellSize
);
g.setColor(Color.WHITE);
g.fillRect(
cx + this.borderThickness,
cy + this.borderThickness,
this.cellSize - 2 * this.borderThickness,
this.cellSize - 2 * this.borderThickness
);
// Draw the cell
if (i == boardWidth / 2 && j == boardHeight / 2) {
this.drawToken(g, cx, cy, Color.BLUE);
}
cx += this.cellSize;
}
cy += this.cellSize;
}
}
public void drawToken(Graphics g, int x, int y, Color color) {
// Draw the token at the specified position
g.setColor(color);
g.fillRect(
x + borderThickness,
y + borderThickness,
cellSize - 2 * borderThickness,
cellSize - 2 * borderThickness
);
}
}

View File

@@ -1,127 +0,0 @@
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 {
private JFrame mainFrame;
private GomokuGame game;
private RenderCanvas canvas;
public SwingRenderer() {}
public static void main(String[] args) {
SwingRenderer s = new SwingRenderer();
s.init(null);
}
@Override
public void init(GomokuGame game) {
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, 0, 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
public void update() {
// Update the game state and repaint the canvas
// 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 implements MouseListener {
public RenderBoard board;
public RenderCanvas() {
setBorder(BorderFactory.createEmptyBorder());
setBackground(Color.white);
board = new RenderBoard(15, 15, 600, 600);
}
public Dimension getPreferredSize() {
return new Dimension(700, 700);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int midX = getWidth() / 2;
int midY = getHeight() / 2;
int x = midX - (this.board.renderWidth / 2);
int y = midY - (this.board.renderHeight / 2);
board.drawBoard(g, x, y);
}
public void draw(GomokuGame game) {
// Update the game state and repaint the canvas
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) {}
}