def DummyAI(self,
             thisgame: ".game.Game",
             option=[],
             gameLog=[],
             debugLog=False):
     player = thisgame.current_player
     loopCount = 0
     ##########
     #new_game = deepcopy_game(thisgame, player, 0)
     #candidate1 = getCandidates(thisgame)
     #candidate2 = getCandidates(new_game)
     #compaireCandidates(candidate1, candidate2)
     ##########
     while loopCount < 20:
         ##########
         #debug_board(new_game,thisgame)#
         ##########
         loopCount += 1
         myCandidate = getCandidates(thisgame)
         if len(myCandidate) > 0:
             myChoice = random.choice(myCandidate)
             ##########
             #executeAction(new_game, myChoice, debugLog=debugLog)
             #postAction(new_game.current_player)
             ##########
             exc = executeAction(thisgame, myChoice, debugLog=debugLog)
             postAction(player)
             if exc == ExceptionPlay.GAMEOVER:
                 ##########
                 #debug_board(new_game,thisgame)#
                 ##########
                 return ExceptionPlay.GAMEOVER
             else:
                 continue
         return ExceptionPlay.VALID
Example #2
0
 def Maya_MCTS(self,
               thisgame: ".game.Game",
               option=[],
               gameLog=[],
               debugLog=False):
     player = thisgame.current_player
     print("--------------------simulate start!!----------------")
     while True:
         #探索編
         candidates = getCandidates(thisgame, _includeTurnEnd=True)
         if len(candidates) == 0:  #ここは1になっていたが、0だと思われる。
             print("len(candidates)==0")
             return ExceptionPlay.VALID
             pass
         takingAction = try_montecarlo_tree_search(thisgame, candidates)
         print("--------------------simulate end!!------------------")
         # print(takingAction)
         # iterate over our hand and play whatever is playable
         #多分executeActionで大丈夫だろ
         if takingAction.type == ExceptionPlay.TURNEND:
             return ExceptionPlay.VALID
             pass
         exc = executeAction(thisgame, takingAction)
         postAction(player)
         if exc == ExceptionPlay.GAMEOVER:
             return ExceptionPlay.GAMEOVER
         else:
             continue
     return ExceptionPlay.VALID
     pass
 def change_turn(self, player):
     game = player.game
     if player.choice != None:
         player.choice = None  #somotimes it comes here
     game.end_turn()
     postAction(player)
     current = player.opponent
     pass
 def preset_play(self):
     super().preset_play()
     player = self.player
     opponent = player.opponent
     game = player.game
     self.play_card(self.mark2, player)
     self.play_card(self.mark3, player)
     game.end_turn()
     postAction(player)
     game.end_turn()
     postAction(opponent)
     self.play_card(self.mark1, player)
     pass
 def preset_play(self):
     super().preset_play()
     player = self.player
     opponent = player.opponent
     game = player.game
     self.play_card(self.mark2, player)
     game.end_turn()
     postAction(player)
     self.play_card(self.mark3, opponent)
     self.play_card(self.mark4, opponent)
     game.end_turn()
     postAction(opponent)
     if self.testNr == 0:
         self.play_card(self.mark1, player, target=self.mark3)
     elif self.testNr == 1:
         self.play_card(self.mark1, player, target=self.mark4)
 def preset_play(self):
     super().preset_play()
     player = self.player
     opponent = player.opponent
     game = player.game
     self.play_card(self.mark2, player)
     self.play_card(self.mark3, player)
     if player.choice != None:
         player.choice = None  #somotimes it comes here
     game.end_turn()
     postAction(player)
     if opponent.choice != None:
         opponent.choice = None  #somotimes it comes here
     game.end_turn()
     postAction(opponent)
     self.play_card(self.mark1, player)
Example #7
0
 def preset_play(self):
     super().preset_play()
     controller = self.player
     opponent = controller.opponent
     game = controller.game
     ##########controller
     self.play_card(self.mark1, controller)  #
     postAction(controller)
     self.change_turn(controller)
     ##########opponent
     #self.play_card(self.mark1, opponent)#
     self.change_turn(opponent)
     ##########controller
     self.change_turn(controller)
     ##########opponent
     self.change_turn(opponent)
     pass
Example #8
0
def simulate_random_turn(game: ".game.Game"):
    #申し訳ないがちょっとだけ賢い可能性がある
    player = game.current_player
    while True:
        #getCandidate使った方が早くないか?
        # iterate over our hand and play whatever is playable
        simCandidates = getCandidates(game, _includeTurnEnd=True)
        index = int(random.random() * len(simCandidates))
        if simCandidates[index].type == ExceptionPlay.TURNEND:
            game.end_turn()
            return ExceptionPlay.VALID
        exc = executeAction(game, simCandidates[index], debugLog=False)
        postAction(player)
        if exc == ExceptionPlay.GAMEOVER:
            return ExceptionPlay.GAMEOVER
        else:
            continue
            pass
Example #9
0
 def StandardRandom(self,
                    thisgame: ".game.Game",
                    option=[],
                    gameLog=[],
                    debugLog=False):
     player = thisgame.current_player
     loopCount = 0
     while loopCount < 20:
         loopCount += 1
         myCandidate = getCandidates(thisgame)  #「何もしない」選択肢は入れていない
         if len(myCandidate) > 0:
             myChoice = random.choice(myCandidate)
             exc = executeAction(thisgame, myChoice, debugLog=debugLog)
             postAction(player)
             if exc == ExceptionPlay.GAMEOVER:
                 return ExceptionPlay.GAMEOVER
             else:
                 continue
         return ExceptionPlay.VALID
Example #10
0
 def preset_play(self):
     super().preset_play()
     controller = self.player
     opponent = controller.opponent
     game = controller.game
     ########## controller
     self.play_card(self.mark1, controller)
     self.change_turn(controller)
     ########## opponent
     self.change_turn(opponent)
     ########## controller
     self.attack_card(self.mark1, opponent.hero, controller)
     self.change_turn(controller)
     ########## opponent
     self.count1 = opponent.hero.health
     self.play_card(self.mark2, opponent)
     postAction(opponent)
     self.activate_heropower(opponent)
     self.change_turn(opponent)
     pass
Example #11
0
 def preset_play(self):
     super().preset_play()
     controller = self.player
     opponent = controller.opponent
     game = controller.game
     ########## controller
     self.play_card(self.mark4, controller)
     self.change_turn(controller)
     ########## opponent
     self.play_card(self.mark2, opponent)
     self.play_card(self.mark3, opponent)
     self.change_turn(opponent)
     ########## controller
     self.play_card(self.mark5, controller)
     self.attack_card(self.mark4, self.mark2, controller)
     self.change_turn(controller)
     ########## opponent
     self.change_turn(opponent)
     ########## controller
     self.attack_card(self.mark5, self.mark3, controller)
     self.attack_card(self.mark4, self.mark3, controller)
     self.change_turn(controller)
     ########## opponent
     print(">>>>>> %s (%s): zone=%s" %
           (self.mark1, self.mark1.controller, self.mark1.zone))
     print(">>>>>> %s (%s): zone=%s(candidate of revive)" %
           (self.mark2, self.mark2.controller, self.mark2.zone))
     print(">>>>>> %s (%s): zone=%s(candidate of revive)" %
           (self.mark3, self.mark3.controller, self.mark3.zone))
     print(">>>>>> %s (%s): zone=%s" %
           (self.mark4, self.mark4.controller, self.mark4.zone))
     print(">>>>>> %s (%s): zone=%s" %
           (self.mark5, self.mark5.controller, self.mark5.zone))
     self.play_card(self.mark1, opponent)
     postAction(opponent)
     self.change_turn(opponent)
     pass
Example #12
0
 def expandChild(self, action):
     self.expandingGame = copy.deepcopy(self.gameTree)
     simcand = getCandidates(self.expandingGame, _includeTurnEnd=True)
     myPolicy = ""
     for item in simcand:
         if item == action:
             myPolicy = item
             pass
         pass
     exc = executeAction(self.expandingGame, myPolicy, debugLog=False)
     postAction(self.expandingGame.current_player)
     if exc == ExceptionPlay.GAMEOVER:
         print("the game has been ended.")
         child = Node(self.expandingGame, action, self, [])
         self.childNodes.append(child)
         return child
         pass
     elif action.type == ExceptionPlay.TURNEND:
         self.expandingGame.end_turn()
         pass
     child = Node(self.expandingGame, action, self,
                  getCandidates(self.expandingGame, _includeTurnEnd=True))
     self.childNodes.append(child)
     return child
Example #13
0
 def StandardStep1(self, game, option=None, gameLog=[], debugLog=True):
     debugChoice = False  ###  Display parameters and scores
     if option == None:
         print("StandardStep1 needs an option")
         return ExceptionPlay.INVALID
     myWeight = option
     loopCount = 0
     while loopCount < 20:
         loopCount += 1
         if debugChoice:
             print(">>>>>>>>>>>>>>>>>>>")
         myCandidate = getCandidates(game)  #「何もしない」選択肢は入れていない
         myChoices = [
             Candidate(None, type=ExceptionPlay.TURNEND, turn=game.turn)
         ]  #何もしない選択
         maxScore = self.getStageScore(game, myWeight,
                                       debugChoice)  #何もしないときのスコア
         if debugChoice:
             print("   %s %d" % (myChoices[0], maxScore))
         maxChoice = None
         for myChoice in myCandidate:
             tmpGame = fireplace_deepcopy(game)
             #tmpGame = copy.deepcopy(game)
             log.info("Estimate the score for [%s]" % (myChoice))
             result = executeAction(tmpGame, myChoice, debugLog=False)
             postAction(tmpGame.current_player)
             if result == ExceptionPlay.INVALID:
                 stop = True
             if result == ExceptionPlay.GAMEOVER:
                 score = 100000
             else:
                 if self.__standard_agent__.StandardRandom(
                         tmpGame, debugLog=False
                 ) == ExceptionPlay.GAMEOVER:  #ここをもっと賢くしてもよい
                     score = 100000
                 else:
                     score = self.getStageScore(tmpGame, myWeight,
                                                debugChoice)
             if debugChoice:
                 print("   %s %d" % (myChoice, score))
             if score > maxScore:
                 maxScore = score
                 myChoices = [myChoice]
                 if score == 100000:
                     break
             elif score == maxScore:
                 myChoices.append(myChoice)
         if debugChoice:
             print("<<<<<<<<<<<<<<<<<<<")
         if len(myChoices) > 0:
             myChoice = random.choice(myChoices)
             if myChoice.type == ExceptionPlay.TURNEND:
                 if debugLog:
                     print(">%s -> turnend." % (self.name))
                 return ExceptionPlay.VALID
             ret = executeAction(game, myChoice, debugLog=debugLog)
             if ret == ExceptionPlay.GAMEOVER:
                 return ExceptionPlay.GAMEOVER
             if ret == ExceptionPlay.INVALID:
                 return ExceptionPlay.INVALID
             player = game.current_player
             postAction(player)
             continue
         else:
             return ExceptionPlay.VALID
Example #14
0
 def HumanInput(self, game, option=None, gameLog=[], debugLog=True):
     player = game.current_player
     ###############
     #from fireplace.deepcopy import deepcopy_game
     #new_game = debug_deepcopy(game, player)
     #######
     while True:
         ###################
         #debug_board(new_game,game)#
         ###################
         myCandidate = []
         print("========My HAND======")
         for card in player.hand:
             print("%s" % card, end='   : ')
             if card.data.type == CardType.MINION:
                 print("%2d(%2d/%2d)%s"%(card.cost, card.atk, card.health, \
                  adjust_text_by_spellpower(card.data.description, player, card)))
             elif card.data.type == CardType.SPELL:
                 print("%2d : %s"%(card.cost, \
                  adjust_text_by_spellpower(card.data.description, player, card)))
             elif card.data.type == CardType.WEAPON:
                 print("%2d(%2d/%2d) : %s"%(card.cost, card.atk, card.durability, \
                  adjust_text_by_spellpower(card.data.description, player, card)))
                 ##
             if card.is_playable():
                 if card.must_choose_one:
                     for card2 in card.choose_cards:
                         if card2.is_playable():
                             if card2.requires_target():
                                 for target in card2.targets:
                                     myCandidate.append(
                                         Candidate(card,
                                                   card2=card2,
                                                   type=ActionType.PLAY,
                                                   target=target,
                                                   turn=game.turn))
                             else:
                                 myCandidate.append(
                                     Candidate(card,
                                               card2=card2,
                                               type=ActionType.PLAY,
                                               target=None,
                                               turn=game.turn))
                 else:  # card2=None
                     if card.requires_target():
                         for target in card.targets:
                             myCandidate.append(
                                 Candidate(card,
                                           type=ActionType.PLAY,
                                           target=target,
                                           turn=game.turn))
                     else:
                         myCandidate.append(
                             Candidate(card,
                                       type=ActionType.PLAY,
                                       target=None,
                                       turn=game.turn))
             _yes, _option = card.can_trade()
             if _yes:
                 myCandidate.append(
                     Candidate(card,
                               type=ActionType.TRADE,
                               target=None,
                               turn=game.turn))
         print("========OPPONENT'S PLAYGROUND======")
         for character in player.opponent.characters:
             print("%s" % character, end='   : ')
             if character == player.opponent.hero:
                 if player.opponent.weapon:
                     print(
                         "(%2d/%2d/%2d+%d)(%s)" %
                         (character.atk, player.opponent.weapon.durability,
                          character.health, character.armor,
                          player.opponent.weapon.data.name),
                         end=" ")
                 else:
                     print(
                         "(%2d/%2d+%d)" %
                         (character.atk, character.health, character.armor),
                         end=" ")
             else:
                 print("(%2d/%2d)" % (character.atk, character.health),
                       end=" ")
                 if character._Asphyxia_ == 'asphyxia':
                     print("(Now Asphyxia)", end=' ')
                 if character.silenced:
                     print("(silenced)", end=" ")
                 if character.windfury:
                     print("(windfury)", end=" ")
                 if character.poisonous:
                     print("(poisonous)", end=" ")
                 if character.frozen:
                     print("(frozen)", end=" ")
                 if character.rush:
                     print("(rush)", end=" ")
                 if character.reborn:
                     print("(reborn)", end=" ")
                 if character.taunt:
                     print("(taunt)", end=" ")
                 if character.immune:
                     print("(immune)", end=" ")
                 if character.divine_shield:
                     print("(divine_shield)", end=" ")
                 if character.dormant > 0:
                     print("(dormant:%d)" % (character.dormant), end=" ")
                 elif character.dormant < 0:
                     if character._sidequest_counter_ > 0:
                         print("(dormant:%d)" %
                               (character._sidequest_counter_),
                               end=" ")
                     else:
                         print("(dormant)", end=" ")
                 #if character._sidequest_counter_>0:
                 #	if character.dormant==0:
                 #		print("(sidequest:%d)"%(character._sidequest_counter_), end=" ")
             print("%s" % (adjust_text_by_spellpower(
                 character.data.description, player.opponent, character)))
         print("========MY PLAYGROUND======")
         for character in player.characters:
             print("%s" % character, end='   : ')
             if character == player.hero:
                 if player.weapon:
                     print("(%2d/%2d/%2d+%d)(%s)" %
                           (character.atk, player.weapon.durability,
                            character.health, character.armor,
                            player.weapon.data.name),
                           end=" ")
                 else:
                     print(
                         "(%2d/%2d+%d)" %
                         (character.atk, character.health, character.armor),
                         end=" ")
             else:
                 print("(%2d/%2d)" % (character.atk, character.health),
                       end=" ")
                 if character._Asphyxia_ == 'asphyxia':
                     print("(Now Asphyxia %d)" %
                           (character._sidequest_counter_),
                           end=' ')
                 if character.charge:
                     print("(charge)", end=" ")
                 if character.divine_shield:
                     print("(divine_shield)", end=" ")
                 if character.dormant > 0:
                     print("(dormant:%d)" % (character.dormant), end=" ")
                 elif character.dormant < 0:
                     if character._sidequest_counter_ > 0:
                         print("(dormant:%d)" %
                               (character._sidequest_counter_),
                               end=" ")
                     else:
                         print("(dormant)", end=" ")
                 if character.frozen:
                     print("(frozen)", end=" ")
                 if character.immune:
                     print("(immune)", end=" ")
                 if character.poisonous:
                     print("(poisonous)", end=" ")
                 if character.reborn:
                     print("(reborn)", end=" ")
                 if character.rush:
                     print("(rush)", end=" ")
                 if character.silenced:
                     print("(silenced)", end=" ")
                 if character.spellpower > 0:
                     print("(spellpower:%d)" % (character.spellpower),
                           end=" ")
                 if character.stealthed:
                     print("(stealthed)", end=" ")
                 if character.taunt:
                     print("(taunt)", end=" ")
                 if character.windfury:
                     print("(windfury)", end=" ")
             print("%s" % (adjust_text_by_spellpower(
                 character.data.description, player, character)))
             if character.can_attack():
                 for target in character.targets:
                     if character.can_attack(target):
                         myH = character.health
                         hisA = target.atk
                         #if myH > hisA:
                         myCandidate.append(
                             Candidate(character,
                                       type=ActionType.ATTACK,
                                       target=target,
                                       turn=game.turn))
         if player.hero.power.is_usable():
             print("%s" % player.hero.power, end='   : ')
             print("<%2d>" % player.hero.power.cost, end=' ')
             print("%s" % adjust_text(player.hero.power.data.description))
             if player.hero.power.requires_target():
                 for target in player.hero.power.targets:
                     if player.hero.power.is_usable():
                         myCandidate.append(
                             Candidate(player.hero.power,
                                       type=BlockType.POWER,
                                       target=target,
                                       turn=game.turn))
             else:
                 myCandidate.append(
                     Candidate(player.hero.power,
                               type=BlockType.POWER,
                               target=None,
                               turn=game.turn))
         print("========MY SECRETS======")
         for card in player.secrets:
             print("%s" % card, end='   : ')
             if hasattr(card, 'sidequest') or hasattr(card, 'questline'):
                 print("(sidequest %d)" % card._sidequest_counter_, end="")
             print("%s" % (adjust_text(card.data.description)))
         print(
             "========Your turn : %d/%d mana==(spell damage %d (fire %d))==="
             % (player.mana, player.max_mana, player.spellpower,
                player.spellpower_fire))
         print("[0] ターンを終了する")
         myCount = 1
         for myChoice in myCandidate:
             print('[%d]' % myCount, end=' ')
             myCard = myChoice.card
             print("%s" % myCard, end='  ')
             if myChoice.card2 != None:
                 print("(%s)" % myChoice.card2, end=' ')
                 if myCard.data.type == CardType.SPELL:
                     print('<%2d> %s' %
                           (myCard.cost,
                            adjust_text_by_spellpower(
                                myChoice.card2.data.description, player,
                                myCard)),
                           end=' ')
             else:
                 if myCard.data.type == CardType.MINION:
                     print('<%2d>(%2d/%2d)' %
                           (myCard.cost, myCard.atk, myCard.health),
                           end=' ')
                 elif myCard.data.type == CardType.SPELL:
                     print('<%2d> %s' %
                           (myCard.cost,
                            adjust_text_by_spellpower(
                                myCard.data.description, player, myCard)),
                           end=' ')
                 elif myCard.data.type == CardType.WEAPON:
                     print('<%2d> %s' %
                           (myCard.cost,
                            adjust_text_by_spellpower(
                                myCard.data.description, player, myCard)),
                           end=' ')
             if myChoice.type == ActionType.PLAY:
                 print(' play', end=' ')
             if myChoice.type == ActionType.TRADE:
                 print(' trade', end=' ')
             if myChoice.type == ActionType.ATTACK:
                 print(' attack', end=' ')
             if myChoice.type == ActionType.POWER:
                 print('<%2d> power' % (myCard.cost), end=' ')
             targetCard = myChoice.target
             if targetCard != None:
                 print("%s(%s)" % (targetCard, targetCard.controller.name),
                       end=' ')
                 if targetCard.data.type == CardType.MINION:
                     print('(%2d/%2d)' %
                           (targetCard.atk, targetCard.health),
                           end=' ')
             myCount += 1
             print('')
         while True:
             str = input()
             try:
                 inputNum = int(str)
                 break
             except ValueError:
                 inputNum = 0
         if len(myCandidate) == 0 or inputNum == 0:
             ########################################
             #debug_board(new_game,game)###
             ########################################
             break
         if inputNum > 0 and inputNum <= len(myCandidate):
             myChoice = myCandidate[inputNum - 1]
             ###################
             #executeAction(new_game, myChoice)#
             #postAction(new_game.current_player)#
             ##################
             executeAction(game, myChoice)
             postAction(player)