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] 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