def test_promotion(): g = Game() g.clear() g.add_piece(Pawn("WHITE"), ("B", 7)) g.add_piece(King("WHITE"), ("E", 1)) g.add_piece(Pawn("BLACK"), ("G", 2)) g.add_piece(King("BLACK"), ("E", 7)) g.update_all_pieces() assert (g.is_legal_move("WHITE", ("B", 7), ("B", 8))) g.move(("B", 7), ("B", 8)) assert (g.board.piece_at(("B", 8)).piece_type == "Queen") g.update_all_pieces() assert (g.is_legal_move("BLACK", ("G", 2), ("G", 1))) g.move(("G", 2), ("G", 1)) assert (g.board.piece_at(("G", 1)).piece_type == "Queen")
def test_no_checkmate_king_takes(): """ Problem seen when game declared checkmate even though king could have taken the piece threatening it - check this is fixed. """ g = Game() g.clear() g.add_piece(King("BLACK"), ("H", 8)) g.add_piece(King("WHITE"), ("E", 1)) g.add_piece(Pawn("WHITE"), ("E", 2)) g.add_piece(Pawn("WHITE"), ("F", 2)) g.add_piece(Queen("BLACK"), ("D", 5)) g.update_all_pieces() g.next_player_turn() g.move(("D", 5), ("D", 1)) assert (not g.is_checkmate("WHITE"))
def test_castling_king_side(): g = Game() g.move(("G", 1), ("H", 3)) g.move(("G", 8), ("H", 6)) g.move(("E", 2), ("D", 3)) g.move(("E", 7), ("D", 6)) g.move(("F", 1), ("E", 2)) g.move(("F", 8), ("E", 7)) g.update_all_pieces() assert (g.is_legal_move("WHITE", ("E", 1), ("G", 1))) g.move(("E", 1), ("G", 1)) assert (not g.board.is_empty(("G", 1))) assert (g.board.piece_at(("G", 1)).piece_type == "King") assert (not g.board.is_empty(("F", 1))) assert (g.board.piece_at(("F", 1)).piece_type == "Rook") ## Now have black do the same assert (g.is_legal_move("BLACK", ("E", 8), ("G", 8))) g.move(("E", 8), ("G", 8)) assert (not g.board.is_empty(("G", 8))) assert (g.board.piece_at(("G", 8)).piece_type == "King") assert (not g.board.is_empty(("F", 8))) assert (g.board.piece_at(("F", 8)).piece_type == "Rook")
def test_check(): g = Game() g.move(("A", 2), ("D", 7)) assert (g.is_check("BLACK"))
def test_take(): g = Game() g.move(("A", 2), ("A", 7)) assert (len(g.board.pieces) == 31) assert (g.board.piece_at(("A", 7)).colour == "WHITE")
def test_white_pawn_move(): g = Game() g.move(("A", 2), ("A", 4)) assert (g.board.is_empty(("A", 2))) assert (g.board.piece_at(("A", 4)).has_moved == True) assert (g.next_to_play == "BLACK")
def test_cant_castle_after_moving(): g = Game() g.move(("G", 1), ("H", 3)) g.move(("G", 8), ("H", 6)) g.move(("E", 2), ("D", 3)) g.move(("E", 7), ("D", 6)) g.move(("F", 1), ("E", 2)) g.move(("F", 8), ("E", 7)) ## move white king g.move(("E", 1), ("F", 1)) ## and black rook g.move(("H", 8), ("G", 8)) ## and move both back again g.move(("F", 1), ("E", 1)) g.move(("G", 8), ("H", 8)) g.update_all_pieces() assert (not g.is_legal_move("WHITE", ("E", 1), ("G", 1))) ## Now have black do the same assert (not g.is_legal_move("BLACK", ("E", 8), ("G", 8)))