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])
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))
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)
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)
def testStr(self): self.assertEqual(str(self.start_specified), "f4a3") self.white_pawn_move = Move(Location(7, 0), piece=self.white_pawn, status=notation_const.MOVEMENT, start_loc=Location(6, 0), promoted_to_piece=Queen(color.white, Location(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)
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)
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") ) )
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") ) )
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") ) )
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") ) )
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)
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)
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))
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"))
def test_piece_at_square(self): self.assertEqual(self.board.piece_at_square(Location(0, 0)), Rook(color.white, Location(0, 0))) self.assertEqual(self.board.piece_at_square(Location(1, 0)), Pawn(color.white, Location(1, 0))) self.assertEqual(self.board.piece_at_square(Location(0, 1)), Knight(color.white, Location(0, 1)))
def setUp(self): self.white_pawn = Pawn(color.white, Location(1, 0)) self.black_pawn = Pawn(color.black, Location(1, 0)) self.white_pawn_move = Move(Location(2, 0), piece=self.white_pawn, status=notation_const.MOVEMENT, start_loc=Location(1, 0)) self.start_specified = Move(Location(2, 0), piece=self.white_pawn, status=notation_const.MOVEMENT, start_loc=Location(3, 5))
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") ) )
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)
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))
def testShiftDownRight(self): self.assertEqual(Location(5, 3).shift_down_right(), Location(4, 4))
def testShiftUpLeft(self): self.assertEqual(Location(1, 2).shift_up_left(), Location(2, 1))
def testShiftUpRight(self): self.assertEqual(Location(3, 4).shift_up_right(), Location(4, 5))
def testShiftLeft(self): self.assertEqual(Location(3, 4).shift_left(), Location(3, 3)) self.assertEqual(Location(0, 2).shift_left(), Location(0, 1))
def testShiftRight(self): self.assertEqual(Location(3, 4).shift_right(), Location(3, 5)) self.assertEqual(Location(0, 2).shift_right(), Location(0, 3))
def testShiftDown(self): self.assertEqual(Location(3, 4).shift_down(), Location(2, 4)) self.assertEqual(Location(1, 2).shift_down(), Location(0, 2))
def testShiftUp(self): self.assertEqual(Location(3, 4).shift_up(), Location(4, 4)) self.assertEqual(Location(0, 2).shift_up(), Location(1, 2))
def testShiftDownLeft(self): self.assertEqual(Location(1, 1).shift_down_left(), Location(0, 0))
def testEquals(self): self.assertEqual(Location(2, 3), Location(2, 3)) self.assertEqual(Location(7, 6), Location(7, 6)) self.assertNotEqual(Location(4, 5), Location(5, 4))
def testStr(self): self.assertEqual(str(Location(3, 4)), "e4") self.assertEqual(str(Location(0, 0)), "a1") self.assertEqual(str(Location(7, 7)), "h8")