Beispiel #1
0
class TestFoolsMate(unittest.TestCase):
    def setUp(self):
        self.board = Board()
        self.board.move_if_legal("f2", "f3", "white")
        self.board.move_if_legal("e7", "e5", "black")
        self.board.move_if_legal("g2", "g4", "white")
        self.board.move_if_legal("d8", "h4", "black")

    def test_countLegalMovesAfterFoolsMate(self):
        self.assertEqual(0, self.board.count_legal_moves("white"))

    def test_countLegalMoves_inInitialPosition(self):
        self.board = Board()
        self.assertEqual(20, self.board.count_legal_moves("white"))
        self.assertEqual(20, self.board.count_legal_moves("black"))

    def test_checkMate_afterFoolsMate(self):
        self.assertTrue(self.board.is_checkmated("white"))
        self.assertFalse(self.board.is_checkmated("black"))

    def test_gameEndedWithBlackWin_afterWhiteIsCheckmated(self):
        self.assertTrue(self.board.game_ended)
        self.assertEqual("black wins", self.board.result)
Beispiel #2
0
class TestLegalMoves(unittest.TestCase):
    def setUp(self):
        self.board = Board()

    def test_rejectMoveIfIllegal(self):
        self.board.move_if_legal("e2", "d3", "white")
        self.assertEqual("white pawn", self.board.e2)
        self.assertEqual("empty", self.board.d3)

    def test_movePawnIfLegal(self):
        self.board.move_if_legal("e2", "e3", "white")
        self.assertEqual("empty", self.board.e2)
        self.assertEqual("white pawn", self.board.e3)

    def test_moveBishopIfLegal(self):
        self.board.move_if_legal("e2", "e4", "white")
        self.board.move_if_legal("f1", "c4", "white")
        self.assertEqual("empty", self.board.f1)
        self.assertEqual("white bishop", self.board.c4)
Beispiel #3
0
from src.Chess2 import Board

board = Board()
color_to_move = "white"


def switch_turn(color):
    if color == "white":
        return "black"
    else:
        return "white"


print(board.display())
while not board.game_ended:

    move = input("Enter move in the form <from_square>,<to_square>:")
    from_square = move.split(",")[0]
    to_square = move.split(",")[1]
    if board.move_is_legal(from_square, to_square, color_to_move):
        board.move_if_legal(from_square, to_square, color_to_move)
        color_to_move = switch_turn(color_to_move)
        print("")
        print(board.display())
    else:
        print("illegal move!")
else:
    print(board.result)
Beispiel #4
0
class TestCastlingMove(unittest.TestCase):
    def setUp(self):
        self.board = Board()

    def test_castlingKingsideFieldIsReachable(self):
        self.board.free_square("f1")
        self.board.free_square("g1")
        self.assertTrue(self.board.is_reachable("e1", "g1", "white"))
        self.board.free_square("f8")
        self.board.free_square("g8")
        self.assertTrue(self.board.is_reachable("e8", "g8", "black"))

    def test_castlingKingsideIsLegal(self):
        self.board.free_square("f1")
        self.board.free_square("g1")
        self.assertTrue(self.board.move_is_legal("e1", "g1", "white"))
        self.board.free_square("f8")
        self.board.free_square("g8")
        self.assertTrue(self.board.move_is_legal("e8", "g8", "black"))

    def test_castlingKingsideFieldIsNotReachable_ifPathNotClear(self):
        self.assertFalse(self.board.is_reachable("e1", "g1", "white"))
        self.assertFalse(self.board.move_is_legal("e8", "g8", "black"))

    def test_castlingQueensideFieldIsReachable(self):
        self.board.free_square("d1")
        self.board.free_square("c1")
        self.board.free_square("b1")
        self.assertTrue(self.board.is_reachable("e1", "c1", "white"))
        self.board.free_square("d8")
        self.board.free_square("c8")
        self.board.free_square("b8")
        self.assertTrue(self.board.is_reachable("e8", "c8", "black"))

    def test_castlingQueensideIsLegal(self):
        self.board.free_square("d1")
        self.board.free_square("c1")
        self.board.free_square("b1")
        self.assertTrue(self.board.move_is_legal("e1", "c1", "white"))
        self.board.free_square("d8")
        self.board.free_square("c8")
        self.board.free_square("b8")
        self.assertTrue(self.board.move_is_legal("e8", "c8", "black"))

    def test_castlingQueensideFieldIsNotReachable_ifPathNotClear(self):
        self.assertFalse(self.board.is_reachable("e1", "c1", "white"))
        self.assertFalse(self.board.move_is_legal("e8", "c8", "black"))

    def test_piecesOnTargetSquares_afterWhiteCastlingKingside(self):
        self.board.free_square("f1")
        self.board.free_square("g1")
        self.board.move_if_legal("e1", "g1", "white")
        self.assertEqual("white king", self.board.g1)
        self.assertEqual("white rook", self.board.f1)

    def test_piecesOnTargetSquares_afterBlackCastlingKingside(self):
        self.board.free_square("f8")
        self.board.free_square("g8")
        self.board.move_if_legal("e8", "g8", "black")
        self.assertEqual("black king", self.board.g8)
        self.assertEqual("black rook", self.board.f8)

    def test_piecesOnTargetSquares_afterWhiteCastlingQueenside(self):
        self.board.free_square("d1")
        self.board.free_square("c1")
        self.board.free_square("cb")
        self.board.move_if_legal("e1", "c1", "white")
        self.assertEqual("white king", self.board.c1)
        self.assertEqual("white rook", self.board.d1)

    def test_piecesOnTargetSquares_afterBlackCastlingQueenside(self):
        self.board.free_square("d8")
        self.board.free_square("c8")
        self.board.free_square("b8")
        self.board.move_if_legal("e8", "c8", "black")
        self.assertEqual("black king", self.board.c8)
        self.assertEqual("black rook", self.board.d8)
Beispiel #5
0
class TestCastlingPiecesHaveMoved(unittest.TestCase):
    def setUp(self):
        self.board = Board()

    def test_kingHasNotMoved_ifInitialBoard(self):
        self.assertFalse(self.board.white_king_has_moved)
        self.assertFalse(self.board.black_king_has_moved)

    def test_whiteKingHasMoved(self):
        self.board.free_square("e2")
        self.board.move_if_legal("e1", "e2", "white")
        self.assertTrue(self.board.white_king_has_moved)

    def test_blackKingHasMoved(self):
        self.board.free_square("e7")
        self.board.move_if_legal("e8", "e7", "black")
        self.assertTrue(self.board.black_king_has_moved)

    def test_RooksHaveNotMoved_ifInitialBoard(self):
        self.assertFalse(self.board.a1_rook_has_moved)
        self.assertFalse(self.board.h1_rook_has_moved)
        self.assertFalse(self.board.a8_rook_has_moved)
        self.assertFalse(self.board.h8_rook_has_moved)

    def test_a1RookHasMoved(self):
        self.board.free_square("a2")
        self.board.move_if_legal("a1", "a3", "white")
        self.assertTrue(self.board.a1_rook_has_moved)

    def test_h1RookHasMoved(self):
        self.board.free_square("h2")
        self.board.move_if_legal("h1", "h3", "white")
        self.assertTrue(self.board.h1_rook_has_moved)

    def test_a8RookHasMoved(self):
        self.board.free_square("a7")
        self.board.move_if_legal("a8", "a3", "black")
        self.assertTrue(self.board.a8_rook_has_moved)

    def test_h8RookHasMoved(self):
        self.board.free_square("h7")
        self.board.move_if_legal("h8", "h3", "black")
        self.assertTrue(self.board.h8_rook_has_moved)

    def test_moveIsWhiteCastlingKingside(self):
        self.assertTrue(self.board.move_is_white_castling_kingside("e1", "g1"))

    def test_moveIsBlackCastlingKingside(self):
        self.assertTrue(self.board.move_is_black_castling_kingside("e8", "g8"))

    def test_moveIsNotBlackCastlingKingside_ifKingMoved(self):
        self.board.free_square("e7")
        self.board.move_if_legal("e8", "e7", "black")
        self.board.move_if_legal("e7", "e8", "black")
        self.assertFalse(self.board.move_is_black_castling_kingside(
            "e8", "g8"))

    def test_moveIsNotBlackCastlingKingside_ifRookMoved(self):
        self.board.free_square("h7")
        self.board.move_if_legal("h8", "h7", "black")
        self.board.move_if_legal("h7", "h8", "black")
        self.assertFalse(self.board.move_is_black_castling_kingside(
            "e8", "g8"))

    def test_moveIsWhiteCastlingQueenside(self):
        self.assertTrue(self.board.move_is_white_castling_queenside(
            "e1", "c1"))

    def test_moveIsBlackCastlingQueenside(self):
        self.assertTrue(self.board.move_is_black_castling_queenside(
            "e8", "c8"))

    def test_moveIsNotBlackCastlingQueenside_ifKingMoved(self):
        self.board.free_square("e7")
        self.board.move_if_legal("e8", "e7", "black")
        self.board.move_if_legal("e8", "e7", "black")
        self.assertFalse(
            self.board.move_is_black_castling_queenside("e8", "c8"))

    def test_moveIsNotBlackCastlingQueenside_ifRookMoved(self):
        self.board.free_square("a7")
        self.board.move_if_legal("a8", "a7", "black")
        self.board.move_if_legal("a8", "a7", "black")
        self.assertFalse(
            self.board.move_is_black_castling_queenside("e8", "c8"))

    def test_moveIsCastlingMove(self):
        self.assertTrue(self.board.move_is_castling("e1", "g1", "white"))
        self.assertTrue(self.board.move_is_castling("e1", "c1", "white"))
        self.assertTrue(self.board.move_is_castling("e8", "g8", "black"))
        self.assertTrue(self.board.move_is_castling("e8", "c8", "black"))
        self.assertFalse(self.board.move_is_castling("e1", "g1", "black"))
        self.assertFalse(self.board.move_is_castling("e1", "c1", "black"))
        self.assertFalse(self.board.move_is_castling("e8", "g8", "white"))
        self.assertFalse(self.board.move_is_castling("e8", "c8", "white"))
Beispiel #6
0
class SheperdsMate(unittest.TestCase):
    def setUp(self):
        self.board = Board()
        self.board.move_if_legal("e2", "e4", "white")
        self.board.move_if_legal("e7", "e5", "black")
        self.board.move_if_legal("f1", "c4", "white")
        self.board.move_if_legal("b8", "c6", "black")
        self.board.move_if_legal("d1", "h5", "white")
        self.board.move_if_legal("g8", "c6", "black")
        self.board.move_if_legal("h5", "f7", "white")

    def test_gameEnds_afterSheperdsMate(self):
        self.assertTrue(self.board.game_ended)

    def test_whiteWins_afterSheperdsMate(self):
        self.assertEqual("white wins", self.board.result)

    def test_pieceOnCorrectSquares_afterFoolsMate(self):
        self.assertEqual(self.board.a1, "white rook")
        self.assertEqual(self.board.a2, "white pawn")
        self.assertEqual(self.board.a3, "empty")
        self.assertEqual(self.board.a4, "empty")
        self.assertEqual(self.board.a5, "empty")
        self.assertEqual(self.board.a6, "empty")
        self.assertEqual(self.board.a7, "black pawn")
        self.assertEqual(self.board.a8, "black rook")
        self.assertEqual(self.board.b1, "white knight")
        self.assertEqual(self.board.b2, "white pawn")
        self.assertEqual(self.board.b3, "empty")
        self.assertEqual(self.board.b4, "empty")
        self.assertEqual(self.board.b5, "empty")
        self.assertEqual(self.board.b6, "empty")
        self.assertEqual(self.board.b7, "black pawn")
        self.assertEqual(self.board.b8, "empty")
        self.assertEqual(self.board.c1, "white bishop")
        self.assertEqual(self.board.c2, "white pawn")
        self.assertEqual(self.board.c3, "empty")
        self.assertEqual(self.board.c4, "white bishop")
        self.assertEqual(self.board.c5, "empty")
        self.assertEqual(self.board.c6, "black knight")
        self.assertEqual(self.board.c7, "black pawn")
        self.assertEqual(self.board.c8, "black bishop")
        self.assertEqual(self.board.d1, "empty")
        self.assertEqual(self.board.d2, "white pawn")
        self.assertEqual(self.board.d3, "empty")
        self.assertEqual(self.board.d4, "empty")
        self.assertEqual(self.board.d5, "empty")
        self.assertEqual(self.board.d6, "empty")
        self.assertEqual(self.board.d7, "black pawn")
        self.assertEqual(self.board.d8, "black queen")
        self.assertEqual(self.board.e1, "white king")
        self.assertEqual(self.board.e2, "empty")
        self.assertEqual(self.board.e3, "empty")
        self.assertEqual(self.board.e4, "white pawn")
        self.assertEqual(self.board.e5, "black pawn")
        self.assertEqual(self.board.e6, "empty")
        self.assertEqual(self.board.e7, "empty")
        self.assertEqual(self.board.e8, "black king")
        self.assertEqual(self.board.f1, "empty")
        self.assertEqual(self.board.f2, "white pawn")
        self.assertEqual(self.board.f3, "empty")
        self.assertEqual(self.board.f4, "empty")
        self.assertEqual(self.board.f5, "empty")
        self.assertEqual(self.board.f6, "empty")
        self.assertEqual(self.board.f7, "white queen")
        self.assertEqual(self.board.f8, "black bishop")
        self.assertEqual(self.board.g1, "white knight")
        self.assertEqual(self.board.g2, "white pawn")
        self.assertEqual(self.board.g3, "empty")
        self.assertEqual(self.board.g4, "empty")
        self.assertEqual(self.board.g5, "empty")
        self.assertEqual(self.board.g6, "empty")
        self.assertEqual(self.board.g7, "black pawn")
        self.assertEqual(self.board.g8, "black knight")
        self.assertEqual(self.board.h1, "white rook")
        self.assertEqual(self.board.h2, "white pawn")
        self.assertEqual(self.board.h3, "empty")
        self.assertEqual(self.board.h4, "empty")
        self.assertEqual(self.board.h5, "empty")
        self.assertEqual(self.board.h6, "empty")
        self.assertEqual(self.board.h7, "black pawn")
        self.assertEqual(self.board.h8, "black rook")