State / Behavioral• Patterns: Observer, State
Chess Clock
Easy
Problem Summary
Design a two-player Chess Clock tracking match timing, dynamic increments (Fischer delay), and flags.
Functional Scope
- Dual timers tracking Player 1 and Player 2 remaining time.
- Switch active clock cleanly on player turn switch.
- Support time increment increments (adds N seconds after move).
- Trigger notifications if a player's timer runs out (Flagged).
Entity-Relationship (ER) Schema
ChessClock [1] <---> [2] PlayerTimer ChessClock [1] <---> [1] ClockState
Design Approach
Implement thread-safe pause/resume triggers. Protect state mutations from multiple button presses during intense time scrambles.
Core Classes & Models
ChessClock (Main wrapper)Timer (Active thread / counter)ClockState (ActivePlayer1, ActivePlayer2, Paused, Flagged)
Code Blueprint
public class PlayerTimer {
private long remainingMs;
private long incrementMs;
public void commitMove() { remainingMs += incrementMs; }
}