Example #1
0
    def test_possible_moves(self):
        self.board.move_piece(Location.from_string("c1"), Location.from_string("d4"))
        test_moves = self.board.piece_at_square(Location.from_string("d4")).possible_moves(self.board)
        real_moves = ["d4e5", "d4f6", "d4g7", "d4c5", "d4b6", "d4a7", "d4e3", "d4c3"]

        for i, move in enumerate(test_moves):
            self.assertEqual(str(move), real_moves[i])
Example #2
0
    def test_possible_moves(self):
        self.board = Board([[None for _ in range(8)] for _ in range(8)])
        my_king = King(color.white, Location.from_string("f3"))
        self.board.place_piece_at_square(my_king, Location.from_string("f3"))
        moves = ['f3f4', 'f3g3', 'f3f2', 'f3e3', 'f3g4', 'f3e4', 'f3g2', 'f3e2']

        for i, move in enumerate(my_king.possible_moves(self.board)):
            self.assertEqual(move, converter.long_alg(moves[i], self.board))
Example #3
0
    def test_possible_moves(self):
        self.empty_pos.place_piece_at_square(
            Knight(color.white, Location.from_string("e4")),
            Location.from_string("e4"))
        knight = self.empty_pos.piece_at_square(Location.from_string("e4"))

        moves = knight.possible_moves(self.empty_pos)
        self.assertEqual(len(list(moves)), 8)
Example #4
0
    def test_place_piece_at_square(self):
        test = Board.init_default()
        pawn = Pawn(color.white, Location.from_string("e3"))

        test.position[2][4] = pawn

        self.board.place_piece_at_square(pawn, Location.from_string("e3"))

        self.assertEqual(self.board, test)
Example #5
0
 def test_incomplete_alg_piece_movement_with_file_specified_alt(self):
     self.assertEqual(
         converter.incomplete_alg("Ngf3", color.white, self.test_board),
         Move(
             end_loc=Location.from_string("f3"),
             piece=Knight(color.white, Location.from_string("g1")),
             status=notation_const.MOVEMENT,
             start_loc=Location.from_string("g1")
         )
     )
Example #6
0
 def test_incomplete_alg_pawn_movement(self):
     self.assertEqual(
         converter.incomplete_alg("e4", color.white, self.test_board),
         Move(
             end_loc=Location.from_string("e4"),
             piece=Pawn(color.white, Location.from_string("e4")),
             status=notation_const.MOVEMENT,
             start_loc=Location.from_string("e2")
         )
     )
Example #7
0
    def test_update_pawn_moves_one_step(self):
        pawn = self.board.piece_at_square(Location.from_string("e2"))
        self.board.update(converter.long_alg("e2e3", self.board))

        self.assertIsInstance(pawn, Pawn)
        self.assertEqual(
            self.board.piece_at_square(Location.from_string("e3")), pawn)
        self.assertIsNone(
            self.board.piece_at_square(Location.from_string("e2")))
        self.assertFalse(pawn.just_moved_two_steps)
Example #8
0
    def test_move_piece(self):
        test = Board.init_default()
        pawn = test.position[1][4]
        test.position[1][4] = None
        test.position[3][4] = pawn

        self.board.move_piece(Location.from_string("e2"),
                              Location.from_string("e4"))

        self.assertEqual(self.board, test)
Example #9
0
 def test_incomplete_alg_piece_capture(self):
     self.test_board.update(converter.short_alg("Nf3", color.white, self.test_board))
     self.test_board.update(converter.short_alg("e5", color.black, self.test_board))
     self.assertEqual(
         converter.incomplete_alg("Nxe5", color.white, self.test_board),
         Move(
             end_loc=Location.from_string("e5"),
             piece=Knight(color.white, Location.from_string("f3")),
             status=notation_const.CAPTURE,
             start_loc=Location.from_string("f3")
         )
     )
Example #10
0
 def test_incomplete_alg_pawn_promotion_with_capture(self):
     self.test_board.move_piece(Location.from_string("a2"), Location.from_string("a7"))
     self.assertEqual(
         converter.incomplete_alg("axb8=R", color.white, self.test_board),
         Move(
             end_loc=Location.from_string("b8"),
             piece=Pawn(color.white, Location.from_string("a7")),
             status=notation_const.CAPTURE_AND_PROMOTE,
             promoted_to_piece=Rook,
             start_loc=Location.from_string("a7")
         )
     )
Example #11
0
    def test_update_moves_king_side_castle(self):
        self.board.update(converter.short_alg("e4", color.white, self.board))
        self.board.update(converter.short_alg("Nf3", color.white, self.board))
        self.board.update(converter.short_alg("Be2", color.white, self.board))
        self.board.update(converter.short_alg("o-o", color.white, self.board))

        king = self.board.piece_at_square(Location.from_string("g1"))
        self.assertIsInstance(king, King)
        self.assertTrue(king.has_moved)

        rook = self.board.piece_at_square(Location.from_string("f1"))
        self.assertIsInstance(rook, Rook)
        self.assertTrue(rook.has_moved)
Example #12
0
    def test_kingside_castle(self):
        self.board.update(converter.short_alg("e4", color.white, self.board))
        self.board.update(converter.short_alg("Nf3", color.white, self.board))
        self.board.update(converter.short_alg("Be2", color.white, self.board))

        castle_move = Move(
            end_loc=Location.from_string("g1"),
            piece=King(color.white, Location.from_string("g1")),
            status=notation_const.KING_SIDE_CASTLE,
            start_loc=Location.from_string("e1")
        )

        self.assertEqual(
            list(self.board.get_king(color.white).add_castle(self.board))[0], castle_move)
Example #13
0
    def test_left_diagonal(self):
        self.board.update(converter.long_alg("b2b3", self.board))
        moves = list(self.board.piece_at_square(Location.from_string("c1")).possible_moves(self.board))

        self.assertEqual(len(moves), 2)
        self.assertEqual(moves[0], converter.long_alg("c1b2", self.board))
        self.assertEqual(moves[1], converter.long_alg("c1a3", self.board))
Example #14
0
    def test_find_piece(self):
        self.assertEqual(
            self.board.find_piece(Rook(color.white, Location(0, 0))),
            Location(0, 0))

        self.assertEqual(
            self.board.find_piece(Rook(color.black, Location(7, 0))),
            Location(7, 0))

        self.assertNotEqual(
            self.board.find_piece(Rook(color.black, Location(7, 0))),
            Location(3, 0))

        self.assertEqual(
            self.board.find_piece(Pawn(color.white, Location(0, 0))),
            Location.from_string("a2"))

        self.assertEqual(
            self.board.find_piece(Knight(color.white, Location(0, 0))),
            Location.from_string("b1"))
Example #15
0
 def test_incomplete_alg_pawn_promotion(self):
     self.test_board.move_piece(Location.from_string("a2"), Location.from_string("a7"))
     self.test_board.remove_piece_at_square(Location.from_string("a8"))
     self.assertEqual(
         converter.incomplete_alg("a8=Q", color.white, self.test_board),
         Move(
             end_loc=Location.from_string("a8"),
             piece=Pawn(color.white, Location.from_string("e7")),
             status=notation_const.PROMOTE,
             promoted_to_piece=Queen,
             start_loc=Location.from_string("a7")
         )
     )
Example #16
0
    def test_queenside_castle(self):

        self.board.remove_piece_at_square(Location.from_string("b1"))
        self.board.remove_piece_at_square(Location.from_string("c1"))
        self.board.remove_piece_at_square(Location.from_string("d1"))

        castle_move = Move(
            end_loc=Location.from_string("c1"),
            piece=King(color.white, Location.from_string("c1")),
            status=notation_const.QUEEN_SIDE_CASTLE,
            start_loc=Location.from_string("e1")
        )

        self.assertEqual(
            list(self.board.get_king(color.white).add_castle(self.board))[0], castle_move)
Example #17
0
    def test_in_check(self):
        self.board = Board([[None for _ in range(8)] for _ in range(8)])
        my_king = King(color.white, Location.from_string("f3"))
        self.board.place_piece_at_square(my_king, Location.from_string("f3"))
        self.board.place_piece_at_square(Rook(color.black, Location.from_string("f1")), Location.from_string("f1"))

        print(self.board.piece_at_square(Location.from_string("f1")).color)

        print(self.board)
        print(my_king.color)
        print(color.white == color.black)
        self.assertTrue(my_king.in_check(self.board))

        self.board = Board.init_default()
        self.board.update(converter.long_alg("f2f3", self.board))
        self.board.move_piece(Location.from_string("d8"), Location.from_string("g3"))

        self.assertTrue(self.board.get_king(color.white).in_check(self.board))
Example #18
0
 def setUp(self):
     self.test_board = Board.init_default()
     self.e_four_move = Move(end_loc=Location.from_string("e4"),
                             piece=Pawn(color.white, Location.from_string("e4")),
                             status=notation_const.MOVEMENT,
                             start_loc=Location.from_string("e2"))
Example #19
0
    def test_capture(self):
        self.board.move_piece(Location.from_string("g1"), Location.from_string("g7"))
        moves = list(self.board.piece_at_square(Location.from_string("f8")).possible_moves(self.board))

        self.assertEqual(len(moves), 1)
        self.assertEqual(moves[0], converter.long_alg("f8g7", self.board))
Example #20
0
    def test_in_check_as_result(self):
        self.assertFalse(self.board.get_king(color.white).in_check_as_result(self.board,
                                                 converter.long_alg("e2e4", self.board)))

        self.board.move_piece(Location.from_string("e1"), Location.from_string("e3"))
        self.board.move_piece(Location.from_string("e8"), Location.from_string("e5"))
Example #21
0
 def test_no_possible_moves(self):
     self.assertEqual(len(list(self.board.piece_at_square(Location.from_string("c1"))
                          .possible_moves(self.board))), 0)