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/GomokuAI.java b/src/GomokuAI.java index 850cfb0..45a324a 100644 --- a/src/GomokuAI.java +++ b/src/GomokuAI.java @@ -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 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 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 6878c3a..f93c90d 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.*/ @@ -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 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; + } + + /** + * 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-------------------------- diff --git a/src/GomokuCell.java b/src/GomokuCell.java index 06db3e1..5b9578c 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; import java.util.List; import java.util.ArrayList; @@ -41,12 +43,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; } diff --git a/src/GomokuRenderer.java b/src/GomokuRenderer.java index 1618ff1..ab4f21e 100644 --- a/src/GomokuRenderer.java +++ b/src/GomokuRenderer.java @@ -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(); } diff --git a/src/Human.java b/src/Human.java index 2e63a3c..089c6e1 100644 --- a/src/Human.java +++ b/src/Human.java @@ -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); } } \ No newline at end of file diff --git a/src/Player.java b/src/Player.java index 73138d0..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 Coordinate chooseMove(GomokuBoard board); + public abstract GomokuCell chooseMove(GomokuBoard board); } \ No newline at end of file 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 e8f8b8e..30b1670 100644 --- a/src/SwingRenderer.java +++ b/src/SwingRenderer.java @@ -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(); + } +} \ No newline at end of file