def test_attach_hole_card(self): game_state = restore_game_state(TwoPlayerSample.round_state) to_card = lambda s: Card.from_str(s) hole1, hole2 = [to_card(c) for c in ["SA", "DA"]], [to_card(c) for c in ["HK", "C2"]] processed1 = attach_hole_card(game_state, "tojrbxmkuzrarnniosuhct", hole1) processed2 = attach_hole_card(processed1, "pwtwlmfciymjdoljkhagxa", hole2) players = processed2["table"].seats.players self.eq(hole1, players[0].hole_card) self.eq(hole2, players[1].hole_card) self.eq([0,0], [len(p.hole_card) for p in game_state["table"].seats.players])
def setup_game_state(round_state, my_hole_cards, opponent_hole_cards, my_uuid): game_state = restore_game_state(round_state) for player_info in round_state['seats']: uuid = player_info['uuid'] if uuid == my_uuid: game_state = attach_hole_card(game_state, uuid, my_hole_cards) else: game_state = attach_hole_card(game_state, uuid, opponent_hole_cards) return game_state
def test_attach_hole_card(self): game_state = restore_game_state(TwoPlayerSample.round_state) to_card = lambda s: Card.from_str(s) hole1, hole2 = [to_card(c) for c in ["SA", "DA"] ], [to_card(c) for c in ["HK", "C2"]] processed1 = attach_hole_card(game_state, "tojrbxmkuzrarnniosuhct", hole1) processed2 = attach_hole_card(processed1, "pwtwlmfciymjdoljkhagxa", hole2) players = processed2["table"].seats.players self.eq(hole1, players[0].hole_card) self.eq(hole2, players[1].hole_card) self.eq([0, 0], [len(p.hole_card) for p in game_state["table"].seats.players])
def test_run_until_game_finish_when_one_player_is_left(self): uuids = [ "ruypwwoqwuwdnauiwpefsw", "sqmfwdkpcoagzqxpxnmxwm", "uxrdiwvctvilasinweqven" ] holecards = [[Card.from_str(s) for s in ss] for ss in [["C2", "C3"], ["HA", "CA"], ["D5", "H6"]]] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) game_state = reduce( lambda state, item: attach_hole_card(state, item[0], item[1]), zip(uuids, holecards), game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) p1_acts = [("fold", 0), ("call", 10), ('call', 0), ('call', 10), ("fold", 0)] p2_acts = [] p3_acts = [("raise", 10)] players = [TestPlayer(acts) for acts in [p1_acts, p2_acts, p3_acts]] [ self.emu.register_player(uuid, player) for uuid, player in zip(uuids, players) ] game_state["table"].deck.deck.append(Card.from_str("C7")) game_state, events = self.emu.run_until_game_finish(game_state) self.eq("event_game_finish", events[-1]["type"]) self.eq(0, game_state["table"].seats.players[0].stack) self.eq(0, game_state["table"].seats.players[1].stack) self.eq(292, game_state["table"].seats.players[2].stack)
def test_run_until_game_finish_when_one_player_is_left(self): uuids = [ 'ruypwwoqwuwdnauiwpefsw', 'sqmfwdkpcoagzqxpxnmxwm', 'uxrdiwvctvilasinweqven' ] holecards = [[Card.from_str(s) for s in ss] for ss in [['2c', '3c'], ['Ah', 'Ac'], ['5d', '6d']]] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) game_state = reduce( lambda state, item: attach_hole_card(state, item[0], item[1]), zip(uuids, holecards), game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) p1_acts = [('fold', 0), ('call', 10), ('call', 0), ('call', 10), ('fold', 0)] p2_acts = [] p3_acts = [('raise', 10)] players = [TestPlayer(acts) for acts in [p1_acts, p2_acts, p3_acts]] [ self.emu.register_player(uuid, player) for uuid, player in zip(uuids, players) ] game_state['table'].deck.deck.append(Card.from_str('7c')) game_state, events = self.emu.run_until_game_finish(game_state) self.eq('event_game_finish', events[-1]['type']) self.eq(0, game_state['table'].seats.players[0].stack) self.eq(0, game_state['table'].seats.players[1].stack) self.eq(292, game_state['table'].seats.players[2].stack)
def test_attach_hole_card(self): game_state = restore_game_state(TwoPlayerSample.round_state) def to_card(s): return Card.from_str(s) hole1, hole2 = [to_card(c) for c in ['As', 'Ad'] ], [to_card(c) for c in ['Kh', '2c']] processed1 = attach_hole_card(game_state, 'tojrbxmkuzrarnniosuhct', hole1) processed2 = attach_hole_card(processed1, 'pwtwlmfciymjdoljkhagxa', hole2) players = processed2['table'].seats.players self.eq(hole1, players[0].hole_card) self.eq(hole2, players[1].hole_card) self.eq([0, 0], [len(p.hole_card) for p in game_state['table'].seats.players])
def setup_game_state(round_state, my_hole_card): game_state = restore_game_state(round_state) for player_info in round_state['seats']: if uuid == self.uuid: # Hole card of my player should be fixed. Because we know it. game_state = attach_hole_card(game_state, uuid, my_hole_card) else: # We don't know hole card of opponents. So attach them at random from deck. game_state = attach_hole_card_from_deck(game_state, uuid)
def _setup_game_state(self, round_state, my_hole_card): game_state = restore_game_state(round_state) game_state['table'].deck.shuffle() player_uuids = [player_info['uuid'] for player_info in round_state['seats']] for uuid in player_uuids: if uuid == self.uuid: game_state = attach_hole_card(game_state, uuid, gen_cards(my_hole_card)) # attach my holecard else: game_state = attach_hole_card_from_deck(game_state, uuid) # attach opponents holecard at random return game_state
def _setup_game_state(self, round_state): game_state = restore_game_state(round_state) for player in game_state['table'].seats.players: if self.my_uuid == player.uuid: game_state = attach_hole_card(game_state, player.uuid, self.my_hole_card) else: game_state = attach_hole_card_from_deck( game_state, player.uuid) return game_state
def get_game_state(round_state, hole_card, uuid): game_state = restore_game_state(round_state) for player_info in round_state['seats']: uuid_new = player_info['uuid'] if uuid_new == uuid: game_state = attach_hole_card(game_state, uuid, hole_card) else: game_state = attach_hole_card_from_deck(game_state, uuid_new) return game_state
def _setup_game_state(self, round_state, my_hole_card): game_state = restore_game_state(round_state) game_state['table'].deck.shuffle() player_uuids = [player_info['uuid'] for player_info in round_state['seats']] for uuid in player_uuids: if uuid == self.uuid: game_state = attach_hole_card(game_state, uuid, gen_cards(my_hole_card)) # attach my holecard else: game_state = attach_hole_card_from_deck(game_state, uuid) # attach opponents holecard at random return game_state
def _setup_game_state(self, round_state, my_hole_card): game_state = restore_game_state(round_state) game_state['table'].deck.shuffle() players_uuid = [ player_info['uuid'] for player_info in round_state['seats'] ] for uuid in players_uuid: if uuid == self.uuid: game_state = attach_hole_card(game_state, uuid, gen_cards(my_hole_card)) else: game_state = attach_hole_card_from_deck(game_state, uuid) return game_state
def test_run_until_game_finish_when_final_round(self): uuids = ["ruypwwoqwuwdnauiwpefsw", "sqmfwdkpcoagzqxpxnmxwm", "uxrdiwvctvilasinweqven"] holecards = [[Card.from_str(s) for s in ss] for ss in [["C2","C3"],["HA","CA"],["D5","H6"]]] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) game_state = reduce(lambda state, item: attach_hole_card(state, item[0], item[1]), zip(uuids, holecards), game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) [self.emu.register_player(uuid, FoldMan()) for uuid in uuids] game_state["table"].deck.deck.append(Card.from_str("C7")) game_state, events = self.emu.run_until_game_finish(game_state) self.eq("event_game_finish", events[-1]["type"]) self.eq(10, game_state["round_count"]) self.eq(35, game_state["table"].seats.players[0].stack) self.eq(0, game_state["table"].seats.players[1].stack) self.eq(265, game_state["table"].seats.players[2].stack)
def _setup_game_state(self, round_state, my_hole_card): game_state = restore_game_state(round_state) game_state['table'].deck.shuffle() community_card = round_state['community_card'] current_street = round_state['street'] player_uuids = [ player_info['uuid'] for player_info in round_state['seats'] ] for uuid in player_uuids: if uuid == self.uuid: game_state = attach_hole_card( game_state, uuid, gen_cards(my_hole_card)) # attach my holecard else: while True: opponent_card = assuming_card(gen_cards(my_hole_card)) if not self.is_already_fold(opponent_card, current_street, community_card): break game_state = attach_hole_card( game_state, uuid, opponent_card) # attach opponents holecard at random return game_state
def test_run_until_game_finish_when_final_round(self): uuids = ["ruypwwoqwuwdnauiwpefsw", "sqmfwdkpcoagzqxpxnmxwm", "uxrdiwvctvilasinweqven"] holecards = [[Card.from_str(s) for s in ss] for ss in [["C2", "C3"], ["HA", "CA"], ["D5", "H6"]]] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) game_state = reduce(lambda state, item: attach_hole_card(state, item[0], item[1]), zip(uuids, holecards), game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) [self.emu.register_player(uuid, FoldMan()) for uuid in uuids] game_state["table"].deck.deck.append(Card.from_str("C7")) game_state, events = self.emu.run_until_game_finish(game_state) self.eq("event_game_finish", events[-1]["type"]) self.eq(10, game_state["round_count"]) self.eq(35, game_state["table"].seats.players[0].stack) self.eq(0, game_state["table"].seats.players[1].stack) self.eq(265, game_state["table"].seats.players[2].stack)
def test_run_until_game_finish_when_one_player_is_left(self): uuids = ["ruypwwoqwuwdnauiwpefsw", "sqmfwdkpcoagzqxpxnmxwm", "uxrdiwvctvilasinweqven"] holecards = [[Card.from_str(s) for s in ss] for ss in [["C2","C3"],["HA","CA"],["D5","H6"]]] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) game_state = reduce(lambda state, item: attach_hole_card(state, item[0], item[1]), zip(uuids, holecards), game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) p1_acts = [("fold",0), ("call", 10), ('call', 0), ('call', 10), ("fold",0)] p2_acts = [] p3_acts = [("raise", 10)] players = [TestPlayer(acts) for acts in [p1_acts, p2_acts, p3_acts]] [self.emu.register_player(uuid, player) for uuid, player in zip(uuids, players)] game_state["table"].deck.deck.append(Card.from_str("C7")) game_state, events = self.emu.run_until_game_finish(game_state) self.eq("event_game_finish", events[-1]["type"]) self.eq(0, game_state["table"].seats.players[0].stack) self.eq(0, game_state["table"].seats.players[1].stack) self.eq(292, game_state["table"].seats.players[2].stack)
def emulate(hole_card, round_state): # 1. Set game settings in Emulator emulator = Emulator() sb_amount = round_state['small_blind_amount'] # emulator(nb_player,max_rounds,sb_amount,ante) emulator.set_game_rule(2, 10, sb_amount, 1) # 2. Setup Gamestate object game_state = restore_game_state(round_state) # Attach hole_cards for each player (at random for opponent) game_state = attach_hole_card(game_state,round_state['seats'][0]['uuid'], gen_cards(hole_card)) game_state = attach_hole_card_from_deck(game_state,round_state['seats'][1]['uuid']) # 3. Run simulation and get updated GameState object # updated_state, events = emulator.apply_action(game_state, "call", 10) return game_state, emulator
def test_run_until_round_finish_game_finish_detect(self): uuids = ["tojrbxmkuzrarnniosuhct", "pwtwlmfciymjdoljkhagxa"] holecards = [[Card.from_str(s) for s in ss] for ss in [["CA", "D2"], ["C8", "H5"]]] game_state = restore_game_state(TwoPlayerSample.round_state) game_state = reduce(lambda a, e: attach_hole_card(a, e[0], e[1]), zip(uuids, holecards), game_state) game_state = attach_hole_card_from_deck(game_state, "pwtwlmfciymjdoljkhagxa") self.emu.set_game_rule(2, 10, 5, 0) p1 = TestPlayer([("raise", 65)]) p2 = TestPlayer([("call", 15), ("call", 65)]) self.emu.register_player("tojrbxmkuzrarnniosuhct", p1) self.emu.register_player("pwtwlmfciymjdoljkhagxa", p2) game_state["table"].deck.deck.append(Card.from_str("C7")) game_state, events = self.emu.run_until_round_finish(game_state) self.eq("event_new_street", events[0]["type"]) self.eq("event_ask_player", events[1]["type"]) self.eq("event_ask_player", events[2]["type"]) self.eq("event_round_finish", events[3]["type"]) self.eq("event_game_finish", events[4]["type"]) self.eq(0, events[4]["players"][0]["stack"]) self.eq(200, events[4]["players"][1]["stack"])
def test_run_until_round_finish_game_finish_detect(self): uuids = ["tojrbxmkuzrarnniosuhct", "pwtwlmfciymjdoljkhagxa"] holecards = [[Card.from_str(s) for s in ss] for ss in [["CA", "D2"], ["C8", "H5"]]] game_state = restore_game_state(TwoPlayerSample.round_state) game_state = reduce(lambda a,e: attach_hole_card(a, e[0], e[1]), zip(uuids, holecards), game_state) game_state = attach_hole_card_from_deck(game_state, "pwtwlmfciymjdoljkhagxa") self.emu.set_game_rule(2, 10, 5, 0) p1 = TestPlayer([("raise", 65)]) p2 = TestPlayer([("call", 15), ("call", 65)]) self.emu.register_player("tojrbxmkuzrarnniosuhct", p1) self.emu.register_player("pwtwlmfciymjdoljkhagxa", p2) game_state["table"].deck.deck.append(Card.from_str("C7")) game_state, events = self.emu.run_until_round_finish(game_state) self.eq("event_new_street", events[0]["type"]) self.eq("event_ask_player", events[1]["type"]) self.eq("event_ask_player", events[2]["type"]) self.eq("event_round_finish", events[3]["type"]) self.eq("event_game_finish", events[4]["type"]) self.eq(0, events[4]["players"][0]["stack"]) self.eq(200, events[4]["players"][1]["stack"])
def test_run_until_game_finish_when_final_round(self): uuids = [ 'ruypwwoqwuwdnauiwpefsw', 'sqmfwdkpcoagzqxpxnmxwm', 'uxrdiwvctvilasinweqven' ] holecards = [[Card.from_str(s) for s in ss] for ss in [['2c', '3c'], ['Ah', 'Ac'], ['5d', '6d']]] game_state = restore_game_state(ThreePlayerGameStateSample.round_state) game_state = reduce( lambda state, item: attach_hole_card(state, item[0], item[1]), zip(uuids, holecards), game_state) sb_amount, ante = 5, 7 self.emu.set_game_rule(3, 10, sb_amount, ante) [self.emu.register_player(uuid, FoldMan()) for uuid in uuids] game_state['table'].deck.deck.append(Card.from_str('7c')) game_state, events = self.emu.run_until_game_finish(game_state) self.eq('event_game_finish', events[-1]['type']) self.eq(10, game_state['round_count']) self.eq(35, game_state['table'].seats.players[0].stack) self.eq(0, game_state['table'].seats.players[1].stack) self.eq(265, game_state['table'].seats.players[2].stack)
def test_run_until_round_finish_game_finish_detect(self): uuids = ['tojrbxmkuzrarnniosuhct', 'pwtwlmfciymjdoljkhagxa'] holecards = [[Card.from_str(s) for s in ss] for ss in [['Ac', '2d'], ['8c', '5h']]] game_state = restore_game_state(TwoPlayerSample.round_state) game_state = reduce(lambda a, e: attach_hole_card(a, e[0], e[1]), zip(uuids, holecards), game_state) game_state = attach_hole_card_from_deck(game_state, 'pwtwlmfciymjdoljkhagxa') self.emu.set_game_rule(2, 10, 5, 0) p1 = TestPlayer([('raise', 65)]) p2 = TestPlayer([('call', 15), ('call', 65)]) self.emu.register_player('tojrbxmkuzrarnniosuhct', p1) self.emu.register_player('pwtwlmfciymjdoljkhagxa', p2) game_state['table'].deck.deck.append(Card.from_str('7c')) game_state, events = self.emu.run_until_round_finish(game_state) self.eq('event_new_street', events[0]['type']) self.eq('event_ask_player', events[1]['type']) self.eq('event_ask_player', events[2]['type']) self.eq('event_round_finish', events[3]['type']) self.eq('event_game_finish', events[4]['type']) self.eq(0, events[4]['players'][0]['stack']) self.eq(200, events[4]['players'][1]['stack'])
"stack": STACK } } game_state = emul.generate_initial_game_state(players_info) while True: game_state, events = emul.start_new_round(game_state) if events[-1]["type"] == "event_game_finish": break for player in game_state["table"].seats.players: if player.uuid == "target": card1 = input("Enter first hole card > ") card2 = input("Enter second hold card > ") hole_card = gen_cards([card1, card2]) bot.add_hole_card(hole_card) game_state = attach_hole_card(game_state, player.uuid, hole_card) else: game_state = attach_hole_card_from_deck( game_state, player.uuid) while game_state["street"] != Const.Street.FINISHED: print("action hitories: ", events[0]["round_state"]['action_histories']) print("community cards: ", events[0]["round_state"]["community_card"]) cur_street = game_state["street"] print("valid actions: ", events[-1]['valid_actions']) if events[0]['round_state']['next_player'] == 1: action = input("Enter a valid action > ") amount = int(input("Enter a valid amount > ")) game_state, events = emul.apply_action(game_state, action, amount)
def test_attach_hole_card_when_same_uuid_players_exist(self): game_state = restore_game_state(TwoPlayerSample.round_state) p1, p2 = game_state["table"].seats.players[:2] p2.uuid = p1.uuid attach_hole_card(game_state, p1.uuid, "dummy_hole")
def test_attach_hole_card_when_uuid_is_wrong(self): game_state = restore_game_state(TwoPlayerSample.round_state) attach_hole_card(game_state, "hoge", "dummy_hole")
def test_attach_hole_card_when_same_uuid_players_exist(self): game_state = restore_game_state(TwoPlayerSample.round_state) p1, p2 = game_state["table"].seats.players[:2] p2.uuid = p1.uuid attach_hole_card(game_state, p1.uuid, "dummy_hole")
def test_attach_hole_card_when_uuid_is_wrong(self): game_state = restore_game_state(TwoPlayerSample.round_state) attach_hole_card(game_state, "hoge", "dummy_hole")