def main(): seed = argv[1] if len(argv) > 1 else None session, bot_class = None, None game_type = input("Play (l)ocal, (r)emote or (b)ot game? ") or 'b' if game_type and game_type[0] == 'r': server_class = \ MockHanabiServer if game_type[0:2] == 'rt' \ else HanabiLocalFileServer if game_type[0:2] == 'rl' \ else HanabiGistServer is_rejoin = game_type[-2:] == 'rj' session = start_remote_game(seed, server_class, is_rejoin=is_rejoin) hanabi = session.hanabi elif game_type == 'b': reps = int(input("how many reps? ") or 1) bot_names = [c[0] for c in inspect.getmembers(hanabibot) if c[0][-3:] == "Bot"] print("\n".join(["{} = {}".format(i, bc) for i, bc in enumerate(bot_names)])) bot_name = bot_names[int(input("Which bot to use? ") or 0)] bot_class = getattr(hanabibot, bot_name) num_players = int(input("How many instances of {} (2-5)? ".format(bot_name)) or 2) if(reps > 1): bot_game(bot_class, num_players, seed, reps) hanabi = HanabiGame(num_players, seed) else: hanabi = HanabiGame(2, seed) move_descriptions = game_loop(hanabi, session, bot_class) print_end_game(hanabi, move_descriptions)
def test_game_over_on_third_mistake(self): h = HanabiGame(2, 'aaaaa') h.play(1) h.play(1) self.assertFalse(h.is_game_over(), "Game is not over after two mistakes") h.play(1) self.assertTrue(h.is_game_over(), "Game is over after three mistakes")
def test_discarding_card_adds_clock_to_max_8(self): h = HanabiGame(2, 'aaaaa') h.inform(1, "1") clocks = h.clocks h.discard(0) self.assertEqual(clocks + 1, h.clocks, "discarding card adds a clock") h.add_clock() self.assertEqual(h.clocks, 8, "clocks can't be added above 8")
def bot_game(bot_class, num_players, seed, reps): scores = [] title = "{} x {} playing, starting seed {} for {} reps"\ .format(num_players, bot_class.__name__, seed, reps) print(title) for i in range(reps): sys.stdout = StringIO() hanabi = HanabiGame(num_players, seed) while not hanabi.is_game_over(): bot = bot_class(hanabi) play_move(hanabi, bot.get_move()) scores.append((seed, hanabi.score(), hanabi.lives)) seed = hanabi.random_seed() sys.stdout = sys.__stdout__ # these two lines can be uncommented if the render block is moved into the above loop # os.system('clear') # print(title) print(render_scores(scores)) sleep(0.1) exit()
def start_remote_game(seed, server_class, is_rejoin=False): """ Prompts player to create or join or re-join a remote game, returns a session object for it """ player_name = input("What's your name? ") check_credentials() import credentials server = server_class('https://api.github.com/gists', credentials, is_test="--auto" in argv) session = HanabiSession(server) game_list = session.request_game_list(new=not is_rejoin) print("Choose game to join:") for i, f in enumerate(game_list): print(" - ({}) join {}".format(i, f)) if not game_list: print("\n( no current games exist )\n") chosen_game = input(" - (n) create new game? ") if chosen_game == 'n': num_players = input("how many players (2-5)? ") hanabi = HanabiGame(int(num_players), seed) session.new_game(hanabi, player_name) session.await_players() elif is_rejoin: game_title = game_list[int(chosen_game)] print("Loading players in {}...".format(game_title)) for i, f in enumerate(session.list_players(game_title)): print(" - ({}) {}".format(i, f)) player_id = int(input("which player to join as? ")) session.rejoin_game(game_title, player_id) print("Loading moves...") for move in session.list_moves(): play_move(session.hanabi, move) else: session.join_game(game_list[int(chosen_game)], player_name) session.await_players() return session
def test_deck_same_given_same_seed(self): h1 = HanabiGame(2, 'aaaaa') h2 = HanabiGame(2, 'aaaaa') self.assertEqual(h1.deck, h2.deck)
def test_cant_discard_if_max_clocks(self): h = HanabiGame(2, 'aaaaa') self.assertRaises(AssertionError, h.discard, 0)
def test_informing_costs_clock(self): h = HanabiGame(2, 'aaaaa') clocks = h.clocks h.inform(1, "1") self.assertEqual(clocks - 1, h.clocks)
def test_bad_card_loses_life(self): h = HanabiGame(2, 'aaaaa') lives = h.lives h.play(1) self.assertEqual(lives - 1, h.lives, "Playing non-valid card loses a life")