def prepare_empty_game(hero1=None, hero2=None, game_class=BaseTestGame): log.info("Initializing a new game with empty decks") heroes = _select_heroes(hero1, hero2) player1 = Player("Player1", [], heroes[0]) player1.cant_fatigue = True player2 = Player("Player2", [], heroes[1]) player2.cant_fatigue = True game = game_class(players=(player1, player2)) game.start() _empty_mulligan(game) return game
def prepare_empty_game(class1=None, class2=None, game_class=BaseTestGame): log.info("Initializing a new game with empty decks") if class1 is None: class1 = _random_class() if class2 is None: class2 = _random_class() player1 = Player("Player1", [], class1.default_hero) player1.cant_fatigue = True player2 = Player("Player2", [], class2.default_hero) player2.cant_fatigue = True game = game_class(players=(player1, player2)) game.start() _empty_mulligan(game) return game
def prepare_empty_game(game_class=BaseTestGame): player1 = Player(name="Player1") player1.prepare_deck([], random.choice(_heroes)) player1.cant_fatigue = True player2 = Player(name="Player2") player2.prepare_deck([], random.choice(_heroes)) player2.cant_fatigue = True game = game_class(players=(player1, player2)) game.start() return game
def main(): deck1 = randomDraft(hero=fireplace.heroes.MAGE) deck2 = randomDraft(hero=fireplace.heroes.WARRIOR) player1 = Player(name="Player1") player1.prepareDeck(deck1, fireplace.heroes.MAGE) player2 = Player(name="Player2") player2.prepareDeck(deck2, fireplace.heroes.WARRIOR) game = Game(players=(player1, player2)) game.start() while True: heropower = game.currentPlayer.hero.power # always play the hero power, just for kicks if heropower.isPlayable(): if heropower.hasTarget(): heropower.play(target=random.choice(heropower.targets)) else: heropower.play() # iterate over our hand and play whatever is playable for card in game.currentPlayer.hand: if card.isPlayable(): if card.hasTarget(): card.play(target=random.choice(card.targets)) else: card.play() else: print("Not playing", card) # Randomly attack with whatever can attack for character in game.currentPlayer.characters: if character.canAttack(): character.attack(random.choice(character.targets)) game.endTurn()
def create_game(self, payload): # self.game_id = payload["GameID"] player_data = payload["Players"] players = [] for player in player_data: p = Player(player["Name"]) # Shuffle the cards to prevent information leaking cards = player["Cards"] random.shuffle(cards) p.prepare_deck(cards, player["Hero"]) players.append(p) INFO("Initializing a Kettle game with players=%r", players) game = Game(players=players) manager = KettleManager(game) game.manager.register(manager) game.current_player = game.players[0] # Dumb. game.start() # Skip mulligan for player in game.players: player.choice = None return manager
def play_full_game(): deck1 = random_draft(hero=MAGE) deck2 = random_draft(hero=WARRIOR) player1 = Player(name="Player1") player1.prepare_deck(deck1, MAGE) player2 = Player(name="Player2") player2.prepare_deck(deck2, WARRIOR) game = Game(players=(player1, player2)) game.start() for player in game.players: print("Can mulligan %r" % (player.choice.cards)) mull_count = random.randint(0, len(player.choice.cards)) cards_to_mulligan = random.sample(player.choice.cards, mull_count) player.choice.choose(*cards_to_mulligan) while True: player = game.current_player heropower = player.hero.power if heropower.is_usable() and random.random() < 0.1: if heropower.has_target(): heropower.use(target=random.choice(heropower.targets)) else: heropower.use() continue # iterate over our hand and play whatever is playable for card in player.hand: if card.is_playable() and random.random() < 0.5: target = None if card.choose_cards: card = random.choice(card.choose_cards) if card.has_target(): target = random.choice(card.targets) print("Playing %r on %r" % (card, target)) card.play(target=target) continue # Randomly attack with whatever can attack for character in player.characters: if character.can_attack(): character.attack(random.choice(character.targets)) continue if player.choice: choice = random.choice(player.choice.cards) print("Choosing card %r" % (choice)) player.choice.choose(choice) continue game.end_turn()
def play_game(hero1, hero2): deck1 = decks[hero1] player1 = Player(name="Player1") player1.prepare_deck(deck1, hero1) deck2 = decks[hero2] player2 = Player(name="Player2") player2.prepare_deck(deck2, hero2) game = Game(players=(player1, player2)) game.start() player1.choice.choose(*[]) player2.choice.choose(*[]) while game.state != State.COMPLETE: try: heropower = game.current_player.hero.power if heropower.is_usable(): if heropower.has_target(): heropower.use(target=random.choice(heropower.targets)) else: heropower.use() for card in game.current_player.hand: if card.is_playable(): target = None if card.choose_cards: card = random.choice(card.choose_cards) if card.has_target(): target = random.choice(card.targets) card.play(target=target) for character in game.current_player.characters: if character.can_attack(): character.attack(random.choice(character.targets)) game.end_turn() except GameOver: pass if player1.playstate == PlayState.TIED: return None if player1.playstate == PlayState.WON: return player1 return player2
def create_game(self, payload): # self.game_id = payload["GameID"] player_data = payload["Players"] players = [] for player in player_data: # Shuffle the cards to prevent information leaking cards = player["Cards"] random.shuffle(cards) p = Player(player["Name"], cards, player["Hero"]) players.append(p) INFO("Initializing a Kettle game with players=%r", players) game = Game(players=players) manager = KettleManager(game) game.manager.register(manager) game.current_player = game.players[0] # Dumb. game.start() # Skip mulligan for player in game.players: player.choice = None return manager
def main(): deck1 = random_draft(hero=MAGE) deck2 = random_draft(hero=WARRIOR) player1 = Player(name="Player1") player1.prepare_deck(deck1, MAGE) player2 = Player(name="Player2") player2.prepare_deck(deck2, WARRIOR) game = Game(players=(player1, player2)) game.start() for player in game.players: print("Can mulligan %r" % (player.choice.cards)) mull_count = random.randint(0, len(player.choice.cards)) cards_to_mulligan = random.sample(player.choice.cards, mull_count) player.choice.choose(*cards_to_mulligan) while True: heropower = game.current_player.hero.power # always play the hero power, just for kicks if heropower.is_usable(): if heropower.has_target(): heropower.use(target=random.choice(heropower.targets)) else: heropower.use() # iterate over our hand and play whatever is playable for card in game.current_player.hand: if card.is_playable(): target = None choice = None if card.has_target(): target = random.choice(card.targets) if card.data.choose_cards: choice = random.choice(card.data.choose_cards) card.play(target=target, choose=choice) else: print("Not playing", card) # Randomly attack with whatever can attack for character in game.current_player.characters: if character.can_attack(): character.attack(random.choice(character.targets)) game.end_turn()
def play_game(hero1, hero2): open(log_file, 'w').close() deck1 = random_draft(hero=hero1) deck1[0] = "EX1_160a" player1 = Player(name="Player1") player1.prepare_deck(deck1, hero1) deck2 = random_draft(hero=hero2) deck2[0] = "EX1_160a" player2 = Player(name="Player2") player2.prepare_deck(deck2, hero2) game = Game(players=(player1, player2)) game.start() player1.choice.choose(*[]) player2.choice.choose(*[]) while game.state != State.COMPLETE: try: heropower = game.current_player.hero.power if heropower.is_usable(): if heropower.has_target(): heropower.use(target=random.choice(heropower.targets)) else: heropower.use() for card in game.current_player.hand: if card.is_playable(): target = None if card.choose_cards: card = random.choice(card.choose_cards) if card.has_target(): target = random.choice(card.targets) card.play(target=target) for character in game.current_player.characters: if character.can_attack(): character.attack(random.choice(character.targets)) game.end_turn() except GameOver: pass
def prepare_game(hero1=None, hero2=None, exclude=(), game_class=BaseTestGame): print("Initializing a new game") if hero1 is None: hero1 = random.choice(_heroes) if hero2 is None: hero2 = random.choice(_heroes) deck1 = _draft(hero=hero1, exclude=exclude) deck2 = _draft(hero=hero2, exclude=exclude) player1 = Player(name="Player1") player1.prepare_deck(deck1, hero1) player2 = Player(name="Player2") player2.prepare_deck(deck2, hero2) game = game_class(players=(player1, player2)) game.start() # Do empty mulligans for player in game.players: if player.choice: player.choice.choose() return game
def _prepare_player(name, hero, deck=[]): player = Player(name) player.prepare_deck(deck, hero) return player
def choose_deck(self, name): if name not in self.decks: raise DeckNotExists self.current_deck_name = name deck = self.decks[name] self.player = Player(self.id, deck.card_list, deck.hero)
def play_one_game(P1: Agent, P2: Agent, deck1=[], deck2=[], debugLog=True, HEROHPOPTION=30, P1MAXMANA=1, P2MAXMANA=1, P1HAND=3, P2HAND=3): """ エージェント同士で1回対戦する。 実験的に、ヒーローの体力、初期マナ数、初期ハンド枚数をコントロールできます。 play one game by P1 and P2 """ from fireplace.utils import random_draft from fireplace.player import Player import random exclude = [] # you may exclude some cards to construct a deck log.info("New game settings") if len(deck1) == 0: deck1 = random_draft(P1.myClass, exclude) #random deck wrt its class if len(deck2) == 0: deck2 = random_draft(P2.myClass, exclude) #random deck wrt its class player1 = Player(P1.name, deck1, P1.myClass.default_hero) player2 = Player(P2.name, deck2, P2.myClass.default_hero) player1.choiceStrategy = P1.choiceStrategy player2.choiceStrategy = P2.choiceStrategy game = Game(players=(player1, player2)) # Configurations player1._start_hand_size = P1HAND ## this line must be before 'start()' player2._start_hand_size = P2HAND ## player1.max_mana = int( P1MAXMANA) - 1 ## this line must be before 'start()' player2.max_mana = int(P2MAXMANA) - 1 game.start() player1.hero.max_health = int( HEROHPOPTION) ## this line must be below 'start()' player2.hero.max_health = int(HEROHPOPTION) ## #mulligan exchange # Any agent are allowed to give an algorithm to do mulligan exchange. for player in game.players: if player.name == P1.name: if P1.mulliganStrategy == None: mull_count = random.randint(0, len(player.choice.cards)) cards_to_mulligan = random.sample(player.choice.cards, mull_count) else: cards_to_mulligan = P1.mulliganStrategy( P1, player.choice.cards) elif player.name == P2.name: if P2.mulliganStrategy == None: mull_count = random.randint(0, len(player.choice.cards)) cards_to_mulligan = random.sample(player.choice.cards, mull_count) else: cards_to_mulligan = P2.mulliganStrategy( P2, player.choice.cards) player.choice.choose(*cards_to_mulligan) # includes begin_turn() #mulligan exchange end log.info("New game start") while True: #game main loop player = game.current_player start_time = time.time() if player.name == P1.name: #please make each Agent.func has arguments 'self, game, option, gameLog, debugLog' P1.func(P1, game, option=P1.option, gameLog=game.get_log(), debugLog=debugLog) elif player.name == P2.name: #please make each Agent.func has arguments 'self, game, option, gameLog, debugLog' P2.func(P2, game, option=P2.option, gameLog=game.get_log(), debugLog=debugLog) else: Original_random(game) #random player by fireplace #turn end procedure from here if player.choice != None: player.choice = None #somotimes it comes here if game.state != State.COMPLETE: try: game.end_turn() if debugLog: print(">>>>%s>>>>turn change %d[sec]>>>>%s" % (player, time.time() - start_time, player.opponent), end=' ') print("%d : %d" % (player1.hero.health + player1.hero.armor, player2.hero.health + player2.hero.armor)) if game.current_player.choice != None: postAction(game.current_player) except GameOver: #it rarely occurs gameover = 0 #ゲーム終了フラグが立っていたらゲーム終了処理を行う #if game was over if game.state == State.COMPLETE: if debugLog: print(">>>>>>>>>>game end >>>>>>>>" % (), end=' ') print("%d : %d" % (player1.hero.health, player2.hero.health)) if game.current_player.playstate == PlayState.WON: return game.current_player.name if game.current_player.playstate == PlayState.LOST: return game.current_player.opponent.name return 'DRAW' #Maybe impossible to come here.
def find_card_pair(onlyresult=1): card_class = CardClass.MAGE #カードクラスを設定することは必須 allCards = get_all_cards(card_class, costMax=2) vanillas = get_all_vanillas(allCards) nonVanillas = get_all_non_vanillas(allCards) spells = get_all_spells(allCards) #良いカードペアを漠然と探す旅に出る2枚は呪文カードしばり for matchNumber in range(10): count1 = 0 count2 = 0 #ヒーローパワーを消す、というのもよいかも。 nonvanillaName1 = random.choice(nonVanillas) nonvanilla1 = nonvanillaName1.id nonvanillaName2 = random.choice(spells) #この呪文によって、まえのカードが活かされるかどうか nonvanilla2 = nonvanillaName2.id print(" specific cards : %r%r" % (nonvanillaName1, nonvanillaName2)) for repeat in range(25): if onlyresult == 0: print(" GAME %d" % repeat, end=" -> ") #set decks and players deck1 = [] position = random.randint(1, 7) for i in range(position): deck1.append((random.choice(vanillas)).id) deck1.append(nonvanilla1) deck1.append(nonvanilla2) for i in range(8 - position): #デッキは10枚 deck1.append(random.choice(vanillas).id) player1 = Player("Player1", deck1, card_class.default_hero) deck2 = copy.deepcopy(deck1) random.shuffle(deck2) player2 = Player("Player2", deck2, card_class.default_hero) #set a game game = Game(players=(player1, player2)) game.start() for player in game.players: #print("Can mulligan %r" % (player.choice.cards)) mull_count = random.randint(0, len(player.choice.cards)) cards_to_mulligan = random.sample(player.choice.cards, mull_count) player.choice.choose(*cards_to_mulligan) game.player1.hero.max_health = 10 game.player2.hero.max_health = 10 turnNumber = 0 if onlyresult == 0: print("Turn ", end=':') while True: turnNumber += 1 if onlyresult == 0: print(turnNumber, end=":") #StandardRandom(game)#ここはもう少し賢くする weight = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] weight[0] = weight[1] = 5 weight[26] = 10 weight[2] = weight[3] = weight[6] = weight[7] = 5 weight[10] = weight[11] = weight[12] = weight[13] = 5 StandardStep1(game, debugLog=False) if game.state != State.COMPLETE: try: game.end_turn() except GameOver: #まれにおこる pass if game.state == State.COMPLETE: if game.current_player.playstate == PlayState.WON: winner = game.current_player.name break elif game.current_player.playstate == PlayState.LOST: winner = game.current_player.opponent.name break else: winner = "DRAW" break if onlyresult == 0: print("%s won." % winner) if winner == "Player1": if onlyresult == 1: print("O", end=".") count1 += 1 elif winner == "Player2": if onlyresult == 1: print("X", end=".") count2 += 1 print("(%d : %d)" % (count1, count2), end=" ") #25戦で10点差があれば有意な差あり if count1 - count2 >= 10: print("Significant") else: print("") pass
def investigate_card_pair(onlyresult=0): card_class = CardClass.HUNTER allCards = get_all_cards(card_class) vanillas = get_all_vanillas(allCards) nonVanillas = get_all_non_vanillas(allCards) count1 = 0 count2 = 0 #ヒーローパワーを消す、というのもよいかも。 #良いカードペアを漠然と探す旅に出る? nonvanilla1 = 'EX1_045' #random.choice(nonVanillas).id#自分で指定してもよい nonvanilla2 = 'EX1_332' #random.choice(nonVanillas).id# #古代の番人:EX1_045:攻撃できない。 #沈黙:EX1_332:ミニオン1体を沈黙させる print(" specific cards : %r%r" % (nonvanilla1, nonvanilla2)) for repeat in range(50): print(" GAME %d" % repeat, end=" -> ") #set decks and players deck1 = [] position = random.randint(1, 7) for i in range(position): deck1.append((random.choice(vanillas)).id) deck1.append(nonvanilla1) deck1.append(nonvanilla2) for i in range(8 - position): #デッキは10枚 deck1.append(random.choice(vanillas).id) player1 = Player("AAAA", deck1, card_class.default_hero) deck2 = copy.deepcopy(deck1) random.shuffle(deck2) player2 = Player("BBBB", deck2, card_class.default_hero) #set a game game = Game(players=(player1, player2)) game.start() for player in game.players: #print("Can mulligan %r" % (player.choice.cards)) mull_count = random.randint(0, len(player.choice.cards)) cards_to_mulligan = random.sample(player.choice.cards, mull_count) player.choice.choose(*cards_to_mulligan) game.player1.hero.max_health = 10 # ヒーローのHPは10 game.player2.hero.max_health = 10 turnNumber = 0 print("Turn ", end=':') while True: turnNumber += 1 print(turnNumber, end=":") weight = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] weight[0] = weight[1] = 5 weight[26] = 10 weight[2] = weight[3] = weight[6] = weight[7] = 5 weight[10] = weight[11] = weight[12] = weight[13] = 5 StandardStep1(game, debugLog=False) #StandardRandom(game,debugLog=True) #ここはもう少し賢い人にやってほしい if game.state != State.COMPLETE: try: game.end_turn() except GameOver: #まれにおこる pass else: if game.current_player.playstate == PlayState.WON: winner = game.current_player.name break elif game.current_player.playstate == PlayState.LOST: winner = game.current_player.opponent.name break else: winner = "DRAW" break print("%s won." % winner) if winner == "AAAA": count1 += 1 elif winner == "BBBB": count2 += 1 print("%d : %d" % (count1, count2))
def play_one_game(P1: Agent, P2: Agent, deck1=[], deck2=[], HeroHPOption=30, debugLog=True): from fireplace.utils import random_draft from fireplace.player import Player import random #バグが確認されているものを当面除外する exclude = ['CFM_672', 'CFM_621', 'CFM_095', 'LOE_076', 'BT_490'] # 'LOE_076' : Sir Finley Mrrgglton # 'BT_490' : 魔力喰い、ターゲットの扱いにエラーがあるので除外。 if len(deck1) == 0: deck1 = random_draft(P1.myClass, exclude) #ランダムなデッキ if len(deck2) == 0: deck2 = random_draft(P2.myClass, exclude) #ランダムなデッキ player1 = Player(P1.name, deck1, P1.myClass.default_hero) player2 = Player(P2.name, deck2, P2.myClass.default_hero) game = Game(players=(player1, player2)) game.start() for player in game.players: #print("Can mulligan %r" % (player.choice.cards)) mull_count = random.randint(0, len(player.choice.cards)) cards_to_mulligan = random.sample(player.choice.cards, mull_count) player.choice.choose(*cards_to_mulligan) if HeroHPOption != 30: game.player1.hero.max_health = HeroHPOption game.player2.hero.max_health = HeroHPOption while True: from agent_Standard import StandardRandom, HumanInput, Original_random from agent_Maya import Maya_MCTS player = game.current_player #print("%r starts their turn."%player.name); if player.name == "Maya": Maya_MCTS(game) #マヤ氏の作品 elif player.name == "Standard": StandardRandom( game, debugLog=debugLog) #公式のランダムより、もう少しキチンとしたランダムプレーエージェント elif player.name == "Human": HumanInput(game) #人がプレーするときはこれ elif player.name == P1.name: P1.func(game, option=P1.option, debugLog=debugLog) #P1.funcには引数option, debugLogを作ってください elif player.name == P2.name: P2.func(game, option=P2.option, debugLog=debugLog) #P2.funcには引数option, debugLogを作ってください else: Original_random(game) #公式のランダム if player.choice != None: player.choice = None #論理的にはおこらないが、ときどきおこる if game.state != State.COMPLETE: try: game.end_turn() except GameOver: #まれにおこる gameover = 0 if game.state == State.COMPLETE: #ゲーム終了フラグが立っていたら if game.current_player.playstate == PlayState.WON: return game.current_player.name if game.current_player.playstate == PlayState.LOST: return game.current_player.opponent.name return 'DRAW'
from fireplace import cards from fireplace.cards.heroes import * from fireplace.exceptions import GameOver from fireplace.entity import Entity from fireplace.game import Game from fireplace.player import Player from fireplace.utils import random_draft from hearthstone.enums import PlayState cards.db.initialize() fireplace.logging.log.setLevel(logging.WARN) deck1 = random_draft(SHAMAN) deck2 = random_draft(PALADIN) player1 = Player("Player1", deck1, SHAMAN) player2 = Player("Player2", deck2, PALADIN) game = Game(players=(player1, player2)) game.start() for player in game.players: if player.choice: player.choice.choose() begin_time = datetime.datetime.utcnow() N = 100 for i in range(N): game2 = copy.deepcopy(game) end_time = datetime.datetime.utcnow()
def player(self): if self._player is not None: return self._player else: self._player = Player(self.name, self.deck, self.hero) return self._player