ChessInterface
In addition to the methods and attributes described in the overview, this object provides the methods below.
Methods
| Method | Description |
|---|---|
ChessInterface.make_move(move) |
move is a string written in UCI, like 'e2e4'. Plays it. Call it from on_my_turn, once: a move the engine did not ask for - a second one on the same board, or one sent from on_update or setup - forfeits the game, and sending none leaves the engine waiting until your clock runs out. A move that is not one of ChessGameState.all_valid_moves forfeits the game as well, with the reason on your console. |
ChessInterface.make_pseudo_move(state, move) |
state is a ChessGameState object and move is a string written in UCI. Returns the new ChessGameState that results if you played move on state, without playing it. Raises ValueError if the move is invalid, and TypeError, before anything is asked, if state is not a ChessGameState or move is not a string. Note that on the returned state the values for ChessGameState.clocks will be -1, and clock_ends and time_left will be None. Safe to ask at any moment, your opponent's turn included: a board that arrives while the answer is on its way is kept, and reaches on_update in its turn. |
def on_my_turn(state):
ChessInterface.log(state.board['a4':'h1'])
# > ------------------------
# 4 │ · · · · · · · · │
# 3 │ · · · · · · · · │
# 2 │ P P P P P P P P │
# 1 │ R N B Q K B N R │
# ------------------------
# a b c d e f g h
new_state = ChessInterface.make_pseudo_move(state, 'd2d4')
ChessInterface.log(new_state.board['a4':'h1'])
# > ------------------------
# 4 │ · · · P · · · · │
# 3 │ · · · · · · · · │
# 2 │ P P P · P P P P │
# 1 │ R N B Q K B N R │
# ------------------------
# a b c d e f g h
# an invalid move raises instead of answering
try:
ChessInterface.make_pseudo_move(state, 'a2a5')
except ValueError as problem:
ChessInterface.log(problem)
# > White cannot play the move "a2a5" on "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1": that piece cannot make it
ChessInterface.make_move('d2d4')