feat(chess_game): ChessGame core (try_move, history, checkmate)
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import pytest
|
||||
|
||||
from ai_chess.chess_game import ChessGame, MoveResult
|
||||
from ai_chess.sides import Side
|
||||
|
||||
|
||||
def test_new_game_state():
|
||||
g = ChessGame(game_id="g1")
|
||||
state = g.get_state()
|
||||
assert state["side_to_move"] == "white"
|
||||
assert state["history"] == []
|
||||
assert state["status"]["result"] is None
|
||||
assert "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR" in state["fen"]
|
||||
|
||||
|
||||
def test_legal_move_white_san():
|
||||
g = ChessGame(game_id="g1")
|
||||
r = g.try_move(Side.WHITE, "e4")
|
||||
assert r.ok is True
|
||||
assert r.san == "e4"
|
||||
assert r.uci == "e2e4"
|
||||
assert g.get_state()["side_to_move"] == "black"
|
||||
assert g.get_state()["history"][0]["san"] == "e4"
|
||||
|
||||
|
||||
def test_legal_move_uci():
|
||||
g = ChessGame(game_id="g1")
|
||||
r = g.try_move(Side.WHITE, "e2e4")
|
||||
assert r.ok is True
|
||||
assert r.san == "e4"
|
||||
|
||||
|
||||
def test_not_your_turn():
|
||||
g = ChessGame(game_id="g1")
|
||||
r = g.try_move(Side.BLACK, "e5")
|
||||
assert r.ok is False
|
||||
assert r.reason == "not_your_turn"
|
||||
|
||||
|
||||
def test_illegal_move_default_rejected():
|
||||
g = ChessGame(game_id="g1")
|
||||
r = g.try_move(Side.WHITE, "e2e5") # pawn can't move two from e2 to e5
|
||||
assert r.ok is False
|
||||
assert r.reason == "illegal_move"
|
||||
|
||||
|
||||
def test_history_contains_comment():
|
||||
g = ChessGame(game_id="g1")
|
||||
g.try_move(Side.WHITE, "e4", comment="готовься")
|
||||
h = g.get_state()["history"]
|
||||
assert h[0]["san"] == "e4"
|
||||
assert h[0]["comment"] == "готовься"
|
||||
|
||||
|
||||
def test_game_over_checkmate_fools_mate():
|
||||
g = ChessGame(game_id="g1")
|
||||
moves = [
|
||||
(Side.WHITE, "f3"),
|
||||
(Side.BLACK, "e5"),
|
||||
(Side.WHITE, "g4"),
|
||||
(Side.BLACK, "Qh4"),
|
||||
]
|
||||
for side, mv in moves:
|
||||
r = g.try_move(side, mv)
|
||||
assert r.ok, (mv, r)
|
||||
st = g.get_state()
|
||||
assert st["status"]["result"] == "0-1"
|
||||
assert st["status"]["termination"] == "checkmate"
|
||||
|
||||
|
||||
def test_move_after_game_over():
|
||||
g = ChessGame(game_id="g1")
|
||||
for side, mv in [(Side.WHITE, "f3"), (Side.BLACK, "e5"),
|
||||
(Side.WHITE, "g4"), (Side.BLACK, "Qh4")]:
|
||||
g.try_move(side, mv)
|
||||
r = g.try_move(Side.WHITE, "a3")
|
||||
assert r.ok is False
|
||||
assert r.reason == "game_over"
|
||||
Reference in New Issue
Block a user