AI difficulty + problem fixing for file loading

This commit is contained in:
Romain MURPHY 2025-04-30 15:52:48 +02:00
parent 2ae4296d7d
commit 2ca782b4b0
6 changed files with 81 additions and 27 deletions

View File

@ -6,4 +6,4 @@ BP,BP,BP,BP,BP,BP,BP,BP
, , , , , , ,
WP,WP,WP,WP,WP,WP,WP,WP
WR,WN,WB,WQ,WK,WB,WN,WR
W
0W

View File

@ -10,9 +10,9 @@ public class Main {
public static void main(String[] args) {
// testing :
Board testBoard = new Board(8, 8);
testBoard.populateBoard();
System.out.println(testBoard.toString());
// Board testBoard = new Board(8, 8);
// testBoard.populateBoard();
// System.out.println(testBoard.toString());
// launches graphical interface :
MyInterface mjf = new MyInterface();

View File

@ -5,7 +5,11 @@ import java.util.ArrayList;
public class AutoPlayer {
private KingCheck kingCheck = new KingCheck();
private int MAX_DEPTH = 3;
public int MAX_DEPTH;
public AutoPlayer() {}
public AutoPlayer(int MAX_DEPTH) {
this.MAX_DEPTH = MAX_DEPTH;
}
private int[][] PAWN_TABLE = {
{ 0, 0, 0, 0, 0, 0, 0, 0 },
{ 5, 5, 5, -5, -5, 5, 5, 5 },
@ -67,6 +71,7 @@ public class AutoPlayer {
{ 20, 30, 10, 0, 0, 10, 30, 20}
};
public Move computeBestMove(ArrayList<ArrayList<Piece>> board, boolean isWhiteTurn) {
// System.out.println(MAX_DEPTH);
Move bestMove = null;
int bestScore = isWhiteTurn ? Integer.MIN_VALUE : Integer.MAX_VALUE;
@ -84,7 +89,6 @@ public class AutoPlayer {
bestMove = move;
}
}
return bestMove;
}
@ -224,4 +228,8 @@ public class AutoPlayer {
return 0;
}
}
public void setMAX_DEPTH(int mAX_DEPTH) {
MAX_DEPTH = mAX_DEPTH;
}
}

View File

@ -197,19 +197,21 @@ public class Board {
return boardToFile.toFile();
}
public Board(ArrayList<ArrayList<Piece>> board,int turnNumber,boolean turnColor) {
this.board = board;
this.turnNumber = turnNumber;
this.turnColor = turnColor;
}
public Board toBoard(String[] array) {
public Board(String[] array) {
FileBoard fileToBoard = new FileBoard(array);
Board boardF = new Board(fileToBoard.getBoard(),fileToBoard.getTurnNumber(),fileToBoard.isTurnColor());
return boardF;
// this.turnColor = fileToBoard.isTurnColor();
// this.turnNumber = fileToBoard.getTurnNumber();
// boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
board = fileToBoard.getBoard();
turnNumber = fileToBoard.getTurnNumber();
turnColor = fileToBoard.isTurnColor();
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
}
// public Board toBoard(String[] array) {
// FileBoard fileToBoard = new FileBoard(array);
// Board boardF = new Board(fileToBoard.getBoard(),fileToBoard.getTurnNumber(),fileToBoard.isTurnColor());
// return boardF;
//// this.turnColor = fileToBoard.isTurnColor();
//// this.turnNumber = fileToBoard.getTurnNumber();
//// boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
// }
/* The following methods require more work ! */
@ -300,4 +302,8 @@ public class Board {
this.boardHistory = boardHistory;
}
public boolean isTurnColor() {
return turnColor;
}
}

View File

@ -3,7 +3,6 @@ package backend;
import windowInterface.MyInterface;
public class Game extends Thread {
private AutoPlayer AP = new AutoPlayer();
private AutoPlayer aiPlayer;
private Board board;
@ -49,11 +48,11 @@ public class Game extends Thread {
private void aiPlayerTurn() {
if(isAITurn()) {
if (AP.getAllLegalMoves(board.getBoard(), board.isTurnWhite()).size() != 0){
board.playMove(aiPlayer.computeBestMove(board.getBoard(),board.isTurnWhite()));
if (aiPlayer.getAllLegalMoves(board.getBoard(), board.isTurnWhite()).size() == 0){
mjf.showGameOverMessage("Game Over");
}
else {
mjf.showGameOverMessage("Game Over");
board.playMove(aiPlayer.computeBestMove(board.getBoard(),board.isTurnWhite()));
}
}
}
@ -66,7 +65,7 @@ public class Game extends Thread {
return;
}
if(!isAITurn()) {
if (AP.getAllLegalMoves(board.getBoard(), board.isTurnWhite()).size() != 0){
if (aiPlayer.getAllLegalMoves(board.getBoard(), board.isTurnWhite()).size() != 0){
board.userTouch(x, y);
}
else {
@ -95,7 +94,7 @@ public class Game extends Thread {
}
public void setBoard(String[] array) {
board = board.toBoard(array);
board = new Board(array);
}
public Iterable<Piece> getPieces() {
@ -115,7 +114,26 @@ public class Game extends Thread {
}
public void toggleAI(boolean isWhite) {
// System.out.println(isWhite);
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
}
public void setTurnNumber(int turnNumber) {
board.setTurnNumber(turnNumber);
}
public void setTurnColor(boolean turnColor) {
board.setTurnColor(turnColor);
}
public int getTurnNumber() {
return board.getTurnNumber();
}
public boolean getTurnColor() {
return board.isTurnColor();
}
public void setAutoPlayerDepth(int depth) {
aiPlayer.setMAX_DEPTH(depth);
}
}

View File

@ -161,6 +161,8 @@ public class MyInterface extends JFrame {
public void clicButtonStart() {
this.instantiateSimu();
int depth = askDifficulty();
game.setAutoPlayerDepth(depth);
game.setDefaultSetup();
}
@ -200,7 +202,6 @@ public class MyInterface extends JFrame {
try {
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
String line = fileContent.readLine();
int colorID = 0;
while (line != null) {
lines.add(line);
line = fileContent.readLine();
@ -213,8 +214,9 @@ public class MyInterface extends JFrame {
game = loadedSim;
panelDraw.setGame(game);
this.repaint();
// update(game.getTurnNumber(), game.isTurnColor());
game.start();
// this.repaint();
// update(game.getTurnNumber(), game.getTurnColor());
}
}
@ -272,4 +274,24 @@ public class MyInterface extends JFrame {
public void showGameOverMessage(String message) {
JOptionPane.showMessageDialog(this, message, "Game Over", JOptionPane.INFORMATION_MESSAGE);
}
private int askDifficulty() {
Object[] options = {"Easy", "Medium", "Hard"};
int choice = JOptionPane.showOptionDialog(
this,
"Select Difficulty Level:",
"Difficulty",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1] // default: Medium
);
switch (choice) {
case 0: return 1; // Easy
case 1: return 2; // Medium
case 2: return 3; // Hard
default: return 2; // fallback to Medium
}
}
}