コード例 #1
0
    def test_random_character_saving(self):
        deck1 = hearthbreaker.engine.Deck(
            [RagnarosTheFirelord() for i in range(0, 30)], Jaina())
        deck2 = hearthbreaker.engine.Deck(
            [StonetuskBoar() for i in range(0, 30)], Malfurion())
        agent1 = PlayAndAttackAgent()
        agent2 = OneCardPlayingAgent()
        random.seed(4879)
        game = Game([deck1, deck2], [agent1, agent2])
        replay = record(game)
        game.pre_game()
        for turn in range(0, 17):
            game.play_single_turn()

        output = StringIO()
        replay.write_json(output)
        random.seed(4879)
        new_game = playback(Replay(StringIO(output.getvalue())))
        new_game.pre_game()
        for turn in range(0, 17):
            new_game.play_single_turn()

        self.assertEqual(2, len(new_game.current_player.minions))
        self.assertEqual(30, new_game.other_player.hero.health)
        self.assertEqual(5, len(new_game.other_player.minions))
コード例 #2
0
    def test_create_game(self):
        card_set1 = []
        card_set2 = []
        test_env = self

        for cardIndex in range(0, 30):
            card_set1.append(card_lookup("Stonetusk Boar"))
            card_set2.append(card_lookup("Novice Engineer"))

        deck1 = Deck(card_set1, Malfurion())
        deck2 = Deck(card_set2, Jaina())
        checked_cards = []

        class MockAgent1:
            def do_card_check(self, cards):
                test_env.assertEqual(len(cards), 3)
                checked_cards.append(list(cards))
                return [False, True, True]

            def set_game(self, game):
                pass

        class MockAgent2:
            def do_card_check(self, cards):
                test_env.assertEqual(len(cards), 4)
                checked_cards.append(list(cards))
                return [False, True, True, False]

            def set_game(self, game):
                pass

        agent1 = mock.Mock(spec=MockAgent1(), wraps=MockAgent1())
        agent2 = mock.Mock(spec=MockAgent2(), wraps=MockAgent2())
        game = Game([deck1, deck2], [agent1, agent2])
        game.pre_game()

        self.assertEqual(agent1.method_calls[0][0], "do_card_check",
                         "Agent not asked to select cards")
        self.assertEqual(agent2.method_calls[0][0], "do_card_check",
                         "Agent not asked to select cards")

        self.assertTrue(game.players[0].deck == deck1,
                        "Deck not assigned to player")
        self.assertTrue(game.players[1].deck == deck2,
                        "Deck not assigned to player")

        self.assertTrue(game.players[0].agent == agent1,
                        "Agent not stored in the hearthbreaker")
        self.assertTrue(game.players[1].agent == agent2,
                        "Agent not stored in the hearthbreaker")

        self.assertListEqual(checked_cards[0][1:], game.players[0].hand[1:],
                             "Cards not retained after request")
        self.assertListEqual(checked_cards[1][1:2], game.players[1].hand[1:2],
                             "Cards not retained after request")
コード例 #3
0
    def test_first_turn(self):
        card_set1 = []
        card_set2 = []

        for cardIndex in range(0, 30):
            card_set1.append(card_lookup("Stonetusk Boar"))
            card_set2.append(card_lookup("Novice Engineer"))

        deck1 = Deck(card_set1, Malfurion())
        deck2 = Deck(card_set2, Jaina())

        agent1 = mock.Mock(spec=DoNothingAgent(), wraps=DoNothingAgent())
        agent2 = mock.Mock(spec=DoNothingAgent(), wraps=DoNothingAgent())
        game = Game([deck1, deck2], [agent1, agent2])

        game.start()
コード例 #4
0
ファイル: run_games.py プロジェクト: Inariss/hearthbreaker
def do_stuff():
    _count = 0

    def play_game():
        nonlocal _count
        _count += 1
        new_game = game.copy()
        try:
            winner = new_game.start()
            print("Winner: ", winner)
            print("turns passed : ", new_game._turns_passed)

            print("# " * 27, " GAME OVER ", " #" * 27)
            print(new_game.players[0],
                  "has",
                  new_game.players[0].hero.health,
                  "life points,\t",
                  end='')
            print(new_game.players[1], "has", new_game.players[1].hero.health,
                  "life points")
            print(winner, 'won the game (', winner.agent, ')')
            print("# " * 61, "\n")
            print("AVG tree depth per nr of turns", global_depth)
            print("AVG percent of explored children", global_nodesvisited)

        except Exception as e:
            # print(json.dumps(new_game.__to_json__(), default=lambda o: o.__to_json__(), indent=1))
            # print(new_game._all_cards_played)
            raise e

        del new_game

        if _count % 1000 == 0:
            print("---- game #{} ----".format(_count))

    cards = load_deck("mage3.hsdeck")
    deck1 = Deck(cards, Jaina())
    deck2 = Deck(cards, Malfurion())
    game = Game([deck1, deck2], [MCTSAgent(30), RandomAgent()])
    # game = Game([deck1, deck2], [AggressiveAgent(), RandomAgent()])

    # game = Game([deck1, deck2], [ControllingAgent(), RandomAgent()])
    # game = Game([deck1, deck2], [TalkativeAgent(), RandomAgent()])
    # game = Game([deck1, deck2], [RandomAgent(), RandomAgent()])

    print(timeit.timeit(play_game, 'gc.enable()', number=1))
コード例 #5
0
    def test_recording_game(self):
        self.maxDiff = None
        random.seed(9876)
        deck1 = hearthbreaker.engine.Deck(
            [StonetuskBoar() for i in range(0, 30)], Jaina())
        deck2 = hearthbreaker.engine.Deck([Naturalize() for i in range(0, 30)],
                                          Malfurion())
        agent1 = PredictableAgent()
        agent2 = PredictableAgent()

        game = Game([deck1, deck2], [agent1, agent2])
        replay = record(game)
        game.start()
        output = StringIO()
        replay.write_json(output)
        f = open("tests/replays/stonetusk_innervate.hsreplay", 'r')
        dif = self.__compare_json(output.getvalue(), f.read())
        self.assertTrue(dif)
        f.close()
コード例 #6
0
 def test_deck_shortening(self):
     deck1 = Deck([
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         RagnarosTheFirelord(),
         GoldshireFootman(),
         GoldshireFootman()
     ], Malfurion())
     deck2 = StackedDeck([StonetuskBoar()], CHARACTER_CLASS.HUNTER)
     game = Game([deck1, deck2], [RandomAgent(), RandomAgent()])
     replay = record(game)
     game.start()
     replay.write(StringIO())
コード例 #7
0
    def test_json_saving(self):
        self.maxDiff = 6000
        deck1 = hearthbreaker.engine.Deck(
            [RagnarosTheFirelord() for i in range(0, 30)], Jaina())
        deck2 = hearthbreaker.engine.Deck(
            [StonetuskBoar() for i in range(0, 30)], Malfurion())
        agent1 = PlayAndAttackAgent()
        agent2 = OneCardPlayingAgent()
        random.seed(4879)
        game = Game([deck1, deck2], [agent1, agent2])
        replay = record(game)
        game.pre_game()
        for turn in range(0, 17):
            game.play_single_turn()

        output = StringIO()
        replay.write_json(output)
        inp = StringIO(output.getvalue())
        new_replay = Replay()
        new_replay.read_json(inp)
        old_output = output.getvalue()
        other_output = StringIO()
        new_replay.write_json(other_output)
        self.assertEqual(other_output.getvalue(), old_output)
コード例 #8
0
    def test_RandomAgent(self):
        deck1 = Deck([
            GoldshireFootman(),
            GoldshireFootman(),
            MurlocRaider(),
            MurlocRaider(),
            BloodfenRaptor(),
            BloodfenRaptor(),
            FrostwolfGrunt(),
            FrostwolfGrunt(),
            RiverCrocolisk(),
            RiverCrocolisk(),
            IronfurGrizzly(),
            IronfurGrizzly(),
            MagmaRager(),
            MagmaRager(),
            SilverbackPatriarch(),
            SilverbackPatriarch(),
            ChillwindYeti(),
            ChillwindYeti(),
            KeeperOfTheGrove(),
            KeeperOfTheGrove(),
            SenjinShieldmasta(),
            SenjinShieldmasta(),
            BootyBayBodyguard(),
            BootyBayBodyguard(),
            FenCreeper(),
            FenCreeper(),
            BoulderfistOgre(),
            BoulderfistOgre(),
            WarGolem(),
            WarGolem(),
        ], Malfurion())

        deck2 = Deck([
            Shieldbearer(),
            Shieldbearer(),
            FlameImp(),
            FlameImp(),
            YoungPriestess(),
            YoungPriestess(),
            DarkIronDwarf(),
            DarkIronDwarf(),
            DireWolfAlpha(),
            DireWolfAlpha(),
            Voidwalker(),
            Voidwalker(),
            HarvestGolem(),
            HarvestGolem(),
            KnifeJuggler(),
            KnifeJuggler(),
            ShatteredSunCleric(),
            ShatteredSunCleric(),
            ArgentSquire(),
            ArgentSquire(),
            Doomguard(),
            Doomguard(),
            Soulfire(),
            Soulfire(),
            DefenderOfArgus(),
            DefenderOfArgus(),
            AbusiveSergeant(),
            AbusiveSergeant(),
            NerubianEgg(),
            NerubianEgg(),
        ], Guldan())

        game = Game([deck1, deck2], [RandomAgent(), RandomAgent()])
        game.pre_game()
        game.current_player = game.players[1]

        game.play_single_turn()

        self.assertEqual(0, len(game.current_player.minions))

        game.play_single_turn()
        self.assertEqual(2, len(game.current_player.minions))
        self.assertEqual(3, game.current_player.minions[1].health)
        self.assertEqual("Young Priestess",
                         game.current_player.minions[0].card.name)

        game.play_single_turn()
        self.assertEqual(1, len(game.current_player.minions))
        self.assertEqual("Frostwolf Grunt",
                         game.current_player.minions[0].card.name)

        game.play_single_turn()
        self.assertEqual(0, len(game.other_player.minions))
        self.assertEqual(28, game.other_player.hero.health)
        self.assertEqual(3, len(game.current_player.minions))
        self.assertEqual("Dire Wolf Alpha",
                         game.current_player.minions[2].card.name)

        for turn in range(0, 13):
            game.play_single_turn()
            self.assertFalse(game.game_ended)

        game.play_single_turn()

        self.assertEqual(0, game.current_player.hero.health)
        self.assertEqual(21, game.other_player.hero.health)

        self.assertTrue(game.game_ended)