def set_up_game(self): """Create the pieces for a new game, store them in two lists white pieces and black pieces""" self.black_king = King(self, BLACK, Position(4, 7)) self.white_king = King(self, WHITE, Position(4, 0)) self.white_pieces = [self.white_king] self.black_pieces = [self.black_king] self.white_pieces.append(Queen(self, WHITE, Position(3, 0))) self.black_pieces.append(Queen(self, BLACK, Position(3, 7))) self.black_pieces.append(Rook(self, BLACK, Position(0, 7))) self.black_pieces.append(Rook(self, BLACK, Position(7, 7))) self.white_pieces.append(Rook(self, WHITE, Position(7, 0))) self.white_pieces.append(Rook(self, WHITE, Position(0, 0))) self.black_pieces.append(Bishop(self, BLACK, Position(2, 7))) self.black_pieces.append(Bishop(self, BLACK, Position(5, 7))) self.white_pieces.append(Bishop(self, WHITE, Position(2, 0))) self.white_pieces.append(Bishop(self, WHITE, Position(5, 0))) self.black_pieces.append(Knight(self, BLACK, Position(1, 7))) self.black_pieces.append(Knight(self, BLACK, Position(6, 7))) self.white_pieces.append(Knight(self, WHITE, Position(1, 0))) self.white_pieces.append(Knight(self, WHITE, Position(6, 0))) for i in range(0, 8): self.white_pieces.append(Pawn(self, WHITE, Position(i, 1))) self.black_pieces.append(Pawn(self, BLACK, Position(i, 6)))
def test_knight_moves_enemy_and_friendly(self): """Test valid moves for a knight with enemy and friendly pieces in attacked squares""" white_knight = Knight(None, WHITE, Position(3, 3)) black_knight = Knight(None, BLACK, Position(2, 1)) white_knight2 = Knight(None, WHITE, Position(5, 4)) white_knight3 = Knight(None, WHITE, Position(5, 2)) test_board = { 'white_pieces': [white_knight, white_knight2, white_knight3], 'black_pieces': [black_knight] } board = set_up_test_board(new_game=False, test_board=test_board) self.assertEqual(len(white_knight.moves), 6) moves = [ Position(1, 2), Position(1, 4), Position(4, 1), Position(4, 5), Position(2, 5), Position(2, 1) ] for m in moves: self.assertIn(m, white_knight.moves)
def test_get_moves_no_special(self, board, try_move): invalid_position = Position(3, 3) def my_side_effect(*args): if args[1] == invalid_position: return False return True try_move.side_effect = my_side_effect game = Game() game._board = board rook = Rook(board, constants.WHITE, Position(4, 4)) bishop = Bishop(board, constants.WHITE, Position(1, 1)) knight = Knight(board, constants.WHITE, Position(7, 0)) mocked_board_moves = { rook: { constants.MOVES: [Position(5, 4), Position(3, 4)], constants.SPECIAL_MOVES: {} }, bishop: { constants.MOVES: [Position(2, 2), invalid_position], constants.SPECIAL_MOVES: {} }, knight: { constants.MOVES: [], constants.SPECIAL_MOVES: {} } } valid_moves = { rook: { constants.MOVES: [Position(5, 4), Position(3, 4)], constants.SPECIAL_MOVES: {} }, bishop: { constants.MOVES: [Position(2, 2)], constants.SPECIAL_MOVES: {} }, knight: { constants.MOVES: [], constants.SPECIAL_MOVES: {} } } board.get_moves.return_value = mocked_board_moves all_game_moves = game.get_moves(constants.WHITE) self.assertEqual(len(mocked_board_moves), len(all_game_moves)) for key in valid_moves.keys(): self.assertIn(key, all_game_moves) self.assertEqual(len(valid_moves[key]), len(all_game_moves[key])) for move in mocked_board_moves[key]: self.assertIn(move, all_game_moves[key]) self.assertEqual(game.status, constants.IN_PROGRESS)
def test_en_passant_moves_empty(self): """Test en passant moves empty if piece in attacked position is not a pawn""" black_pawn_position = Position(1, 3) white_knight_position = Position(0, 3) black_pawn = Pawn(None, BLACK, black_pawn_position) white_knight = Knight(None, WHITE, white_knight_position) test_board = { 'white_pieces': [white_knight], 'black_pieces': [black_pawn] } board = set_up_test_board(test_board=test_board) self.assertEqual(0, len(black_pawn.en_passant_moves))
def test_king_moves_open_board(self): """Test valid moves for king on an open board with no pieces""" white_king = King(None, WHITE, Position(4, 4)) white_knight = Knight(None, WHITE, Position(5, 3)) black_knight = Knight(None, BLACK, Position(3, 5)) test_board = { 'white_pieces': [white_king, white_knight], 'black_pieces': [black_knight] } board = set_up_test_board(new_game=False, test_board=test_board) self.assertEqual(len(white_king.moves), 7) moves = [ Position(3, 4), Position(3, 5), Position(4, 5), Position(5, 5), Position(5, 4), Position(4, 3), Position(3, 3) ] for m in moves: self.assertIn(m, white_king.moves)
def test_castling_moves_empty_when_attacked(self): """Test castling moves is empty when an enemy is attacking a square in between king and rook""" black_knight = Knight(None, BLACK, Position(2, 2)) white_rook = Rook(None, WHITE, Position(0, 0)) white_rook2 = Rook(None, WHITE, Position(7, 0)) test_board = { 'white_pieces': [white_rook, white_rook2], 'black_pieces': [black_knight] } board = set_up_test_board(new_game=False, test_board=test_board) white_king = board.get_piece(Position(4, 0)) self.assertEqual(len(white_king.castling_moves), 1)
def test_knight_moves_open(self): """Test valid moves for a knight on an open board with no pieces""" white_knight = Knight(None, WHITE, Position(3, 3)) test_board = { 'white_pieces': [white_knight] } board = set_up_test_board(new_game=False, test_board=test_board) self.assertEqual(len(white_knight.moves), 8) moves = [ Position(5, 2), Position(5, 4), Position(1, 2), Position(1, 4), Position(4, 1), Position(4, 5), Position(2, 5), Position(2, 1) ] for m in moves: self.assertIn(m, white_knight.moves)