From 7ba07d74985a207522f8d61dc13336aea5acfbcc Mon Sep 17 00:00:00 2001 From: Cyprien111 <105004796+Cyprien111@users.noreply.github.com> Date: Mon, 31 Mar 2025 09:40:32 +0200 Subject: [PATCH 1/5] get la somme des gomoku cell aligned --- src/GomokuBoard.java | 25 +++++++++++++++++++++++-- src/GomokuCell.java | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/GomokuBoard.java b/src/GomokuBoard.java index 089ffc5..be17475 100644 --- a/src/GomokuBoard.java +++ b/src/GomokuBoard.java @@ -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.*/ @@ -72,6 +75,24 @@ public class GomokuBoard { return output; } + //-------------------Methods------------------------- + /** + * This method return a Map of number aligned cells. + * @param cell A cell. + * @return Map of number aligned cells. + */ + public Map countAlignedCells(GomokuCell cell){ + + Map map = new HashMap<>(); + Map 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; + } //------------------Overides-------------------------- diff --git a/src/GomokuCell.java b/src/GomokuCell.java index 9c6cdb6..4b7c95d 100644 --- a/src/GomokuCell.java +++ b/src/GomokuCell.java @@ -1,4 +1,6 @@ import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.ExecutionException; /** @@ -39,12 +41,42 @@ public class GomokuCell{ public EnumMap getAllNeighbour(){ return neighbour; } + + /** + * Return the number of same colored neighbours in all direction. + * @return The Map of neighbours. + */ + public Map getSameColorNeighbour(){ + + Map 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 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; } From c5aa817339f73de0d2aa22b850ae238754575c61 Mon Sep 17 00:00:00 2001 From: Dorian HAMDANI Date: Mon, 31 Mar 2025 10:02:42 +0200 Subject: [PATCH 2/5] Add SwingRenderer base code --- src/GomokuRenderer.java | 4 +- src/SwingRenderer.java | 103 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/src/GomokuRenderer.java b/src/GomokuRenderer.java index 1618ff1..09f9f0f 100644 --- a/src/GomokuRenderer.java +++ b/src/GomokuRenderer.java @@ -2,6 +2,6 @@ public abstract class GomokuRenderer { - public abstract void init(); - public abstract void update(GomokuGame game); + public abstract void init(GomokuGame game); + public abstract void update(); } diff --git a/src/SwingRenderer.java b/src/SwingRenderer.java index e8f8b8e..7e06c0f 100644 --- a/src/SwingRenderer.java +++ b/src/SwingRenderer.java @@ -1,12 +1,109 @@ +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 RendererPanel 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 RendererPanel(); + 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 RendererPanel extends JPanel { + + public RendererBoard board; + + public RendererPanel() { + setBorder(BorderFactory.createLineBorder(Color.black)); + setBackground(Color.white); + board = new RendererBoard(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(); + } +} + +class RendererBoard { + + private int width; + private int height; + private GomokuGame game; + + public RendererBoard(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 + this.game = game; + } + + 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); + } + } + } +} \ No newline at end of file From a5d8512b34b6141622d66797756721de356c4bea Mon Sep 17 00:00:00 2001 From: Cyprien111 <105004796+Cyprien111@users.noreply.github.com> Date: Mon, 31 Mar 2025 10:10:27 +0200 Subject: [PATCH 3/5] AI and player play correctly --- src/GomokuAI.java | 22 ++++++++++++++-------- src/Human.java | 5 +++-- src/Player.java | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/GomokuAI.java b/src/GomokuAI.java index 850cfb0..35bc93f 100644 --- a/src/GomokuAI.java +++ b/src/GomokuAI.java @@ -1,3 +1,4 @@ +import java.util.List; import java.util.Random; public class GomokuAI{ @@ -17,20 +18,25 @@ public class GomokuAI{ * @param Board The actual Gomoku board. * @return The coordinate of the move played. */ - public Coordinate chooseMove(GomokuBoard board){ - + public GomokuCell chooseMove(GomokuBoard board){ + List 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; } } \ No newline at end of file diff --git a/src/Human.java b/src/Human.java index 2e63a3c..1b2e0b2 100644 --- a/src/Human.java +++ b/src/Human.java @@ -1,5 +1,6 @@ import java.io.Console; +import javax.swing.plaf.basic.BasicBorders; public class Human extends Player{ @@ -13,7 +14,7 @@ public class Human extends Player{ * @param Board The actual Gomoku board. * @return The coordinate of the move played. */ - public Coordinate chooseMove(GomokuBoard board){ + public GomokuCell chooseMove(GomokuBoard board){ Console cons = System.console(); @@ -60,6 +61,6 @@ public class Human extends Player{ System.out.println("Vous avez saisi : "+ x+", "+ y); - return new Coordinate(x,y); + return board.get(x,y); } } \ No newline at end of file diff --git a/src/Player.java b/src/Player.java index 73138d0..18e65ee 100644 --- a/src/Player.java +++ b/src/Player.java @@ -38,5 +38,5 @@ abstract class Player{ * @param Board The actual Gomoku board. * @return The coordinate of the move played. */ - public abstract Coordinate chooseMove(GomokuBoard board); + public abstract GomokuCell chooseMove(GomokuBoard board); } \ No newline at end of file From 6e5e60cd3640948e6bc98356aa8f316fd8817cd6 Mon Sep 17 00:00:00 2001 From: Dorian HAMDANI Date: Mon, 31 Mar 2025 10:50:51 +0200 Subject: [PATCH 4/5] More Renderer stuff --- src/ConsoleRenderer.java | 8 ++++++-- src/GomokuRenderer.java | 2 ++ src/RenderBoard.java | 33 ++++++++++++++++++++++++++++++ src/SwingRenderer.java | 44 ++++++---------------------------------- 4 files changed, 47 insertions(+), 40 deletions(-) create mode 100644 src/RenderBoard.java diff --git a/src/ConsoleRenderer.java b/src/ConsoleRenderer.java index d536b19..8388013 100644 --- a/src/ConsoleRenderer.java +++ b/src/ConsoleRenderer.java @@ -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 + } } diff --git a/src/GomokuRenderer.java b/src/GomokuRenderer.java index 09f9f0f..ab4f21e 100644 --- a/src/GomokuRenderer.java +++ b/src/GomokuRenderer.java @@ -2,6 +2,8 @@ public abstract class GomokuRenderer { + protected GomokuGame game; + public abstract void init(GomokuGame game); public abstract void update(); } diff --git a/src/RenderBoard.java b/src/RenderBoard.java new file mode 100644 index 0000000..a406c06 --- /dev/null +++ b/src/RenderBoard.java @@ -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); + } + } + } +} \ No newline at end of file diff --git a/src/SwingRenderer.java b/src/SwingRenderer.java index 7e06c0f..30b1670 100644 --- a/src/SwingRenderer.java +++ b/src/SwingRenderer.java @@ -11,7 +11,7 @@ public class SwingRenderer extends GomokuRenderer { private JFrame mainFrame; private GomokuGame game; - private RendererPanel canvas; + private RenderCanvas canvas; public SwingRenderer() {} @@ -27,7 +27,7 @@ public class SwingRenderer extends GomokuRenderer { mainFrame = new JFrame("Gomoku! - Projet L2"); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setSize(960, 960); - canvas = new RendererPanel(); + canvas = new RenderCanvas(); mainFrame.add(canvas); mainFrame.pack(); mainFrame.setVisible(true); @@ -43,14 +43,14 @@ public class SwingRenderer extends GomokuRenderer { } } -class RendererPanel extends JPanel { +class RenderCanvas extends JPanel { - public RendererBoard board; + public RenderBoard board; - public RendererPanel() { + public RenderCanvas() { setBorder(BorderFactory.createLineBorder(Color.black)); setBackground(Color.white); - board = new RendererBoard(8, 8); // Example board size + board = new RenderBoard(8, 8); // Example board size } public Dimension getPreferredSize() { @@ -74,36 +74,4 @@ class RendererPanel extends JPanel { board.update(game); repaint(); } -} - -class RendererBoard { - - private int width; - private int height; - private GomokuGame game; - - public RendererBoard(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 - this.game = game; - } - - 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); - } - } - } } \ No newline at end of file From 12d4b2014a4c11eeea04783d045a85caafdf7719 Mon Sep 17 00:00:00 2001 From: Cyprien111 <105004796+Cyprien111@users.noreply.github.com> Date: Mon, 31 Mar 2025 11:01:12 +0200 Subject: [PATCH 5/5] debut d'une ia par point --- src/GomokuAI.java | 39 +++++++++++++++++++++++++++++++++++++-- src/GomokuBoard.java | 29 +++++++++++++++++++++++------ src/Human.java | 3 +-- src/Player.java | 18 +++++++++++++++--- 4 files changed, 76 insertions(+), 13 deletions(-) diff --git a/src/GomokuAI.java b/src/GomokuAI.java index 35bc93f..45a324a 100644 --- a/src/GomokuAI.java +++ b/src/GomokuAI.java @@ -1,7 +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. @@ -16,7 +18,7 @@ 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 GomokuCell chooseMove(GomokuBoard board){ @@ -39,4 +41,37 @@ public class GomokuAI{ 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 GetCellPoint(GomokuBoard board){ + + List playableCell = board.getPlayableCells(); + Map 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; + } + } \ No newline at end of file diff --git a/src/GomokuBoard.java b/src/GomokuBoard.java index be17475..a68f0d2 100644 --- a/src/GomokuBoard.java +++ b/src/GomokuBoard.java @@ -75,12 +75,12 @@ public class GomokuBoard{ return output; } - //-------------------Methods------------------------- - /** - * This method return a Map of number aligned cells. - * @param cell A cell. - * @return Map of number aligned cells. - */ + + /** + * This method return a Map of number aligned cells. + * @param cell A cell. + * @return Map of number aligned cells. + */ public Map countAlignedCells(GomokuCell cell){ Map map = new HashMap<>(); @@ -94,6 +94,23 @@ public class GomokuBoard{ 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 mapColor){ + + Map map = new HashMap<>(); + int max = 0; + for (Map.Entry entry : map.entrySet()) { + if(entry.getValue() > max){ + max = entry.getValue(); + } + } + return max; + } + //------------------Overides-------------------------- @Override diff --git a/src/Human.java b/src/Human.java index 1b2e0b2..089c6e1 100644 --- a/src/Human.java +++ b/src/Human.java @@ -1,6 +1,5 @@ import java.io.Console; -import javax.swing.plaf.basic.BasicBorders; public class Human extends Player{ @@ -12,7 +11,7 @@ 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 GomokuCell chooseMove(GomokuBoard board){ diff --git a/src/Player.java b/src/Player.java index 18e65ee..9350896 100644 --- a/src/Player.java +++ b/src/Player.java @@ -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 GomokuCell chooseMove(GomokuBoard board); } \ No newline at end of file