def PresetGame(pp, testNr=1): from fireplace import cards cards.db.initialize() for test in range(testNr): class1 = pp.class1 class2 = pp.class2 Dummy1 = DummyAgent("Dummy1", DummyAgent.DummyAI, myClass=class1) Dummy2 = DummyAgent("Dummy2", DummyAgent.DummyAI, myClass=class2) deck1 = random_draft(Dummy1.myClass, []) #random deck wrt its class deck2 = random_draft(Dummy2.myClass, []) #random deck wrt its class player1 = Player(Dummy1.name, deck1, Dummy1.myClass.default_hero) player2 = Player(Dummy2.name, deck2, Dummy2.myClass.default_hero) game = Game(players=(player1, player2)) # Configurations player1._start_hand_size = 3 ## this line must be before 'start()' player2._start_hand_size = 3 ## player1.max_mana = 9 ## this line must be before 'start()' player2.max_mana = 9 ## game.start() player1.hero.max_health = 30 ## this line must be below 'start()' player2.hero.max_health = 30 ## cards_to_mulligan = [] player1.choice.choose(*cards_to_mulligan) player2.choice.choose(*cards_to_mulligan) player1._targetedaction_log = [] player2._targetedaction_log = [] pp(game.current_player).execute(test) pass
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.