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 bot_match(white_path, black_path): game = reconchess.LocalGame(900) _, white = reconchess.load_player(white_path) _, black = reconchess.load_player(black_path) winner_color, win_reason, history = reconchess.play_local_game(white(), black(), game=game) winner = "Draw" if winner_color is None else chess.COLOR_NAMES[winner_color] print("Game Over!") print(f"Winner: {winner}! ({win_reason})") Replay.from_history(history).play_sync()
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('bot_path', help='Path to bot source or bot module name.') parser.add_argument( '--username', default=None, help='Username for login. Enter with prompt if not specified.') parser.add_argument( '--password', default=None, help='Password for login. Enter with prompt if not specified.') parser.add_argument('--server-url', default='https://rbc.jhuapl.edu', help='URL of the server.') parser.add_argument('--ranked', action='store_true', default=False, help='Whether you want to play ranked matches.') parser.add_argument( '--keep-version', action='store_true', default=False, help='Force your ranked version to stay the same with no prompts.') parser.add_argument( '--max-concurrent-games', type=int, default=4, help='The maximum number of games to play at the same time.') args = parser.parse_args() bot_name, bot_cls = load_player(args.bot_path) username = ask_for_username() if args.username is None else args.username password = ask_for_password() if args.password is None else args.password auth = username, password server = RBCServer(args.server_url, auth) # verify we have the correct version of reconchess package check_package_version(server) def handle_term(signum, frame): print( '[{}] Received terminate signal, waiting for games to finish and then exiting...' .format(datetime.now())) unranked_mode(server) sys.exit(0) signal.signal(signal.SIGINT, handle_term) signal.signal(signal.SIGTERM, handle_term) # tell the server whether we want to do ranked matches or not if args.ranked: ranked_mode(server, args.keep_version) else: unranked_mode(server) listen_for_invitations(server, bot_cls, args.max_concurrent_games)
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('bot_path', help='Path to bot source or bot module name.') parser.add_argument( '--username', default=None, help='Username for login. Enter with prompt if not specified.') parser.add_argument( '--password', default=None, help='Password for login. Enter with prompt if not specified.') parser.add_argument('--server-url', default='https://rbc.jhuapl.edu', help='URL of the server.') parser.add_argument( '--max-concurrent-games', type=int, default=4, help='The maximum number of games to play at the same time.') args = parser.parse_args() bot_name, bot_cls = load_player(args.bot_path) username = ask_for_username() if args.username is None else args.username password = ask_for_password() if args.password is None else args.password auth = username, password listen_for_invitations(args.server_url, auth, bot_cls, args.max_concurrent_games)
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 main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('bot1_path', help='path to first bot source file') parser.add_argument('bot2_path', help='path to second 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() if random.randint(0, 1) == 0: white_bot_name, white_player_cls = load_player(args.bot1_path) black_bot_name, black_player_cls = load_player(args.bot2_path) else: white_bot_name, white_player_cls = load_player(args.bot2_path) black_bot_name, black_player_cls = load_player(args.bot1_path) game = LocalGame(args.seconds_per_player) 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] 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 main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('game_history_path', help='Path to game history JSON file.') parser.add_argument('bot_path', help='The path to bot source file or module.') parser.add_argument('color', default='random', choices=['white', 'black'], help='The color of the player to playback.') args = parser.parse_args() game_history = GameHistory.from_file(args.game_history_path) white_bot_name, white_player_cls = load_player(args.bot_path) color = chess.WHITE if args.color == 'white' else chess.BLACK playback(game_history, white_player_cls(), color)
def main(): parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--color', default='random', choices=['white', 'black', 'random'], help='The color you want to play as.') parser.add_argument('--server-url', default='https://rbc.jhuapl.edu', help='URL of the server.') parser.add_argument('--bot-path', help='Path to bot that should play on the server') args = parser.parse_args() # auth = ask_for_auth() # don't make repo public auth = ("stonkfish", "[redacted]") server = RBCServer(args.server_url, auth) usernames = server.get_active_users() if auth[0] in usernames: usernames.remove(auth[0]) if len(usernames) == 0: print('No active users.') quit() for i, username in enumerate(usernames): print('[{}] {}'.format(i, username)) i = int(input('Choose opponent: ')) color = chess.WHITE if args.color == 'black' or (args.color == 'random' and random.uniform(0, 1) < 0.5): color = chess.BLACK game_id = server.send_invitation(usernames[i], color) player_cls = UIPlayer if args.bot_path: _, player_cls = load_player(args.bot_path) winner_color, win_reason, game_history = play_remote_game(args.server_url, game_id, auth, player_cls()) winner = 'Draw' if winner_color is None else chess.COLOR_NAMES[winner_color] timestamp = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S') game_history.save('{}_{}_{}.json'.format(timestamp, game_id, winner))
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('bot_path', help='Path to bot source or bot module name.') parser.add_argument('--server-url', default='https://rbc.jhuapl.edu', help='URL of the server.') parser.add_argument( '--max-concurrent-games', type=int, default=4, help='The maximum number of games to play at the same time.') args = parser.parse_args() bot_name, bot_cls = load_player(args.bot_path) auth = ask_for_auth() listen_for_invitations(args.server_url, auth, bot_cls, args.max_concurrent_games)
import argparse import multiprocessing from reconchess import load_player from reconchess import play_remote_game parser = argparse.ArgumentParser() parser.add_argument('server', help='url to the server') parser.add_argument('auth_file', help='path to the file that contains auth information') parser.add_argument('bot_path', help='path to white bot source file') parser.add_argument('--max_games', type=int, help='the maximum number of bots to start up') args = parser.parse_args() bot_name, bot_cls = load_player(args.white_bot_path) def spawn_bot(game_id): player = bot_cls() multiprocessing.Process(target=play_remote_game, args=(bot_name, game_id, player)) # TODO make requests to server to see if any games are open for this player. if so, call spawn_bot()
import argparse import datetime import chess from reconchess import load_player, play_local_game white_win = 0 black_win = 0 parser = argparse.ArgumentParser() 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') args = parser.parse_args() for i in range(100): white_bot_name, white_player_cls = load_player(args.white_bot_path) black_bot_name, black_player_cls = load_player(args.black_bot_path) winner_color, win_reason, history = play_local_game( white_player_cls(), black_player_cls()) print('Game Over!') if winner_color is not None: print('{} won because of {}!'.format( white_bot_name if winner_color else black_bot_name, win_reason)) else: print('Draw!') winner = 'Draw' if winner_color is None else chess.COLOR_NAMES[winner_color] if winner_color == chess.WHITE: white_win += 1