class LocalGameSenseTest(unittest.TestCase): def setUp(self): self.game = LocalGame() def test_senses_actions_content(self): sense_actions = self.game.sense_actions() for square in SQUARES: self.assertIn(square, sense_actions) def test_sense_invalid(self): for square in [-1, 65, 66, 1023730, -2]: with self.assertRaises(ValueError): self.game.sense(square) def test_sense_squares(self): for square in SQUARES: sense_result = self.game.sense(square) squares = [s for s, p in sense_result] self.assertEqual(squares, SENSE_BY_SQUARE[square]) def test_sense_pieces(self): for sense_square in SQUARES: sense_result = self.game.sense(sense_square) for square, piece in sense_result: self.assertEqual(piece, self.game.board.piece_at(square)) def test_sense_pass(self): sense_result = self.game.sense(None) self.assertEqual(sense_result, [])
def test_capture(self): """ r n b q k b n r p p p . . p p p . . . . . . . . . . . p p . . . . . . P P . . . . . . . . . . . P P P . . P P P R N B Q K B N R """ game = LocalGame() game.board.set_board_fen('rnbqkbnr/ppp2ppp/8/3pp3/3PP3/8/PPP2PPP/RNBQKBNR') game.start() _, _, result1 = game.move(Move(D4, E5)) self.assertEqual(result1, E5) self.assertEqual(result1, game.opponent_move_results()) game.end_turn() self.assertEqual(result1, game.opponent_move_results()) game.sense(E5) self.assertEqual(result1, game.opponent_move_results()) _, _, result2 = game.move(Move(D5, E4)) self.assertEqual(result2, E4) self.assertEqual(result2, game.opponent_move_results())
def test_move_limit(self): game = LocalGame(reversible_moves_limit=1) game.start() for half_turn in range(2): game.sense(None) game.move(None) game.end_turn() self.assertTrue(game.is_over())
def test_move_limit(self): game = LocalGame(reversible_moves_limit=1) game.start() for half_turn in range(2): game.sense(None) game.move(None) game.end_turn() self.assertEqual(None, game.get_winner_color()) self.assertEqual(WinReason.MOVE_LIMIT, game.get_win_reason())
def test_turn_limit(self): game = LocalGame(full_turn_limit=1) game.start() for half_turn in range(2): game.sense(None) game.move(None) game.end_turn() self.assertEqual(None, game.get_winner_color()) self.assertEqual(WinReason.TURN_LIMIT, game.get_win_reason())
def test_no_capture(self): game = LocalGame() game.start() _, _, result1 = game.move(Move(A2, A4)) self.assertIsNone(result1) self.assertEqual(result1, game.opponent_move_results()) game.end_turn() self.assertEqual(result1, game.opponent_move_results()) game.sense(E5) self.assertEqual(result1, game.opponent_move_results()) _, _, result2 = game.move(Move(F7, F5)) self.assertIsNone(result2) self.assertEqual(result2, game.opponent_move_results())
def test_unlimited(self): game = LocalGame( seconds_per_player=None, seconds_increment=None, full_turn_limit=None, reversible_moves_limit=None, ) game.start() for half_turn in range(20): game.sense(None) game.move(None) game.end_turn() self.assertFalse(game.is_over())
def test_no_history_until_game_over(self): g = LocalGame() g.sense(E2) self.assertEqual(g.get_game_history(), None) g.move(Move(E2, E4)) self.assertEqual(g.get_game_history(), None) g.sense(A8) g.move(Move(E7, E5)) self.assertEqual(g.get_game_history(), None) g.sense(E2) g.move(Move(F1, B5)) g.sense(A8) g.move(Move(D7, D5)) g.sense(E8) g.move(Move(B5, E8)) self.assertTrue(g.is_over()) self.assertNotEqual(g.get_game_history(), None)
def playback(game_history: GameHistory, player: Player, color: Color): game = LocalGame() opponent_name = game_history.get_white_player_name() if color == chess.WHITE: opponent_name = game_history.get_black_player_name() player.handle_game_start(color, game.board.copy(), opponent_name) game.start() turn = game_history.first_turn() while not game.is_over() and turn < game_history.last_turn(): opt_capture_square = game.opponent_move_results() if game.turn == color: player.handle_opponent_move_result(opt_capture_square is not None, opt_capture_square) sense_actions = game.sense_actions() move_actions = game.move_actions() sense = game_history.sense(turn) player_sense = player.choose_sense(sense_actions, move_actions, game.get_seconds_left()) if game.turn == color and sense != player_sense: print( 'Warning: Sense action did not match history on turn {}. Using the sense action from history.' .format(turn)) sense_result = game.sense(sense) if game.turn == color: player.handle_sense_result(sense_result) move = game_history.requested_move(turn) player_move = player.choose_move(move_actions, game.get_seconds_left()) if game.turn == color and move != player_move: print( 'Warning: Move action did not match history on turn {}. Using the move action from history.' .format(turn)) requested_move, taken_move, opt_enemy_capture_square = game.move(move) if game.turn == color: player.handle_move_result(requested_move, taken_move, opt_enemy_capture_square is not None, opt_enemy_capture_square) game.end_turn() turn = turn.next game.end() winner_color = game.get_winner_color() win_reason = game.get_win_reason() game_history = game.get_game_history() player.handle_game_end(winner_color, win_reason, game_history)