Ejemplo n.º 1
0
class TestKingInCheck(unittest.TestCase):
    def setUp(self):
        self.board = Board()
        self.board.put("white king", "a3")
        self.board.put("black king", "h6")
        self.board.free_square("e1")
        self.board.free_square("e8")

    def test_kingInCheck_ifMovedIntoCheck(self):
        self.board.put("white rook", "a5")
        self.board.put("black rook", "h4")
        self.assertTrue(
            self.board.king_in_check_after_move("a3", "a4", "white"))
        self.assertTrue(
            self.board.king_in_check_after_move("h6", "h5", "black"))

    def test_moveIllegal_ifKingInCheck_afterMove(self):
        self.board.put("white rook", "a5")
        self.board.put("black rook", "h4")
        self.assertFalse(self.board.move_is_legal("a3", "a4", "white"))
        self.assertFalse(self.board.move_is_legal("h6", "h5", "black"))

    def test_getAllReachableToSquaresFromSquareX(self):
        self.assertSetEqual({"e3", "e4"},
                            self.board.get_legal_to_squares("e2"))
        self.assertSetEqual({"e5", "e6"},
                            self.board.get_legal_to_squares("e7"))
        self.assertSetEqual(set(), self.board.get_legal_to_squares("c1"))

    def test_getAllReachableToSquaresForBlockedPawn(self):
        self.board.put("black pawn", "a4")
        self.assertSetEqual(set(), self.board.get_legal_to_squares("a4"))

    def test_kingInCheck_ifAttacked(self):
        self.board.put("black rook", "h3")
        self.assertTrue(self.board.king_in_check("white"))
        self.assertFalse(self.board.king_in_check("black"))

    def test_kingNotInCheck_ifInFrontOfPawn(self):
        self.board.put("black pawn", "a4")
        self.assertFalse(self.board.king_in_check("white"))

    def test_kingNotInCheck_ifTwoStepsFromPawnOnInitialSquare(self):
        self.board.move("a3", "e5")
        self.assertFalse(self.board.king_in_check("white"))
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)