def play( self, player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ) -> int: empty_piles = 0 for stack in game.supply: if len(game.supply[stack]) == 0: empty_piles += 1 for _ in range(empty_piles): if len(player.hand) > 0: dis_card = player.choose_discard( "choose a card from your hand to discard", game, network, mock=mock, ) c = getcard(dis_card, game.supply, player.hand, "your hand") if c: player.hand.remove(c) c.ondiscard(player, game, network, mock) player.discard.append(c) else: raise ValueError() return 0
def play( self, player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ) -> int: trashed = 0 while trashed < 4 and len(player.hand) > 0: trashcard = player.choose_discard( player.name + ", choose a card from your hand to discard: ", game, network, optional=True, trash_card=True, mock=mock, ) if trashcard is None: break c = getcard(trashcard, game.supply, player.hand, "your hand") if c: game.trash.append(c) c.ontrash(player, game, network, mock) player.hand.remove(c) trashed += 1 else: raise ValueError() return 0
def discard_and_get_game_state( self, game: Game.Game, discard: Card, network: DominionNetwork, additional_draw: int = 0, ondeck: bool = False, trash_card: bool = False, ) -> Tuple[List[List[float]], List[int], List[List[float]], List[int], List[List[float]], List[float], ]: game_copy = deepcopy(game) self_copy = deepcopy(self) if discard: c = getcard(discard.name, game_copy.supply, self_copy.hand, "your hand") self_copy.hand.remove(c) if ondeck: self_copy.deck.insert(0, c) elif trash_card: game_copy.trash.append(c) c.ontrash(self_copy, game_copy, network, mock=True) else: self_copy.discard.append(c) c.ondiscard(self_copy, game_copy, network, mock=True) game_state = game_copy.get_game_state(self_copy, additional_draw) game_copy = None self_copy = None return game_state
def play_action_and_get_game_state( self, game: Game.Game, action: Card, network: DominionNetwork, ) -> Tuple[List[List[float]], List[int], List[List[float]], List[int], List[List[float]], List[float], ]: game_copy = deepcopy(game) self_copy = deepcopy(self) if action: c = getcard(action.name, game_copy.supply, self_copy.hand, "your hand", ["action"]) self_copy.cprint(self_copy.name + " thought about playing " + c.name + ". ") cards_from_deck = self_copy.playcard( c, game_copy, network, draw=False, mock=True, ) else: self_copy.cprint(self_copy.name + " thought about playing nothing. ") self_copy.actions = 0 cards_from_deck = 0 game_state = game_copy.get_game_state(self_copy, cards_from_deck) self_copy = None game_copy = None return game_state
def play_treasure_and_get_game_state( self, game: Game.Game, treasure: Card, network: DominionNetwork, ) -> Tuple[List[List[float]], List[int], List[List[float]], List[int], List[List[float]], List[float], ]: game_copy = deepcopy(game) self_copy = deepcopy(self) if treasure is not None: c = getcard(treasure.name, game_copy.supply, self_copy.hand, "your hand", ["coin"]) c.onuse(self_copy, game_copy, network, mock=True) self_copy.hand.remove(c) self_copy.played.append(c) self_copy.treasures_played += 1 self_copy.cprint(self_copy.name + " thought about playing " + c.name + ". ") else: self_copy.cprint(self_copy.name + " thought about playing nothing. ") game_state = game_copy.get_game_state(self_copy) game_copy = None self_copy = None return game_state
def play( self, player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ) -> int: if "coin" in catinlist(player.hand): this_card = player.choose_discard( player.name + ", choose a card from your hand to trash: ", game, network, constraint="coin", optional=True, trash_card=True, mock=mock, ) if this_card: c = getcard(this_card, game.supply, player.hand, "your hand", ["coin"]) if c: game.trash.append(c) c.ontrash(player, game, network, mock) player.hand.remove(c) player.gain_card( game, network, c.cost + 3, constraint="coin", mock=mock, destination="hand", ) else: raise ValueError() return 0
def play( self, player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ) -> int: discarded = 0 while len(player.hand) > 0: dis_card = player.choose_discard( player.name + ", choose a card from your hand to discard: ", game, network, additional_draw=1, optional=True, mock=mock, ) if dis_card is None: break c = getcard(dis_card, game.supply, player.hand, "your hand") if c: player.discard.append(c) c.ondiscard(player, game, network, mock) player.hand.remove(c) discarded += 1 for _ in range(discarded): if not mock: player.draw() return discarded
def onbuy( self, player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ): if len(player.hand) > 0: trashcard = player.choose_discard( player.name + ", choose a card from your hand to discard: ", game, network, trash_card=True, mock=mock, ) c = getcard(trashcard, game.supply, player.hand, "your hand") if c: game.trash.append(c) c.ontrash(player, game, network, mock) player.hand.remove(c) else: raise ValueError() player.gain_card( game, network, c.cost + 2, exact=True, mock=mock, )
def play( self, player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ) -> int: trashed = 0 while (len(player.hand) > 0) and (trashed < 2): this_card = player.choose_discard( player.name + ", choose a card from your hand to discard: ", game, network, trash_card=True, mock=mock, ) if this_card: c = getcard(this_card, game.supply, player.hand, "your hand") game.trash.append(c) player.hand.remove(c) c.ontrash(player, game, network, mock) trashed += 1 if len(game.supply["Silver"]) > 0: player.hand.append(game.supply["Silver"].pop()) return 0
def gain_card( self, game: Game.Game, network: DominionNetwork, upto: int, exact: bool = False, optional: bool = False, mock: bool = False, constraint: str = None, destination: str = None, ) -> str: card = self.choose_buy( game, network, upto, exact=exact, optional=optional, gain=True, mock=mock, constraint=constraint, destination=destination, ) if card: c = getcard(card, game.supply, upto=upto) if destination == "hand": self.hand.append(game.supply[card].pop()) else: self.discard.append(game.supply[card].pop()) self.cprint(self.name + " gained " + c.name + ". ") c.ongain(self, game, network, mock=False) self.cards_gained += 1 return card return None
def play( self, player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ) -> int: if len(player.hand) > 0: this_card = player.choose_discard( player.name + ", choose a card from your hand to discard: ", game, network, trash_card=True, mock=mock, ) if this_card: c = getcard(this_card, game.supply, player.hand, "your hand") game.trash.append(c) player.hand.remove(c) c.ontrash(player, game, network, mock) player.gain_card( game, network, c.cost + 2, mock=mock, ) return 0
def yes_callback(player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool) -> int: c = getcard("Estate", game.supply, player.hand, "your hand") if c: player.hand.remove(c) player.discard.append(c) player.purse += 4 return 0
def yes_callback(player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool) -> int: has_mining_village = False for c in player.played: if c.name == "Mining Village": has_mining_village = True if not has_mining_village: return 0 c = getcard("Mining Village", game.supply, player.played, "your hand") game.trash.append(c) player.played.remove(c) player.purse += 1 return 0
def play( self, player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ) -> int: has_copper = False for card in player.hand: if "Copper" in card.name: has_copper = True if has_copper: c = getcard("Copper", game.supply, player.hand, "your hand") game.trash.append(c) player.hand.remove(c) player.purse += 3 return 0
def yes_callback(player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool) -> int: for _ in range(2): dis_card = player.choose_discard( player.name + ", choose a card from your hand to discard: ", game, network, mock=mock, ) c = getcard(dis_card, game.supply, player.hand, "your hand") if c: player.discard.append(c) c.ondiscard(player, game, network, mock) player.hand.remove(c) else: raise ValueError() player.purse += 2 return 0
def play( self, player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ) -> int: if len(player.hand) > 0: dis_card = player.choose_discard( " which card do you want to put on top of your deck?\n--> ", game, network, ondeck=True, mock=mock, ) c = getcard(dis_card, game.supply, player.hand, "your hand") player.hand.remove(c) player.deck.insert(0, c) return 0
def play( self, this_player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ) -> int: for player in players_around(game.players, this_player, inclusive=False): has_moat = False if len(player.hand) > 3: for c in player.hand: if c.react(player, game, network, mock) or mock: has_moat = True break else: player.show(lead="\n\n") while len(player.hand) > 3: dis_card = player.choose_discard( player.name + ", choose a card from your hand to discard: ", game, network, mock=mock, ) print("Discard " + dis_card) if dis_card: c = getcard(dis_card, game.supply, player.hand, "your hand") if c: player.hand.remove(c) c.ondiscard(player, game, network, mock) player.discard.append(c) else: raise ValueError() if (not mock) and (len(player.hand) > 3) and (not has_moat): raise ValueError() return 0
def play( self, player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ) -> int: d = 0 choice = player.choose_action(game, network, mock=mock, optional=False) if choice: c = getcard(choice, game.supply, player.hand, " your hand", ["action"]) c.use(player, game.trash) c.augment(player) d += c.play(player, game, network, mock) player.show() c.augment(player) d += c.play(player, game, network, mock) return d
def buy_card_and_get_game_state( self, game: Game.Game, buy: Card, network: DominionNetwork, gain: bool = False, destination: str = None, ) -> Tuple[List[List[float]], List[int], List[List[float]], List[int], List[List[float]], List[float], ]: game_copy = deepcopy(game) self_copy = deepcopy(self) if buy: c = getcard(buy.name, game_copy.supply) if c: if destination == "hand": self_copy.hand.append(game_copy.supply[buy.name].pop()) else: self_copy.discard.append(game_copy.supply[buy.name].pop()) if not gain: self_copy.buys = self_copy.buys - 1 self_copy.purse = self_copy.purse - c.cost if self_copy.purse < 0: raise ValueError c.onbuy(self_copy, game_copy, network, mock=True) c.ongain(self_copy, game_copy, network, mock=True) self_copy.cprint(self_copy.name + " thought about buying " + c.name + ". ") else: self_copy.cprint(self_copy.name + " thought about buying nothing. ") if not gain: self_copy.buys = 0 game_state = game_copy.get_game_state(self_copy) game_copy = None self_copy = None return game_state
def play( self, this_player: Player.Player, game: Game.Game, network: DominionNetwork, mock: bool, ) -> int: if len(game.supply["Silver"]) > 0: this_player.deck.insert(0, game.supply["Silver"].pop()) for player in players_around(game.players, this_player, inclusive=False): for c in player.hand: if c.react(player, game, network, mock) or mock: break else: if "victory" in catinlist(player.hand): putback = player.choose_discard( player.name + ", which victory card" + " do you want to put on top of your deck?\n--> ", game, network, constraint="victory", ondeck=True, mock=mock, ) c = getcard(putback, game.supply, player.hand, "your hand", ["victory"]) if c: player.hand.remove(c) player.deck.insert(0, c) else: raise ValueError() else: player.show(lead="\n\n") return 0
def take_turn(self, player: Player.Player, network: DominionNetwork): player.start_turn() print(player.name + "'s turn") print("Starting hand") for c in player.hand: print(c.name) # Action phase while player.actions > 0: move_to_play = player.choose_action(self, network) if move_to_play: print("Playing " + move_to_play) c = getcard( move_to_play, self.supply, player.hand, "your hand", ["action"] ) player.playcard(c, self, network) player.actions_played += 1 if player.type != "neural_network": board_state = self.get_game_state(player) hand = board_state[0] if len(hand) == 0: print("Empty hand") hand = np.zeros((1, 33)) hand_one_hot = board_state[1] if len(hand_one_hot) == 0: hand_one_hot = [0] player.current_game_hand_states.append(hand) player.current_game_hand_one_hot.append(hand_one_hot) player.current_game_kingdom_states.append(board_state[2]) player.current_game_kingdom_one_hot.append(board_state[3]) player.current_game_opponent_states.append(board_state[4]) player.current_game_states.append(board_state[5]) else: player.actions = 0 if player.type != "neural_network": board_state = self.get_game_state(player) hand = board_state[0] if len(hand) == 0: print("Empty hand") hand = np.zeros((1, 33)) hand_one_hot = board_state[1] if len(hand_one_hot) == 0: hand_one_hot = [0] player.current_game_hand_states.append(hand) player.current_game_hand_one_hot.append(hand_one_hot) player.current_game_kingdom_states.append(board_state[2]) player.current_game_kingdom_one_hot.append(board_state[3]) player.current_game_opponent_states.append(board_state[4]) player.current_game_states.append(board_state[5]) break print("Hand after playing actions") for c in player.hand: print(c.name) # Play treasures while len(player.hand) > 0: to_play = player.choose_treasure(self, network) if to_play: print("Using " + to_play) c = getcard(to_play, self.supply, player.hand, "your hand", ["coin"]) c.onuse(player, self, network, mock=False) player.hand.remove(c) player.played.append(c) player.treasures_played += 1 if player.type != "neural_network": board_state = self.get_game_state(player) hand = board_state[0] if len(hand) == 0: print("Empty hand") hand = np.zeros((1, 33)) hand_one_hot = board_state[1] if len(hand_one_hot) == 0: hand_one_hot = [0] player.current_game_hand_states.append(hand) player.current_game_hand_one_hot.append(hand_one_hot) player.current_game_kingdom_states.append(board_state[2]) player.current_game_kingdom_one_hot.append(board_state[3]) player.current_game_opponent_states.append(board_state[4]) player.current_game_states.append(board_state[5]) else: if player.type != "neural_network": board_state = self.get_game_state(player) hand = board_state[0] if len(hand) == 0: print("Empty hand") hand = np.zeros((1, 33)) hand_one_hot = board_state[1] if len(hand_one_hot) == 0: hand_one_hot = [0] player.current_game_hand_states.append(hand) player.current_game_hand_one_hot.append(hand_one_hot) player.current_game_kingdom_states.append(board_state[2]) player.current_game_kingdom_one_hot.append(board_state[3]) player.current_game_opponent_states.append(board_state[4]) player.current_game_states.append(board_state[5]) break print("Hand after playing treasures") for c in player.hand: print(c.name) # Buy phase print("Total coins: " + str(player.purse)) while player.buys > 0: purchase = player.choose_buy( self, network, upto=player.purse, optional=True ) if purchase: c = getcard(purchase, self.supply, upto=player.purse) print("Purchased " + purchase) player.discard.append(self.supply[purchase].pop()) player.buys = player.buys - 1 player.purse = player.purse - c.cost player.cprint(player.name + " bought " + c.name + ". ") c.ongain(player, self, network, mock=False) c.onbuy(player, self, network, mock=False) player.cards_bought += 1 if player.type != "neural_network": board_state = self.get_game_state(player) hand = board_state[0] if len(hand) == 0: print("Empty hand") hand = np.zeros((1, 33)) hand_one_hot = board_state[1] if len(hand_one_hot) == 0: hand_one_hot = [0] player.current_game_hand_states.append(hand) player.current_game_hand_one_hot.append(hand_one_hot) player.current_game_kingdom_states.append(board_state[2]) player.current_game_kingdom_one_hot.append(board_state[3]) player.current_game_opponent_states.append(board_state[4]) player.current_game_states.append(board_state[5]) else: player.buys = 0 if player.type != "neural_network": board_state = self.get_game_state(player) hand = board_state[0] if len(hand) == 0: print("Empty hand") hand = np.zeros((1, 33)) hand_one_hot = board_state[1] if len(hand_one_hot) == 0: hand_one_hot = [0] player.current_game_hand_states.append(hand) player.current_game_hand_one_hot.append(hand_one_hot) player.current_game_kingdom_states.append(board_state[2]) player.current_game_kingdom_one_hot.append(board_state[3]) player.current_game_opponent_states.append(board_state[4]) player.current_game_states.append(board_state[5]) player.cleanup()