def description(self): game_canvas = self.game_canvas card_name = self.get_card_name dealer_id = game_canvas.dealer_id current_player_id = game_canvas.current_player_id stock_pile_item_ids = game_canvas.find_withtag( configurations.STOCK_PILE_TAG) discard_pile_items = game_canvas.find_withtag( configurations.DISCARD_PILE_TAG) north_held_pile_item_ids = game_canvas.getter.get_held_pile_item_ids( player_id=0) south_held_pile_item_ids = game_canvas.getter.get_held_pile_item_ids( player_id=1) lines = [] lines.append("dealer: {}".format( GinRummyPlayer.short_name_of(player_id=dealer_id))) lines.append("current_player: {}".format( GinRummyPlayer.short_name_of(player_id=current_player_id))) lines.append("north hand: {}".format([ card_name(card_item_id) for card_item_id in north_held_pile_item_ids ])) lines.append("stockpile: {}".format( [card_name(card_item_id) for card_item_id in stock_pile_item_ids])) lines.append("discard pile: {}".format( [card_name(card_item_id) for card_item_id in discard_pile_items])) lines.append("south hand: {}".format([ card_name(card_item_id) for card_item_id in south_held_pile_item_ids ])) return "\n".join(lines)
def test_melding_3(self): # 2020-Apr-19 hand_text = [ '7H', '6H', '5H', '4S', '4H', '3H', '2S', 'AS', 'AH', 'AD', 'AC' ] hand = [utils.card_from_text(x) for x in hand_text] going_out_deadwood_count = 10 knock_cards, gin_cards = judge.get_going_out_cards( hand=hand, going_out_deadwood_count=going_out_deadwood_count) player = GinRummyPlayer(player_id=0, np_random=np.random.RandomState()) player.hand = hand player.did_populate_hand() meld_clusters = player.get_meld_clusters() alpha, beta = judge._get_going_out_cards( meld_clusters=meld_clusters, hand=hand, going_out_deadwood_count=going_out_deadwood_count) self.assertEqual(set(alpha), set(knock_cards)) self.assertEqual(beta, []) correct_knock_cards = [ utils.card_from_text(x) for x in ['7H', '4S', '4H', '3H', '2S', 'AS', 'AH', 'AD', 'AC'] ] self.assertEqual(set(knock_cards), set(correct_knock_cards)) self.assertEqual(gin_cards, [])
def test_melding_3(self): # 2020-Apr-19 hand_text = [ '7H', '6H', '5H', '4S', '4H', '3H', '2S', 'AS', 'AH', 'AD', 'AC' ] hand = [utils.card_from_text(x) for x in hand_text] going_out_deadwood_count = 10 knock_cards, gin_cards = judge.get_going_out_cards( hand=hand, going_out_deadwood_count=going_out_deadwood_count) player = GinRummyPlayer(player_id=0) player.hand = hand player.did_populate_hand() meld_clusters = player.get_meld_clusters() alpha, beta = judge._get_going_out_cards( meld_clusters=meld_clusters, hand=hand, going_out_deadwood_count=going_out_deadwood_count) assert set(alpha) == set(knock_cards) assert beta == [] correct_knock_cards = [ utils.card_from_text(x) for x in ['7H', '4S', '4H', '3H', '2S', 'AS', 'AH', 'AD', 'AC'] ] assert set(knock_cards) == set(correct_knock_cards) assert gin_cards == []
def __init__(self, dealer_id: int): ''' Initialize the round class Args: dealer_id: int ''' self.dealer_id = dealer_id self.dealer = GinRummyDealer() self.players = [ GinRummyPlayer(player_id=0), GinRummyPlayer(player_id=1) ] self.current_player_id = (dealer_id + 1) % 2 self.is_over = False self.going_out_action = None self.going_out_player_id = None self.move_sheet = [] player_dealing = GinRummyPlayer(player_id=dealer_id) shuffled_deck = self.dealer.shuffled_deck self.move_sheet.append( DealHandMove(player_dealing=player_dealing, shuffled_deck=shuffled_deck))
def test_gin_rummy_dealer(self): dealer = GinRummyDealer(np.random.RandomState()) current_deck = utils.get_deck() deck_card_ids = [utils.get_card_id(card) for card in current_deck] self.assertEqual(deck_card_ids, list(range(52))) # Deal 10 cards. player = GinRummyPlayer(player_id=0, np_random=np.random.RandomState()) dealer.deal_cards(player=player, num=10) self.assertEqual(len(dealer.shuffled_deck), 52) self.assertEqual(len(dealer.stock_pile), 42) self.assertEqual(len(current_deck), 52) self.assertEqual(len(utils.get_deck()), 52) # Pop top_card from current_deck. top_card = current_deck.pop(-1) self.assertEqual(str(top_card), "KC") self.assertEqual(len(current_deck), 51) self.assertEqual(len(utils.get_deck()), 52)
def test_gin_rummy_dealer(): dealer = GinRummyDealer() current_deck = get_deck() deck_text = [card.rank + card.suit for card in current_deck] deck_card_ids = [card.card_id for card in current_deck] print("deck=", deck_text) print(deck_card_ids) print("Deal 10 cards.") player = GinRummyPlayer(player_id=0) dealer.deal_cards(player=player, num=10) print("shuffled_deck_count=", len(dealer.shuffled_deck)) print("stock_pile_count=", len(dealer.stock_pile)) print("current_deck_count=", len(current_deck)) print("new_deck_count", len(get_deck())) print("Pop top_card from current_deck.") top_card = current_deck.pop(-1) print("top_card=", top_card, "current_deck_count=", len(current_deck), "new_deck_count=", len(get_deck()))
# Print out the trajectories print('\nEpisode {}'.format(episode)) for ts in trajectories[0]: print('State: {}, Action: {}, Reward: {}, Next State: {}, Done: {}'. format(ts[0], ts[1], ts[2], ts[3], ts[4])) # print move sheet print("\n========== Move Sheet ==========") move_sheet = env.game.round.move_sheet move_sheet_count = len(move_sheet) for i in range(move_sheet_count): move = move_sheet[i] print(move) if i == 0 and type(move) is DealHandMove: player_dealing = move.player_dealing leading_player = GinRummyPlayer.opponent_of(player_dealing) shuffle_deck = move.shuffled_deck leading_player_hand_text = [ str(card) for card in shuffle_deck[-11:] ] dealing_player_hand_text = [ str(card) for card in shuffle_deck[-21:-11] ] stock_pile_text = [str(card) for card in shuffle_deck[:31]] print("player_dealing is", player_dealing, "; leading_player is ", leading_player) print("leading player hand:", leading_player_hand_text) print("dealing player hand:", dealing_player_hand_text) print("stock_pile:", stock_pile_text)
# Generate data from the environment trajectories, _ = env.run(is_training=False) # Print out the trajectories print('\nEpisode {}'.format(episode)) for ts in trajectories[0]: print('State: {}, Action: {}, Reward: {}, Next State: {}, Done: {}'.format(ts[0], ts[1], ts[2], ts[3], ts[4])) # print move sheet print("\n========== Move Sheet ==========") move_sheet = env.game.round.move_sheet move_sheet_count = len(move_sheet) for i in range(move_sheet_count): move = move_sheet[i] print("{}".format(move)) if i == 0 and isinstance(move, DealHandMove): player_dealing_id = move.player_dealing.player_id leading_player_id = GinRummyPlayer.opponent_id_of(player_dealing_id) shuffle_deck = move.shuffled_deck leading_player_hand_text = [str(card) for card in shuffle_deck[-11:]] dealing_player_hand_text = [str(card) for card in shuffle_deck[-21:-11]] stock_pile_text = [str(card) for card in shuffle_deck[:31]] short_name_of_player_dealing = GinRummyPlayer.short_name_of(player_id=player_dealing_id) short_name_of_player_leading = GinRummyPlayer.short_name_of(player_id=leading_player_id) print("player_dealing is {}; leading_player is {}.".format(short_name_of_player_dealing, short_name_of_player_leading)) print("leading player hand: {}".format(leading_player_hand_text)) print("dealing player hand: {}".format(dealing_player_hand_text)) print("stock_pile: {}".format(stock_pile_text))