Actualiser src/backend/Game.java
This commit is contained in:
parent
1c570c3d01
commit
e80133174f
|
|
@ -2,19 +2,26 @@ package backend;
|
|||
|
||||
import windowInterface.MyInterface;
|
||||
|
||||
/**
|
||||
* Main game class that manages the chess game logic, board state, AI, and timer
|
||||
*/
|
||||
public class Game extends Thread {
|
||||
|
||||
private AutoPlayer aiPlayer;
|
||||
private Board board;
|
||||
private ChessTimer chessTimer; // Add chess timer
|
||||
private MyInterface mjf;
|
||||
private int COL_NUM = 8;
|
||||
private int LINE_NUM = 8;
|
||||
private int loopDelay = 250;
|
||||
boolean[] activationAIFlags;
|
||||
private int initialTimeMinutes = 10; // Default to 10 minutes per player
|
||||
private boolean timerEnabled = false; // Flag to enable/disable timer
|
||||
private AutoPlayer aiPlayer; // AI player that can make moves automatically
|
||||
private Board board; // Game board containing pieces and game state
|
||||
private ChessTimer chessTimer; // Chess timer for both players
|
||||
private MyInterface mjf; // Interface to communicate with UI
|
||||
private int COL_NUM = 8; // Number of columns on the board
|
||||
private int LINE_NUM = 8; // Number of rows on the board
|
||||
private int loopDelay = 250; // Delay between game loop iterations in milliseconds
|
||||
boolean[] activationAIFlags; // Flags to control AI activation for each player
|
||||
private int initialTimeMinutes = 10; // Default time allocation for each player
|
||||
private boolean timerEnabled = false; // Whether the timer feature is enabled
|
||||
|
||||
/**
|
||||
* Constructor that initializes the game with UI interface
|
||||
* @param mjfParam The UI interface to update with game changes
|
||||
*/
|
||||
public Game(MyInterface mjfParam) {
|
||||
mjf = mjfParam;
|
||||
board = new Board(COL_NUM, LINE_NUM);
|
||||
|
|
@ -24,7 +31,7 @@ public class Game extends Thread {
|
|||
activationAIFlags = new boolean[2];
|
||||
aiPlayer = new AutoPlayer();
|
||||
|
||||
// Initialize the timer with a listener
|
||||
// Initialize the timer with a listener for UI updates
|
||||
chessTimer = new ChessTimer(initialTimeMinutes, new ChessTimer.TimerUpdateListener() {
|
||||
@Override
|
||||
public void onTimeUpdate(long whiteTimeMillis, long blackTimeMillis) {
|
||||
|
|
@ -40,35 +47,54 @@ public class Game extends Thread {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Width of the chess board
|
||||
*/
|
||||
public int getWidth() {
|
||||
return board.getWidth();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Height of the chess board
|
||||
*/
|
||||
public int getHeight() {
|
||||
return board.getHeight();
|
||||
}
|
||||
|
||||
// Added getBoard method to access the board
|
||||
/**
|
||||
* Access the game board
|
||||
* @return The current board object
|
||||
*/
|
||||
public Board getBoard() {
|
||||
return this.board;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main game loop that runs in a separate thread
|
||||
*/
|
||||
public void run() {
|
||||
while(true) {
|
||||
aiPlayerTurn();
|
||||
mjf.update(board.getTurnNumber(), board.isTurnWhite());
|
||||
aiPlayerTurn(); // Process AI move if it's AI's turn
|
||||
mjf.update(board.getTurnNumber(), board.isTurnWhite()); // Update UI
|
||||
try {
|
||||
Thread.sleep(loopDelay);
|
||||
Thread.sleep(loopDelay); // Brief pause between iterations
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if it's AI's turn to move based on current player and AI activation
|
||||
* @return True if AI should make a move now
|
||||
*/
|
||||
private boolean isAITurn() {
|
||||
return activationAIFlags[board.isTurnWhite()?1:0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes AI move if it's AI's turn
|
||||
*/
|
||||
private void aiPlayerTurn() {
|
||||
if(isAITurn()) {
|
||||
// Before AI makes a move
|
||||
|
|
@ -76,13 +102,18 @@ public class Game extends Thread {
|
|||
|
||||
board.playMove(aiPlayer.computeBestMove(new Board(board)));
|
||||
|
||||
// After AI makes a move, switch the timer
|
||||
// After AI makes a move, switch the timer if turn changed
|
||||
if (timerEnabled && currentTurn != board.isTurnWhite()) {
|
||||
chessTimer.switchTurn(board.isTurnWhite());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles user clicks on the board
|
||||
* @param x X-coordinate of the click
|
||||
* @param y Y-coordinate of the click
|
||||
*/
|
||||
public void clickCoords(int x, int y) {
|
||||
int width = this.getWidth();
|
||||
int height = this.getHeight();
|
||||
|
|
@ -103,18 +134,35 @@ public class Game extends Thread {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a specific piece at the given location
|
||||
* @param isWhite Whether the piece is white
|
||||
* @param type Type of the piece
|
||||
* @param x X-coordinate on board
|
||||
* @param y Y-coordinate on board
|
||||
*/
|
||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||
board.setPiece(isWhite, type, x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return String array representation of the board for file saving
|
||||
*/
|
||||
public String[] getFileRepresentation() {
|
||||
return board.toFileRep();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the delay between game loop iterations
|
||||
* @param delay Delay in milliseconds
|
||||
*/
|
||||
public void setLoopDelay(int delay) {
|
||||
this.loopDelay = delay;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the board with standard chess starting position
|
||||
*/
|
||||
public void setDefaultSetup() {
|
||||
board.cleanBoard();
|
||||
board.populateBoard();
|
||||
|
|
@ -125,6 +173,10 @@ public class Game extends Thread {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a board state from string array
|
||||
* @param array Board representation to load
|
||||
*/
|
||||
public void setBoard(String[] array) {
|
||||
board = new Board(array);
|
||||
|
||||
|
|
@ -134,18 +186,36 @@ public class Game extends Thread {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection of all pieces on the board
|
||||
*/
|
||||
public Iterable<Piece> getPieces() {
|
||||
return board.getPieces();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the square at coordinates is selected
|
||||
* @param x X-coordinate on board
|
||||
* @param y Y-coordinate on board
|
||||
* @return True if the square is selected
|
||||
*/
|
||||
public boolean isSelected(int x, int y) {
|
||||
return board.isSelected(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the square at coordinates is highlighted as a possible move
|
||||
* @param x X-coordinate on board
|
||||
* @param y Y-coordinate on board
|
||||
* @return True if the square is highlighted
|
||||
*/
|
||||
public boolean isHighlighted(int x, int y) {
|
||||
return board.isHighlighted(x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Undoes the last move made
|
||||
*/
|
||||
public void undoLastMove() {
|
||||
// Store current turn before undo
|
||||
boolean currentTurn = board.isTurnWhite();
|
||||
|
|
@ -158,6 +228,10 @@ public class Game extends Thread {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles AI activation for the specified player color
|
||||
* @param isWhite The player color to toggle AI for
|
||||
*/
|
||||
public void toggleAI(boolean isWhite) {
|
||||
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
||||
}
|
||||
|
|
@ -184,6 +258,7 @@ public class Game extends Thread {
|
|||
|
||||
/**
|
||||
* Enable or disable the timer feature
|
||||
* @param enabled Whether timer should be enabled
|
||||
*/
|
||||
public void setTimerEnabled(boolean enabled) {
|
||||
this.timerEnabled = enabled;
|
||||
|
|
@ -195,7 +270,7 @@ public class Game extends Thread {
|
|||
}
|
||||
|
||||
/**
|
||||
* Check if timer is enabled
|
||||
* @return Whether timer is currently enabled
|
||||
*/
|
||||
public boolean isTimerEnabled() {
|
||||
return timerEnabled;
|
||||
|
|
@ -203,6 +278,7 @@ public class Game extends Thread {
|
|||
|
||||
/**
|
||||
* Set the initial time in minutes for both players
|
||||
* @param minutes Initial time in minutes
|
||||
*/
|
||||
public void setInitialTime(int minutes) {
|
||||
this.initialTimeMinutes = minutes;
|
||||
|
|
@ -212,7 +288,7 @@ public class Game extends Thread {
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the current chess timer
|
||||
* @return The current chess timer object
|
||||
*/
|
||||
public ChessTimer getChessTimer() {
|
||||
return chessTimer;
|
||||
|
|
|
|||
Loading…
Reference in New Issue