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 test_knocking(self): hand_text = [ 'JS', 'JH', 'JD', '8C', '7S', '7H', '7D', '4S', '3D', '2S', 'AC' ] hand = [utils.card_from_text(x) for x in hand_text] knock_cards, _ = judge.get_going_out_cards(hand=hand, going_out_deadwood_count=10) self.assertEqual(set(knock_cards), set([utils.card_from_text(x) for x in ['8C']]))
def post_do_gin_action(self): game_canvas = self.game_canvas game_canvas.going_out_button.place_forget() current_player_id = game_canvas.current_player_id current_hand = game_canvas.getter.get_held_pile_cards( player_id=current_player_id) going_out_deadwood_count = self.game_canvas.game_canvas_updater.env.game.settings.going_out_deadwood_count _, gin_cards = judge.get_going_out_cards( hand=current_hand, going_out_deadwood_count=going_out_deadwood_count) card = gin_cards[0] card_id = gin_rummy_utils.get_card_id(card=card) card_item = game_canvas.card_items[card_id] selected_held_pile_item_id = card_item.item_id top_discard_pile_item_id = game_canvas.getter.get_top_discard_pile_item_id( ) if top_discard_pile_item_id is None: to_location = game_canvas.discard_pile_anchor else: dx = game_canvas.discard_pile_tab to_location = game_canvas.coords(top_discard_pile_item_id) to_location = utils.translated_by(dx=dx, dy=0, location=to_location) utils.set_card_item_id_face_up(card_item_id=selected_held_pile_item_id, face_up=True, game_canvas=game_canvas) def loop_completion(): game_canvas.dtag(selected_held_pile_item_id, configurations.SELECTED_TAG) game_canvas.dtag(selected_held_pile_item_id, configurations.JOGGED_TAG) game_canvas.dtag(selected_held_pile_item_id, game_canvas.held_pile_tags[current_player_id]) game_canvas.addtag_withtag(configurations.DISCARD_PILE_TAG, selected_held_pile_item_id) utils.fan_held_pile(player_id=current_player_id, game_canvas=game_canvas) # show meld piles for both players self._show_meld_piles() # submit action to game_canvas_updater action = GIN_ACTION_ID game_canvas.after_idle( game_canvas.game_canvas_updater.did_perform_actions, [action]) self._move_loop(selected_held_pile_item_id, to_location, completion=loop_completion)
def get_legal_actions(self, player_id: int) -> List[ActionEvent]: if player_id != self.get_current_player_id(): return [] legal_actions = [] # type: List[ActionEvent] game = self.get_game() game_canvas = self.game_canvas moves = game.round.move_sheet[:game_canvas.game_canvas_updater.mark] last_move = None if not moves else moves[-1] if not last_move: return [] settings = self.get_settings() if isinstance(last_move, DealHandMove) or \ isinstance(last_move, DrawCardMove) or \ isinstance(last_move, PickupDiscardMove): going_out_deadwood_count = settings.going_out_deadwood_count hand = self.get_held_pile_cards(player_id=player_id) knock_cards, gin_cards = judge.get_going_out_cards(hand=hand, going_out_deadwood_count=going_out_deadwood_count) if settings.is_allowed_gin and gin_cards: legal_actions = [GinAction()] else: cards_to_discard = [card for card in hand] if isinstance(last_move, PickupDiscardMove): if not settings.is_allowed_to_discard_picked_up_card: picked_up_card = last_move.card cards_to_discard.remove(picked_up_card) discard_actions = [DiscardAction(card=card) for card in cards_to_discard] legal_actions = discard_actions if settings.is_allowed_knock: if player_id == 0 or not settings.is_south_never_knocks: if knock_cards: knock_actions = [KnockAction(card=card) for card in knock_cards] if not settings.is_always_knock: legal_actions.extend(knock_actions) else: legal_actions = knock_actions elif isinstance(last_move, DeclareDeadHandMove): legal_actions = [ScoreNorthPlayerAction()] elif isinstance(last_move, GinMove): legal_actions = [ScoreNorthPlayerAction()] elif isinstance(last_move, DiscardMove): stock_pile_item_ids = self.get_stock_pile_item_ids() can_draw_card = len(stock_pile_item_ids) > settings.stockpile_dead_card_count if settings.max_drawn_card_count < 52: # NOTE: this draw_card_moves = [x for x in moves if isinstance(x, DrawCardMove)] if len(draw_card_moves) >= settings.max_drawn_card_count: can_draw_card = False if can_draw_card: legal_actions = [DrawCardAction()] if settings.is_allowed_pick_up_discard: legal_actions.append(PickUpDiscardAction()) else: legal_actions = [DeclareDeadHandAction()] if settings.is_allowed_pick_up_discard: legal_actions.append(PickUpDiscardAction()) elif isinstance(last_move, KnockMove): legal_actions = [ScoreNorthPlayerAction()] elif isinstance(last_move, ScoreNorthMove): legal_actions = [ScoreSouthPlayerAction()] elif isinstance(last_move, ScoreSouthMove): pass else: raise GinRummyProgramError('get_legal_actions: unknown last_move={}'.format(last_move)) return legal_actions