doc and remove unused file dans method
This commit is contained in:
23
README.md
23
README.md
@@ -1,22 +1,7 @@
|
|||||||
# Gomoku
|
# Gomoku
|
||||||
|
|
||||||
|
## how to build and run
|
||||||
# Saving
|
```bash
|
||||||
|
javac -d build src/*java
|
||||||
## Legend
|
java -cp src/GomokuGame
|
||||||
```
|
|
||||||
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...
|
|
||||||
............
|
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class GomokuAI extends Player {
|
public class GomokuAI extends Player {
|
||||||
|
|
||||||
@@ -9,7 +9,8 @@ public class GomokuAI extends Player {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The constructor of the GomokuAI.
|
* The constructor of the GomokuAI.
|
||||||
* @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) {
|
||||||
@@ -37,7 +38,7 @@ public class GomokuAI extends Player {
|
|||||||
public GomokuCell chooseMove(GomokuBoard board) {
|
public GomokuCell chooseMove(GomokuBoard board) {
|
||||||
|
|
||||||
List<GomokuCell> playableCell = board.getPlayableCells();
|
List<GomokuCell> playableCell = board.getPlayableCells();
|
||||||
GomokuCell playCell= null;
|
GomokuCell playCell = null;
|
||||||
int x = 0, y = 0;
|
int x = 0, y = 0;
|
||||||
|
|
||||||
switch (difficulty) {
|
switch (difficulty) {
|
||||||
@@ -45,7 +46,7 @@ public class GomokuAI extends Player {
|
|||||||
playCell = playableCell.get(random.nextInt(playableCell.size()));
|
playCell = playableCell.get(random.nextInt(playableCell.size()));
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
Map<GomokuCell, Integer> map = GetCellPoint(board,this.color);
|
Map<GomokuCell, Integer> map = GetCellPoint(board, this.color);
|
||||||
int max = 0;
|
int max = 0;
|
||||||
for (Map.Entry<GomokuCell, Integer> entry : map.entrySet()) {
|
for (Map.Entry<GomokuCell, Integer> entry : map.entrySet()) {
|
||||||
if (entry.getValue() > max) {
|
if (entry.getValue() > max) {
|
||||||
@@ -55,15 +56,14 @@ public class GomokuAI extends Player {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
Map<GomokuCell, Integer> map2 = GetCellPoint(board,this.color);
|
Map<GomokuCell, Integer> map2 = GetCellPoint(board, this.color);
|
||||||
Color other = null;
|
Color other = null;
|
||||||
if(this.color == Color.WHITE) {
|
if (this.color == Color.WHITE) {
|
||||||
other = Color.BLACK;
|
other = Color.BLACK;
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
other = Color.WHITE;
|
other = Color.WHITE;
|
||||||
}
|
}
|
||||||
Map<GomokuCell, Integer> map3 = GetCellPoint(board,other);
|
Map<GomokuCell, Integer> map3 = GetCellPoint(board, other);
|
||||||
|
|
||||||
int max3 = 0;
|
int max3 = 0;
|
||||||
int max2 = 0;
|
int max2 = 0;
|
||||||
@@ -81,7 +81,6 @@ public class GomokuAI extends Player {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new AssertionError();
|
throw new AssertionError();
|
||||||
@@ -98,7 +97,7 @@ public class GomokuAI extends Player {
|
|||||||
* @param board The actual Gomoku board.
|
* @param board The actual Gomoku board.
|
||||||
* @return the Map of all Cell playable, and their point.
|
* @return the Map of all Cell playable, and their point.
|
||||||
*/
|
*/
|
||||||
public Map<GomokuCell, Integer> GetCellPoint(GomokuBoard board,Color color) {
|
public Map<GomokuCell, Integer> GetCellPoint(GomokuBoard board, Color color) {
|
||||||
// Prout
|
// Prout
|
||||||
List<GomokuCell> playableCell = board.getPlayableCells();
|
List<GomokuCell> playableCell = board.getPlayableCells();
|
||||||
Map<GomokuCell, Integer> map = new HashMap<>();
|
Map<GomokuCell, Integer> map = new HashMap<>();
|
||||||
@@ -124,11 +123,23 @@ public class GomokuAI extends Player {
|
|||||||
|
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------Getter--------------------------
|
// ------------------Getter--------------------------
|
||||||
|
/**
|
||||||
|
* Return the difficulty of the AI.
|
||||||
|
*
|
||||||
|
* @return 0: easy, 1: medium, 2: hard
|
||||||
|
*/
|
||||||
public int GetDifficulty() {
|
public int GetDifficulty() {
|
||||||
return difficulty;
|
return difficulty;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------Setter--------------------------
|
// ------------------Setter--------------------------
|
||||||
|
/**
|
||||||
|
* Change Difficulty of the AI.
|
||||||
|
*
|
||||||
|
* @param difficulty 0: easy, 1: medium, 2: hard
|
||||||
|
*/
|
||||||
public void SetDifficulty(int difficulty) {
|
public void SetDifficulty(int difficulty) {
|
||||||
this.difficulty = difficulty;
|
this.difficulty = difficulty;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import java.util.ArrayList;
|
import java.util.EnumMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.EnumMap;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The board of the game Gomoku.
|
* The board of the game Gomoku.
|
||||||
@@ -14,32 +14,6 @@ public class GomokuBoard {
|
|||||||
/** The height of the GomokuBoard. */
|
/** The height of the GomokuBoard. */
|
||||||
private int boardHeight;
|
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--------------------------
|
// ------------------Constructors--------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
|
|
||||||
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;
|
||||||
|
|
||||||
public class GomokuGame {
|
public class GomokuGame {
|
||||||
|
|
||||||
@@ -129,7 +129,6 @@ public class GomokuGame {
|
|||||||
this.colorP1 = colorPlayer1;
|
this.colorP1 = colorPlayer1;
|
||||||
currentPlayer = this.player1.color == Color.WHITE ? this.player1 : this.player2;
|
currentPlayer = this.player1.color == Color.WHITE ? this.player1 : this.player2;
|
||||||
this.currentPlayerInt = this.player1.color == Color.WHITE ? 1 : 2;
|
this.currentPlayerInt = this.player1.color == Color.WHITE ? 1 : 2;
|
||||||
this.renderer = new SwingRenderer();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
|
|
||||||
|
|
||||||
public abstract class GomokuRenderer {
|
public abstract class GomokuRenderer {
|
||||||
|
|
||||||
protected GomokuGame game;
|
protected GomokuGame game;
|
||||||
|
|
||||||
public abstract void init(GomokuGame game);
|
public abstract void init(GomokuGame game);
|
||||||
|
|
||||||
public abstract void update();
|
public abstract void update();
|
||||||
|
|
||||||
public abstract void updateStatus(String status);
|
public abstract void updateStatus(String status);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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) {}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user