diff --git a/OOP_2B1_Project/default.board b/OOP_2B1_Project/default.board index 8d83268..cece884 100644 --- a/OOP_2B1_Project/default.board +++ b/OOP_2B1_Project/default.board @@ -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 diff --git a/OOP_2B1_Project/src/Main.java b/OOP_2B1_Project/src/Main.java index 2ff297c..52a0a77 100644 --- a/OOP_2B1_Project/src/Main.java +++ b/OOP_2B1_Project/src/Main.java @@ -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(); diff --git a/OOP_2B1_Project/src/backend/AutoPlayer.java b/OOP_2B1_Project/src/backend/AutoPlayer.java index afd833b..fb995bc 100644 --- a/OOP_2B1_Project/src/backend/AutoPlayer.java +++ b/OOP_2B1_Project/src/backend/AutoPlayer.java @@ -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> 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; + } } \ No newline at end of file diff --git a/OOP_2B1_Project/src/backend/Board.java b/OOP_2B1_Project/src/backend/Board.java index 8cfd3fa..94e7226 100644 --- a/OOP_2B1_Project/src/backend/Board.java +++ b/OOP_2B1_Project/src/backend/Board.java @@ -197,19 +197,21 @@ public class Board { return boardToFile.toFile(); } - public Board(ArrayList> 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; + } + } diff --git a/OOP_2B1_Project/src/backend/Game.java b/OOP_2B1_Project/src/backend/Game.java index 043e1f0..3f7997e 100644 --- a/OOP_2B1_Project/src/backend/Game.java +++ b/OOP_2B1_Project/src/backend/Game.java @@ -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 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); + } } diff --git a/OOP_2B1_Project/src/windowInterface/MyInterface.java b/OOP_2B1_Project/src/windowInterface/MyInterface.java index 8e0da84..1b4ae1e 100644 --- a/OOP_2B1_Project/src/windowInterface/MyInterface.java +++ b/OOP_2B1_Project/src/windowInterface/MyInterface.java @@ -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 + } + } }