The classes in this package collaborate within the State Design Pattern to implement the behavior of a chess engine that uses the Universal Chess Interface (UCI) protocol. The State Design Pattern allows the engine to encapsulate its behavior and manage the transitions between different operational states. Each state is represented as a class that defines specific behavior and controls transitions to other states.
At the center of this architecture is the UciTango
class, which functions as the Context for the State Design Pattern. It holds the current state (currentState
) and enables state transitions by invoking the changeState()
method. The various state classes (WaitCmdUciState
, ReadyState
, WaitCmdGoState
, SearchingState
, and EndState
) implement the UCIEngine
interface, defining the behavior for each specific phase of the UCI engine’s lifecycle. These states interact with each other and the Tango
class (responsible for core chess functionalities like searching and managing the game state).
UciTango
(Context)currentState
).changeState()
(package-private to ensure only state classes trigger transitions).UCIEngine
Implementations)Each state defines specific behavior for requests such as do_uci
, do_setOption
, do_go
, and more. They govern transitions to other states when specific events or commands are handled.
WaitCmdUciState
(Initial State)"uci"
command to initialize.RspId
) and transitions to ReadyState
after replying with RspUciOk
.ReadyState
setOption
, position
, and isReady
.WaitCmdGoState
when a position is set and the "go"
command is expected.EndState
when the "quit"
command is received.WaitCmdGoState
(Transitions From ReadyState
)"go"
command."go"
command using the ReqGoExecutor
, which starts a search in the Tango
instance."go"
, switches to the SearchingState
.SearchingState
newGame
, position
, or a second go
) are restricted."info"
messages (RspInfo
) during the search process.ReadyState
after sending the result (RspBestMove
).EndState
if a "quit"
command is received during a search.EndState
(Terminal State)"quit"
command.Below is an outline of how states interact and transition:
WaitCmdUciState
."uci"
command, identifies itself and transitions to ReadyState
.ReadyState
, the engine can receive commands such as "setOption"
, "position"
, and "newGame"
."position"
command is received, it transitions to WaitCmdGoState
.WaitCmdGoState
, the "go"
command triggers the search process, transitioning to SearchingState
.SearchingState
, the engine actively searches for the best move."info"
messages.ReadyState
."quit"
command is received, the engine transitions to the EndState
.UciTango
as the CoordinatorcurrentState
and delegates incoming commands to the appropriate state instance.changeState
.UCIEngine
and overrides methods to define behavior during that phase.UciTango.changeState
.Tango
Tango
to control chess-specific operations like starting/stopping searches or setting positions.ReadyState
sets the board position in Tango
.SearchingState
uses Tango
’s search capabilities and listens for search results.ReadyState
and WaitCmdGoState
reference each other to manage transitions.SearchingState
set up listeners (SearchListener
) to handle search events."uci"
WaitCmdUciState
processes the input and transitions to ReadyState
."position"
ReadyState
transitions to WaitCmdGoState
."go"
WaitCmdGoState
starts the search and transitions to SearchingState
.SearchingState
transitions back to ReadyState
with the best move."quit"
EndState
.By defining behavior in distinct state classes, the UCI engine achieves clear separation of concerns, dynamic behavior switching, and extensibility for future features.