hanabiapp.game.agent.simple_agent
1from .agent import Agent 2 3class SimpleAgent(Agent): 4 """ 5 シンプルなヒューリスティックに基づいてアクションを選択するエージェント. 6 """ 7 8 def __init__(self, name, player_number,config={}): 9 """ 10 SimpleAgent のコンストラクタ. 11 12 Args: 13 name (str): エージェントの名前 14 player_number (int): プレイヤーの番号 15 config (dict, optional): エージェントの設定(デフォルトは空の辞書) 16 """ 17 super().__init__(name, player_number) 18 self.need_hle_convert= True 19 # Extract max info tokens or set default to 8. 20 self.max_information_tokens = config.get('information_tokens', 8) 21 22 @staticmethod 23 def playable_card(card, fireworks): 24 """ 25 指定されたカードがプレイ可能かを判定する関数. 26 27 Args: 28 card (dict): 確認するカード(色とランクを含む) 29 fireworks (dict): 現在のボード上の状態(各色の最大ランク) 30 31 Returns: 32 bool: カードがプレイ可能であれば True,そうでなければ False 33 """ 34 print(f"card:{card}, fireworks [card['color']]:{fireworks[card['color']]}") 35 if card['rank'] == fireworks[card['color']]: 36 print(f"card [{card}]is playable") 37 return True 38 else: 39 print(f"card [{card}]is NOT playable") 40 return False 41 42 def act(self, observation,rand_ins): 43 """ 44 与えられた観測情報に基づいてアクションを選択する関数. 45 46 - ヒントがある場合,ヒントを基にカードをプレイする. 47 - 相手の手札にプレイ可能なカードがある場合,ヒントを出す. 48 - ヒントトークンが最大数未満なら破棄する. 49 - それ以外の場合はランダムにプレイする. 50 51 Args: 52 observation (dict): 現在のゲーム状態の観測情報 53 rand_ins (random.Random): 乱数生成インスタンス 54 55 Returns: 56 dict: 選択されたアクション(プレイ,破棄,またはヒント) 57 """ 58 if observation['current_player_offset'] != 0: 59 return None 60 61 # Check if there are any pending hints and play the card corresponding to 62 # the hint. 63 for card_index, hint in enumerate(observation['card_knowledge'][0]): 64 if hint['color'] is not None or hint['rank'] is not None: 65 print("自分の手札について,色か数字,どちらかがヒントで明らかになっているのでとりあえずプレイします.(プレイして成功/失敗は考えない)") 66 return {'action_type': 'PLAY', 'card_index': card_index} 67 68 # Check if it's possible to hint a card to your colleagues. 69 fireworks = observation['fireworks'] 70 if observation['information_tokens'] > 0: 71 # Check if there are any playable cards in the hands of the opponents. 72 for player_offset in range(1, observation['num_players']): 73 player_hand = observation['observed_hands'][player_offset] 74 player_hints = observation['card_knowledge'][player_offset] 75 # Check if the card in the hand of the opponent is playable. 76 for card, hint in zip(player_hand, player_hints): 77 if SimpleAgent.playable_card(card, 78 fireworks) and hint['color'] is None: 79 print("相手手札にプレイ可能なカードがあれば,そのカードの色を伝えます") 80 return { 81 'action_type': 'REVEAL_COLOR', 82 'color': card['color'], 83 'target_offset': player_offset 84 } 85 86 # If no card is hintable then discard or play. 87 if observation['information_tokens'] < self.max_information_tokens: 88 print("ヒントトークンが最大数未満なので,最終破棄します.") 89 return {'action_type': 'DISCARD', 'card_index': 0} 90 else: 91 print("ヒントトークンが最大数未満ではないので,最終プレイします.") 92 return {'action_type': 'PLAY', 'card_index': 0}
4class SimpleAgent(Agent): 5 """ 6 シンプルなヒューリスティックに基づいてアクションを選択するエージェント. 7 """ 8 9 def __init__(self, name, player_number,config={}): 10 """ 11 SimpleAgent のコンストラクタ. 12 13 Args: 14 name (str): エージェントの名前 15 player_number (int): プレイヤーの番号 16 config (dict, optional): エージェントの設定(デフォルトは空の辞書) 17 """ 18 super().__init__(name, player_number) 19 self.need_hle_convert= True 20 # Extract max info tokens or set default to 8. 21 self.max_information_tokens = config.get('information_tokens', 8) 22 23 @staticmethod 24 def playable_card(card, fireworks): 25 """ 26 指定されたカードがプレイ可能かを判定する関数. 27 28 Args: 29 card (dict): 確認するカード(色とランクを含む) 30 fireworks (dict): 現在のボード上の状態(各色の最大ランク) 31 32 Returns: 33 bool: カードがプレイ可能であれば True,そうでなければ False 34 """ 35 print(f"card:{card}, fireworks [card['color']]:{fireworks[card['color']]}") 36 if card['rank'] == fireworks[card['color']]: 37 print(f"card [{card}]is playable") 38 return True 39 else: 40 print(f"card [{card}]is NOT playable") 41 return False 42 43 def act(self, observation,rand_ins): 44 """ 45 与えられた観測情報に基づいてアクションを選択する関数. 46 47 - ヒントがある場合,ヒントを基にカードをプレイする. 48 - 相手の手札にプレイ可能なカードがある場合,ヒントを出す. 49 - ヒントトークンが最大数未満なら破棄する. 50 - それ以外の場合はランダムにプレイする. 51 52 Args: 53 observation (dict): 現在のゲーム状態の観測情報 54 rand_ins (random.Random): 乱数生成インスタンス 55 56 Returns: 57 dict: 選択されたアクション(プレイ,破棄,またはヒント) 58 """ 59 if observation['current_player_offset'] != 0: 60 return None 61 62 # Check if there are any pending hints and play the card corresponding to 63 # the hint. 64 for card_index, hint in enumerate(observation['card_knowledge'][0]): 65 if hint['color'] is not None or hint['rank'] is not None: 66 print("自分の手札について,色か数字,どちらかがヒントで明らかになっているのでとりあえずプレイします.(プレイして成功/失敗は考えない)") 67 return {'action_type': 'PLAY', 'card_index': card_index} 68 69 # Check if it's possible to hint a card to your colleagues. 70 fireworks = observation['fireworks'] 71 if observation['information_tokens'] > 0: 72 # Check if there are any playable cards in the hands of the opponents. 73 for player_offset in range(1, observation['num_players']): 74 player_hand = observation['observed_hands'][player_offset] 75 player_hints = observation['card_knowledge'][player_offset] 76 # Check if the card in the hand of the opponent is playable. 77 for card, hint in zip(player_hand, player_hints): 78 if SimpleAgent.playable_card(card, 79 fireworks) and hint['color'] is None: 80 print("相手手札にプレイ可能なカードがあれば,そのカードの色を伝えます") 81 return { 82 'action_type': 'REVEAL_COLOR', 83 'color': card['color'], 84 'target_offset': player_offset 85 } 86 87 # If no card is hintable then discard or play. 88 if observation['information_tokens'] < self.max_information_tokens: 89 print("ヒントトークンが最大数未満なので,最終破棄します.") 90 return {'action_type': 'DISCARD', 'card_index': 0} 91 else: 92 print("ヒントトークンが最大数未満ではないので,最終プレイします.") 93 return {'action_type': 'PLAY', 'card_index': 0}
シンプルなヒューリスティックに基づいてアクションを選択するエージェント.
SimpleAgent(name, player_number, config={})
9 def __init__(self, name, player_number,config={}): 10 """ 11 SimpleAgent のコンストラクタ. 12 13 Args: 14 name (str): エージェントの名前 15 player_number (int): プレイヤーの番号 16 config (dict, optional): エージェントの設定(デフォルトは空の辞書) 17 """ 18 super().__init__(name, player_number) 19 self.need_hle_convert= True 20 # Extract max info tokens or set default to 8. 21 self.max_information_tokens = config.get('information_tokens', 8)
SimpleAgent のコンストラクタ.
Arguments:
- name (str): エージェントの名前
- player_number (int): プレイヤーの番号
- config (dict, optional): エージェントの設定(デフォルトは空の辞書)
@staticmethod
def
playable_card(card, fireworks):
23 @staticmethod 24 def playable_card(card, fireworks): 25 """ 26 指定されたカードがプレイ可能かを判定する関数. 27 28 Args: 29 card (dict): 確認するカード(色とランクを含む) 30 fireworks (dict): 現在のボード上の状態(各色の最大ランク) 31 32 Returns: 33 bool: カードがプレイ可能であれば True,そうでなければ False 34 """ 35 print(f"card:{card}, fireworks [card['color']]:{fireworks[card['color']]}") 36 if card['rank'] == fireworks[card['color']]: 37 print(f"card [{card}]is playable") 38 return True 39 else: 40 print(f"card [{card}]is NOT playable") 41 return False
指定されたカードがプレイ可能かを判定する関数.
Arguments:
- card (dict): 確認するカード(色とランクを含む)
- fireworks (dict): 現在のボード上の状態(各色の最大ランク)
Returns:
bool: カードがプレイ可能であれば True,そうでなければ False
def
act(self, observation, rand_ins):
43 def act(self, observation,rand_ins): 44 """ 45 与えられた観測情報に基づいてアクションを選択する関数. 46 47 - ヒントがある場合,ヒントを基にカードをプレイする. 48 - 相手の手札にプレイ可能なカードがある場合,ヒントを出す. 49 - ヒントトークンが最大数未満なら破棄する. 50 - それ以外の場合はランダムにプレイする. 51 52 Args: 53 observation (dict): 現在のゲーム状態の観測情報 54 rand_ins (random.Random): 乱数生成インスタンス 55 56 Returns: 57 dict: 選択されたアクション(プレイ,破棄,またはヒント) 58 """ 59 if observation['current_player_offset'] != 0: 60 return None 61 62 # Check if there are any pending hints and play the card corresponding to 63 # the hint. 64 for card_index, hint in enumerate(observation['card_knowledge'][0]): 65 if hint['color'] is not None or hint['rank'] is not None: 66 print("自分の手札について,色か数字,どちらかがヒントで明らかになっているのでとりあえずプレイします.(プレイして成功/失敗は考えない)") 67 return {'action_type': 'PLAY', 'card_index': card_index} 68 69 # Check if it's possible to hint a card to your colleagues. 70 fireworks = observation['fireworks'] 71 if observation['information_tokens'] > 0: 72 # Check if there are any playable cards in the hands of the opponents. 73 for player_offset in range(1, observation['num_players']): 74 player_hand = observation['observed_hands'][player_offset] 75 player_hints = observation['card_knowledge'][player_offset] 76 # Check if the card in the hand of the opponent is playable. 77 for card, hint in zip(player_hand, player_hints): 78 if SimpleAgent.playable_card(card, 79 fireworks) and hint['color'] is None: 80 print("相手手札にプレイ可能なカードがあれば,そのカードの色を伝えます") 81 return { 82 'action_type': 'REVEAL_COLOR', 83 'color': card['color'], 84 'target_offset': player_offset 85 } 86 87 # If no card is hintable then discard or play. 88 if observation['information_tokens'] < self.max_information_tokens: 89 print("ヒントトークンが最大数未満なので,最終破棄します.") 90 return {'action_type': 'DISCARD', 'card_index': 0} 91 else: 92 print("ヒントトークンが最大数未満ではないので,最終プレイします.") 93 return {'action_type': 'PLAY', 'card_index': 0}
与えられた観測情報に基づいてアクションを選択する関数.
- ヒントがある場合,ヒントを基にカードをプレイする.
- 相手の手札にプレイ可能なカードがある場合,ヒントを出す.
- ヒントトークンが最大数未満なら破棄する.
- それ以外の場合はランダムにプレイする.
Arguments:
- observation (dict): 現在のゲーム状態の観測情報
- rand_ins (random.Random): 乱数生成インスタンス
Returns:
dict: 選択されたアクション(プレイ,破棄,またはヒント)