186 lines
6.4 KiB
Java
186 lines
6.4 KiB
Java
package backend;
|
|
|
|
import java.util.Timer;
|
|
import java.util.TimerTask;
|
|
|
|
/**
|
|
* ChessTimer manages the time control for both players in a chess game
|
|
*/
|
|
public class ChessTimer {
|
|
private long whiteTimeMillis; // Time remaining for white player in milliseconds
|
|
private long blackTimeMillis; // Time remaining for black player in milliseconds
|
|
private long lastUpdateTime; // The timestamp when timer was last updated
|
|
private boolean isRunning; // Flag to track if timer is active
|
|
private boolean isWhiteTurn; // Flag to track which player's clock is running
|
|
private Timer timer; // Java Timer object that handles periodic updates
|
|
private TimerUpdateListener listener; // Callback interface to notify UI of time changes
|
|
|
|
/**
|
|
* Interface for notifying time updates to the UI or other components
|
|
*/
|
|
public interface TimerUpdateListener {
|
|
// Called periodically to update displayed time
|
|
void onTimeUpdate(long whiteTimeMillis, long blackTimeMillis);
|
|
// Called when a player runs out of time
|
|
void onTimeExpired(boolean isWhiteExpired);
|
|
}
|
|
|
|
/**
|
|
* Constructor with initial time in minutes
|
|
* @param initialTimeMinutes Initial time for both players in minutes
|
|
* @param listener Listener to receive time updates
|
|
*/
|
|
public ChessTimer(int initialTimeMinutes, TimerUpdateListener listener) {
|
|
// Convert minutes to milliseconds for internal tracking
|
|
this.whiteTimeMillis = initialTimeMinutes * 60 * 1000;
|
|
this.blackTimeMillis = initialTimeMinutes * 60 * 1000;
|
|
this.isRunning = false; // Timer starts paused
|
|
this.isWhiteTurn = true; // White always starts in chess
|
|
this.listener = listener; // Store the listener for callbacks
|
|
}
|
|
|
|
/**
|
|
* Start the timer - begins counting down for the current player
|
|
*/
|
|
public void start() {
|
|
if (!isRunning) {
|
|
isRunning = true;
|
|
// Record the current time to measure elapsed time later
|
|
lastUpdateTime = System.currentTimeMillis();
|
|
|
|
// Create a timer that updates every 100ms (10 times per second)
|
|
timer = new Timer(true); // true = daemon thread
|
|
timer.scheduleAtFixedRate(new TimerTask() {
|
|
@Override
|
|
public void run() {
|
|
updateTime(); // Update the clock on each tick
|
|
}
|
|
}, 0, 100); // Initial delay 0ms, then every 100ms
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Stop the timer - pauses countdown for both players
|
|
*/
|
|
public void stop() {
|
|
if (isRunning) {
|
|
isRunning = false;
|
|
updateTime(); // Update one last time before stopping
|
|
|
|
// Clean up the timer resources
|
|
if (timer != null) {
|
|
timer.cancel();
|
|
timer = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reset the timer to the initial values
|
|
* @param initialTimeMinutes New initial time in minutes
|
|
*/
|
|
public void reset(int initialTimeMinutes) {
|
|
stop(); // Stop any running timer first
|
|
// Reset both clocks to the specified time
|
|
this.whiteTimeMillis = initialTimeMinutes * 60 * 1000;
|
|
this.blackTimeMillis = initialTimeMinutes * 60 * 1000;
|
|
this.isWhiteTurn = true; // White starts again
|
|
notifyTimeUpdate(); // Update the UI with new times
|
|
}
|
|
|
|
/**
|
|
* Switch the active player when a move is made
|
|
* @param isWhiteTurn True if it's now white's turn
|
|
*/
|
|
public void switchTurn(boolean isWhiteTurn) {
|
|
if (isRunning) {
|
|
// Before changing turns, update the current player's elapsed time
|
|
updateTime();
|
|
}
|
|
// Switch to the other player's clock
|
|
this.isWhiteTurn = isWhiteTurn;
|
|
notifyTimeUpdate(); // Update the UI with current times
|
|
}
|
|
|
|
/**
|
|
* Update the time based on elapsed time since last update
|
|
*/
|
|
private void updateTime() {
|
|
if (!isRunning) return; // Don't update if timer is paused
|
|
|
|
// Calculate elapsed time since last update
|
|
long currentTime = System.currentTimeMillis();
|
|
long elapsedTime = currentTime - lastUpdateTime;
|
|
lastUpdateTime = currentTime; // Reset for next update
|
|
|
|
// Deduct time from the active player's clock
|
|
if (isWhiteTurn) {
|
|
// White's turn - deduct from white's clock
|
|
whiteTimeMillis -= elapsedTime;
|
|
if (whiteTimeMillis <= 0) {
|
|
// White ran out of time
|
|
whiteTimeMillis = 0; // Don't go negative
|
|
stop(); // Stop the timer
|
|
if (listener != null) {
|
|
listener.onTimeExpired(true); // Notify that white's time expired
|
|
}
|
|
}
|
|
} else {
|
|
// Black's turn - deduct from black's clock
|
|
blackTimeMillis -= elapsedTime;
|
|
if (blackTimeMillis <= 0) {
|
|
// Black ran out of time
|
|
blackTimeMillis = 0; // Don't go negative
|
|
stop(); // Stop the timer
|
|
if (listener != null) {
|
|
listener.onTimeExpired(false); // Notify that black's time expired
|
|
}
|
|
}
|
|
}
|
|
|
|
// Notify the UI to update displayed times
|
|
notifyTimeUpdate();
|
|
}
|
|
|
|
/**
|
|
* Notify the listener about time updates
|
|
*/
|
|
private void notifyTimeUpdate() {
|
|
if (listener != null) {
|
|
// Send current times to the UI
|
|
listener.onTimeUpdate(whiteTimeMillis, blackTimeMillis);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get white player's remaining time in milliseconds
|
|
*/
|
|
public long getWhiteTimeMillis() {
|
|
return whiteTimeMillis;
|
|
}
|
|
|
|
/**
|
|
* Get black player's remaining time in milliseconds
|
|
*/
|
|
public long getBlackTimeMillis() {
|
|
return blackTimeMillis;
|
|
}
|
|
|
|
/**
|
|
* Format milliseconds as "mm:ss" for display
|
|
*/
|
|
public static String formatTime(long timeMillis) {
|
|
// Extract minutes and seconds from milliseconds
|
|
int seconds = (int) (timeMillis / 1000) % 60;
|
|
int minutes = (int) (timeMillis / (60 * 1000));
|
|
// Format as two digits for minutes and seconds
|
|
return String.format("%02d:%02d", minutes, seconds);
|
|
}
|
|
|
|
/**
|
|
* Check if the timer is currently running
|
|
*/
|
|
public boolean isRunning() {
|
|
return isRunning;
|
|
}
|
|
} |