diff --git a/README.md b/README.md index cfa7d23..93f2f0e 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/src/GomokuAI.java b/src/GomokuAI.java index 1fdf95e..4dace93 100644 --- a/src/GomokuAI.java +++ b/src/GomokuAI.java @@ -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,7 +9,8 @@ public class GomokuAI extends Player { /** * 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. */ public GomokuAI(String name, Color color, int tokens) { @@ -37,7 +38,7 @@ public class GomokuAI extends Player { public GomokuCell chooseMove(GomokuBoard board) { List playableCell = board.getPlayableCells(); - GomokuCell playCell= null; + GomokuCell playCell = null; int x = 0, y = 0; switch (difficulty) { @@ -45,7 +46,7 @@ public class GomokuAI extends Player { playCell = playableCell.get(random.nextInt(playableCell.size())); break; case 1: - Map map = GetCellPoint(board,this.color); + Map map = GetCellPoint(board, this.color); int max = 0; for (Map.Entry entry : map.entrySet()) { if (entry.getValue() > max) { @@ -55,15 +56,14 @@ public class GomokuAI extends Player { } break; case 2: - Map map2 = GetCellPoint(board,this.color); - Color other = null; - if(this.color == Color.WHITE) { + Map map2 = GetCellPoint(board, this.color); + Color other = null; + if (this.color == Color.WHITE) { other = Color.BLACK; - } - else{ + } else { other = Color.WHITE; } - Map map3 = GetCellPoint(board,other); + Map map3 = GetCellPoint(board, other); int max3 = 0; int max2 = 0; @@ -81,7 +81,6 @@ public class GomokuAI extends Player { } } - break; default: throw new AssertionError(); @@ -98,7 +97,7 @@ public class GomokuAI extends Player { * @param board The actual Gomoku board. * @return the Map of all Cell playable, and their point. */ - public Map GetCellPoint(GomokuBoard board,Color color) { + public Map GetCellPoint(GomokuBoard board, Color color) { // Prout List playableCell = board.getPlayableCells(); Map map = new HashMap<>(); @@ -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; } diff --git a/src/GomokuBoard.java b/src/GomokuBoard.java index 439a9ad..23e8774 100644 --- a/src/GomokuBoard.java +++ b/src/GomokuBoard.java @@ -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-------------------------- /** diff --git a/src/GomokuGame.java b/src/GomokuGame.java index 458f3aa..ca20648 100644 --- a/src/GomokuGame.java +++ b/src/GomokuGame.java @@ -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(); } diff --git a/src/GomokuRenderer.java b/src/GomokuRenderer.java index fb12b03..2889be1 100644 --- a/src/GomokuRenderer.java +++ b/src/GomokuRenderer.java @@ -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); } diff --git a/src/RenderBoard.java b/src/RenderBoard.java deleted file mode 100644 index 104773f..0000000 --- a/src/RenderBoard.java +++ /dev/null @@ -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 - ); - } -} \ No newline at end of file diff --git a/src/SwingRenderer.java b/src/SwingRenderer.java deleted file mode 100644 index cd55be8..0000000 --- a/src/SwingRenderer.java +++ /dev/null @@ -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) {} -} \ No newline at end of file