def main(): white_bot_name, black_bot_name = 'TroutBot', 'StrangeFish' game = LocalGame() try: winner_color, win_reason, history = play_local_game( TroutBot(), StrangeFish(*multiprocessing_strategies.create_strategy()), game=game) winner = 'Draw' if winner_color is None else chess.COLOR_NAMES[ winner_color] except: traceback.print_exc() game.end() winner = 'ERROR' history = game.get_game_history() print('Game Over!') print('Winner: {}!'.format(winner)) timestamp = datetime.now().strftime('%Y_%m_%d-%H_%M_%S') replay_path = '{}-{}-{}-{}.json'.format(white_bot_name, black_bot_name, winner, timestamp) print('Saving replay to {}...'.format(replay_path)) history.save(replay_path)
def test_time(self, seconds=1, turns=20, phases=3): delta = seconds / (turns * phases) game = LocalGame(seconds_per_player=seconds) turn = True time_by_color = game.seconds_left_by_color.copy() game.start() for i in range(turns): for _ in range(phases): start = game.get_seconds_left() time.sleep(delta) end = game.get_seconds_left() self.assertAlmostEqual(start - end, delta, places=2) time_by_color[turn] = game.get_seconds_left() turn = not turn game.end_turn() self.assertAlmostEqual(game.get_seconds_left(), time_by_color[turn], places=2) game.end() self.assertAlmostEqual(game.get_seconds_left(), time_by_color[turn], places=2) time.sleep(delta) self.assertAlmostEqual(game.get_seconds_left(), time_by_color[turn], places=2)
def play(bot1_path, bot2_path, seconds_per_player): white_bot_name, white_player_cls = load_player(bot1_path) black_bot_name, black_player_cls = load_player(bot2_path) game = LocalGame(seconds_per_player) try: winner_color, win_reason, history = play_local_game(white_player_cls(), black_player_cls(), game=game) winner = 'Draw' if winner_color is None else chess.COLOR_NAMES[winner_color] except: traceback.print_exc() game.end() winner = 'ERROR' history = game.get_game_history() print('Game Over!') print('Winner: {}!'.format(winner)) timestamp = datetime.datetime.now().strftime('%Y_%m_%d-%H_%M_%S') replay_path = '{}-{}-{}-{}.json'.format(white_bot_name, black_bot_name, winner, timestamp) print('Saving replay to {}...'.format(replay_path)) history.save(replay_path) return winner
def test_white_resign(self): game = LocalGame() game.start() game.resign() game.end() self.assertEqual(BLACK, game.get_winner_color()) self.assertEqual(WinReason.RESIGN, game.get_win_reason())
def test_black_resign(self): game = LocalGame() game.start() game.end_turn() game.resign() game.end() self.assertEqual(WHITE, game.get_winner_color()) self.assertEqual(WinReason.RESIGN, game.get_win_reason())
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)
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('white_bot_path', help='path to white bot source file') parser.add_argument('black_bot_path', help='path to black bot source file') parser.add_argument( '--seconds_per_player', default=900, type=float, help='number of seconds each player has to play the entire game.') args = parser.parse_args() white_bot_name, white_player_cls = load_player(args.white_bot_path) black_bot_name, black_player_cls = load_player(args.black_bot_path) game = LocalGame(args.seconds_per_player) try: winner_color, win_reason, history = play_local_game(white_player_cls(), black_player_cls(), game=game) winner = 'Draw' if winner_color is None else chess.COLOR_NAMES[ winner_color] except: traceback.print_exc() game.end() winner = 'ERROR' history = game.get_game_history() print('Game Over!') print('Winner: {}!'.format(winner)) timestamp = datetime.datetime.now().strftime('%Y_%m_%d-%H_%M_%S') replay_path = '{}-{}-{}-{}.json'.format(white_bot_name, black_bot_name, winner, timestamp) print('Saving replay to {}...'.format(replay_path)) history.save(replay_path)
def test_forced_over(self): game = LocalGame() game.start() game.end() self.assertIsNone(game.get_winner_color()) self.assertIsNone(game.get_win_reason())
def test_forced_over(self): game = LocalGame() game.start() self.assertFalse(game.is_over()) game.end() self.assertTrue(game.is_over())