Files
gomoku/src/GomokuAI.java
2025-05-06 16:59:05 +02:00

148 lines
4.3 KiB
Java

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.HashMap;
public class GomokuAI extends Player {
// ------------------Constructors--------------------------
/**
* The constructor of the GomokuAI.
*
* @param name The name of the player.
* @param color The color of the player.
*/
public GomokuAI(String name, Color color, int tokens) {
super(name, color, tokens);
}
/**
* This class is an extends of the class Player, this allows the GomokuAI to
* choose his move.
*/
/** The random initialization */
Random random = new Random();
/** The difficulty of the GomokuAI */
private int difficulty = 2;
// ------------------Methods--------------------------
/**
* Return the coordinate of the move played by the Gomoku AI.
*
* @param board The actual Gomoku board.
* @return The Cell of the move played.
*/
@Override
public GomokuCell chooseMove(GomokuBoard board) {
List<GomokuCell> playableCell = board.getPlayableCells();
GomokuCell playCell = null;
int x = 0, y = 0;
switch (difficulty) {
case 0:
playCell = playableCell.get(random.nextInt(playableCell.size()));
break;
case 1:
Map<GomokuCell, Integer> map = GetCellPoint(board, this.color);
int max = 0;
for (Map.Entry<GomokuCell, Integer> entry : map.entrySet()) {
if (entry.getValue() > max) {
max = entry.getValue();
playCell = entry.getKey();
}
}
break;
case 2:
Map<GomokuCell, Integer> map2 = GetCellPoint(board, this.color);
Color other = null;
if (this.color == Color.WHITE) {
other = Color.BLACK;
} else {
other = Color.WHITE;
}
Map<GomokuCell, Integer> map3 = GetCellPoint(board, other);
int max3 = 0;
int max2 = 0;
for (Map.Entry<GomokuCell, Integer> entry : map2.entrySet()) {
if (entry.getValue() > max2) {
max2 = entry.getValue();
playCell = entry.getKey();
}
}
for (Map.Entry<GomokuCell, Integer> entry : map3.entrySet()) {
if (entry.getValue() > max3) {
max3 = entry.getValue();
playCell = entry.getKey();
}
}
break;
default:
throw new AssertionError();
}
System.out.println("L'IA à choisi : " + 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, Color color) {
// Prout
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;
}
// ------------------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;
}
}