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

This commit is contained in:
Aubin DORIVAL
2025-03-31 11:03:47 +02:00
9 changed files with 254 additions and 27 deletions

View File

@@ -5,8 +5,12 @@ public class ConsoleRenderer extends GomokuRenderer {
public ConsoleRenderer() {}
@Override
public void init() {}
public void init(GomokuGame game) {
System.out.println("Console Renderer initialized.");
}
@Override
public void update(GomokuGame game) {}
public void update() {
// TODO: draw the board
}
}

View File

@@ -1,6 +1,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class GomokuAI{
public class GomokuAI extends Player{
/**
* This class is an extends of the class Player, this allows the GomokuAI to choose his move.
@@ -15,22 +18,60 @@ public class GomokuAI{
/**
* Return the coordinate of the move played by the Gomoku AI.
* @param Board The actual Gomoku board.
* @return The coordinate of the move played.
* @return The Cell of the move played.
*/
public Coordinate chooseMove(GomokuBoard board){
public GomokuCell chooseMove(GomokuBoard board){
List<GomokuCell> playableCell = board.getPlayableCells();
GomokuCell playCell;
int x=0,y=0;
if(difficulty == 0)
{
int rand = random.nextInt(board.getPlayableCells().size());
}
switch (difficulty) {
case 0:
playCell = playableCell.get(random.nextInt(playableCell.size()));
break;
case 1:
System.out.println("L'IA réflechit...");
default:
throw new AssertionError();
}
System.out.println("L'IA à choisi : "+ x+", "+ y);
return new Coordinate(x,y);
return playCell;
}
/**
* Return a Map of all Cell playable, and their point.
* @param Board The actual Gomoku board.
* @return the Map of all Cell playable, and their point.
*/
public Map<GomokuCell, Integer> GetCellPoint(GomokuBoard board){
List<GomokuCell> playableCell = board.getPlayableCells();
Map<GomokuCell, Integer> map = new HashMap<>();
for (GomokuCell gomokuCell : playableCell) {
switch(this.color){
case Color.WHITE:
gomokuCell.setState(Color.BLACK);
break;
case Color.BLACK:
gomokuCell.setState(Color.WHITE);
break;
default:
throw new AssertionError();
}
map.put(gomokuCell, board.countMax(board.countAlignedCells(gomokuCell)));
gomokuCell.setState(Color.NIL);
}
return map;
}
}

View File

@@ -1,7 +1,10 @@
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GomokuBoard {
public class GomokuBoard{
/** The firstcell in the board, at the top left of the board. */
private GomokuCell firstCell;
/** The width of the GomokuBoard.*/
@@ -131,6 +134,41 @@ public class GomokuBoard {
return output;
}
/**
* This method return a Map of number aligned cells.
* @param cell A cell.
* @return Map of number aligned cells.
*/
public Map<Cardinal, Integer> countAlignedCells(GomokuCell cell){
Map<Cardinal, Integer> map = new HashMap<>();
Map<Cardinal, Integer> mapColor = cell.getSameColorNeighbour();
map.put(Cardinal.N, mapColor.get(Cardinal.N)+mapColor.get(Cardinal.S)+1);
map.put(Cardinal.W,mapColor.get(Cardinal.W)+mapColor.get(Cardinal.E)+1);
map.put(Cardinal.NW, mapColor.get(Cardinal.NW)+mapColor.get(Cardinal.SE)+1);
map.put(Cardinal.SW, mapColor.get(Cardinal.SW)+mapColor.get(Cardinal.NE)+1);
return map;
}
/**
* This method return the number max of the aligned Cells.
* @param mapColor A map of number aligned cells.
* @return int, the number max of the aligned Cells.
*/
public int countMax(Map<Cardinal, Integer> mapColor){
Map<Cardinal, Integer> map = new HashMap<>();
int max = 0;
for (Map.Entry<Cardinal, Integer> entry : map.entrySet()) {
if(entry.getValue() > max){
max = entry.getValue();
}
}
return max;
}
//------------------Overides--------------------------

View File

@@ -1,4 +1,6 @@
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.List;
import java.util.ArrayList;
@@ -41,12 +43,42 @@ public class GomokuCell{
public EnumMap<Cardinal, GomokuCell> getAllNeighbour(){
return neighbour;
}
/**
* Return the number of same colored neighbours in all direction.
* @return The Map of neighbours.
*/
public Map<Cardinal, Integer> getSameColorNeighbour(){
Map<Cardinal, Integer> map = new HashMap<>();
map.put(Cardinal.N, 0);
map.put(Cardinal.W,0);
map.put(Cardinal.NW, 0);
map.put(Cardinal.SW, 0);
map.put(Cardinal.S, 0);
map.put(Cardinal.SE, 0);
map.put(Cardinal.E, 0);
map.put(Cardinal.NE, 0);
Color color = this.getState();
for (Map.Entry<Cardinal, Integer> entry : map.entrySet()) {
GomokuCell actualcell = this;
while(this.getState() == actualcell.getNeighbour(entry.getKey()).getState())
{
entry.setValue(entry.getValue()+1);
actualcell=actualcell.getNeighbour(entry.getKey());
}
}
return map;
}
/**
* Return the state.
* @return The state of the current cell with one Color.
*/
public Color getSate(){
public Color getState(){
return this.state;
}

View File

@@ -2,6 +2,8 @@
public abstract class GomokuRenderer {
public abstract void init();
public abstract void update(GomokuGame game);
protected GomokuGame game;
public abstract void init(GomokuGame game);
public abstract void update();
}

View File

@@ -11,9 +11,9 @@ public class Human extends Player{
/**
* Return the coordinate of the move played by the player.
* @param Board The actual Gomoku board.
* @return The coordinate of the move played.
* @return The cell of the move played.
*/
public Coordinate chooseMove(GomokuBoard board){
public GomokuCell chooseMove(GomokuBoard board){
Console cons = System.console();
@@ -60,6 +60,6 @@ public class Human extends Player{
System.out.println("Vous avez saisi : "+ x+", "+ y);
return new Coordinate(x,y);
return board.get(x,y);
}
}

View File

@@ -10,15 +10,20 @@ abstract class Player{
/** The name of the Player */
private String name;
/** The color of the Player */
protected Color color;
//------------------Constructors--------------------------
/**
* The constructor of the Player.
* @param name The name of the player.
* @param color The color of the player.
*/
public void Player(String name){
public void Player(String name,Color color){
this.name = name;
this.color = color;
}
//------------------Gets--------------------------
@@ -28,7 +33,14 @@ abstract class Player{
* @return The name of the player.
*/
public String getName() {
return name;
return this.name;
}
/**
* Return the color of the player.
* @return The color of the player.
*/
public Color getColor() {
return this.color;
}
//------------------Methods--------------------------
@@ -36,7 +48,7 @@ abstract class Player{
/**
* Return the coordinate of the move played by the player.
* @param Board The actual Gomoku board.
* @return The coordinate of the move played.
* @return The cell of the move played.
*/
public abstract Coordinate chooseMove(GomokuBoard board);
public abstract GomokuCell chooseMove(GomokuBoard board);
}

33
src/RenderBoard.java Normal file
View File

@@ -0,0 +1,33 @@
import java.awt.Color;
import java.awt.Graphics;
class RenderBoard {
private int width;
private int height;
private int[][] boardState; // Example representation of the board state
public RenderBoard(int width, int height) {
this.width = width;
this.height = height;
}
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, int w, int h) {
// Draw the board here
g.setColor(Color.LIGHT_GRAY);
int cellSize = w / this.width;
for (int i = 0; i < this.width; i++) {
for (int j = 0; j < this.height; j++) {
g.drawRect(x + i * cellSize, y + j * cellSize, cellSize, cellSize);
// Draw the tokens here
g.fillOval(x + i * cellSize, y + j * cellSize, cellSize, cellSize);
}
}
}
}

View File

@@ -1,12 +1,77 @@
import javax.swing.SwingUtilities;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
public class SwingRenderer extends GomokuRenderer {
private JFrame mainFrame;
private GomokuGame game;
private RenderCanvas canvas;
public SwingRenderer() {}
@Override
public void init() {}
public static void main(String[] args) {
SwingRenderer s = new SwingRenderer();
s.init(null);
}
@Override
public void update(GomokuGame game) {}
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);
}
});
}
@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);
}
}
class RenderCanvas extends JPanel {
public RenderBoard board;
public RenderCanvas() {
setBorder(BorderFactory.createLineBorder(Color.black));
setBackground(Color.white);
board = new RenderBoard(8, 8); // Example board size
}
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int midX = getWidth() / 2;
int midY = getHeight() / 2;
int boardWidth = 400; // Example board width
int boardHeight = 400; // Example board height
int x = midX - (boardWidth / 2);
int y = midY - (boardHeight / 2);
board.drawBoard(g, x, y, boardWidth, boardHeight);
}
public void draw(GomokuGame game) {
// Update the game state and repaint the canvas
board.update(game);
repaint();
}
}