def test_rook_isvalid(self): '''Rook move validation''' testdata = {Rook('white'): [(0, 0), (0, 1), True], Rook('white'): [(0, 0), (1, 1), False], } for piece, (start, end, ans) in testdata.items(): result = piece.isvalid(start, end) self.assertEqual(result, ans)
def testRookClass(self): # test moves array self.assertEqual(Rook.moves, [[1, 0], [0, 1], [-1, 0], [0, -1]]) r = Rook() l = [found_position for found_position in r.possible_positions("D4")] self.assertEqual(l, [ 'E4', 'F4', 'G4', 'H4', 'D5', 'D6', 'D7', 'D8', 'C4', 'B4', 'A4', 'D3', 'D2', 'D1' ])
def test_no_jump(self): '''Pieces cannot jump over pieces of same colour''' game = Board(debug=True) game.add((4, 1), King('white')) game.add((4, 4), Rook('white')) game.add((4, 6), King('black')) game.add((4, 3), Rook('black')) game.turn, game.other_turn = 'white', 'black' self.assertFalse(game.valid_move((4, 4), (4, 0))) self.assertFalse(game.valid_move((4, 3), (4, 7)))
def test_winner(self): '''Identify game winner''' game = gameSetupWithKings() game.add((4, 5), Rook('white')) game.turn, game.other_turn = 'white', 'black' game.update((4, 5), (4, 7)) self.assertEqual(game.winner, 'white') game = gameSetupWithKings() game.add((4, 2), Rook('black')) game.turn, game.other_turn = 'black', 'white' game.update((4, 2), (4, 0)) self.assertEqual(game.winner, 'black')
def test_next_move_uncheck(self): game = gameSetupWithKings() game.add((0, 1), Rook('black')) game.turn = 'white' game.update((0, 1), (0, 0)) # white king is in check, next move must bring it out of check self.assertFalse(game.valid_move((4, 0), (3, 0))) self.assertTrue(game.valid_move((4, 0), (4, 1))) game = gameSetupWithKings() game.add((0, 6), Rook('white')) game.turn = 'black' game.update((0, 6), (0, 7)) # black king is in check, next move must bring it out of check self.assertFalse(game.valid_move((4, 7), (3, 7))) self.assertTrue(game.valid_move((4, 7), (4, 6)))
def test_rook_castling(self): '''Rook can castle''' for col in [0, 7]: game = gameSetupWithKings() game.add((col, 0), Rook('white')) game.turn, game.other_turn = 'white', 'black' game.update((4, 0), (2, 0)) self.assertEqual(game.get_piece((3, 0)).name, 'rook') self.assertEqual(game.get_piece((2, 0)).name, 'king') game = gameSetupWithKings() game.add((col, 7), Rook('black')) game.turn, game.other_turn = 'black', 'white' game.update((4, 7), (6, 7)) self.assertEqual(game.get_piece((5, 7)).name, 'rook') self.assertEqual(game.get_piece((6, 7)).name, 'king')
def test_rook_castling(self): '''Rook can castle''' for colour, row in zip(['white', 'black'], [0, 7]): # TODO: make castling a King move game = gameSetupWithKings() game.add((0, row), Rook(colour)) game.turn = colour game.update((4, row), (2, row)) self.assertEqual(game.get_piece((3, row)).name, 'rook') self.assertEqual(game.get_piece((2, row)).name, 'king') game = gameSetupWithKings() game.add((7, row), Rook(colour)) game.turn = colour game.update((4, row), (6, row)) self.assertEqual(game.get_piece((5, row)).name, 'rook') self.assertEqual(game.get_piece((6, row)).name, 'king')