Ejemplo n.º 1
0
    def test_draw_on_timeout(self):
        test_start = (""" ######
                #0 . #
                # . 1#
                ###### """)
        # the game lasts one round, and then draws
        NUM_ROUNDS = 1
        # players do nothing
        teams = [SimpleTeam(StoppingPlayer()), SimpleTeam(StoppingPlayer())]
        gm = GameMaster(test_start, teams, 2, game_time=NUM_ROUNDS)

        # this test viewer caches all events lists seen through observe
        class TestViewer(AbstractViewer):
            def __init__(self):
                self.cache = list()

            def observe(self, universe, game_state):
                self.cache.append(game_state)

        # run the game
        tv = TestViewer()
        gm.register_viewer(tv)
        gm.set_initial()
        gm.play()

        # check
        self.assertTrue(tv.cache[-1]["game_draw"])
        self.assertEqual(gm.game_state["round_index"], NUM_ROUNDS)
Ejemplo n.º 2
0
def run_game():
    layout = LAYOUT
    teams = [
        SimpleTeam(StoppingPlayer(), StoppingPlayer()),
        SimpleTeam(StoppingPlayer(), StoppingPlayer())
    ]
    gm = GameMaster(layout, teams, 4, 10)
    #gm.register_viewer(AsciiViewer())
    gm.play()
Ejemplo n.º 3
0
    def test_lose_on_eating_all(self):
        test_start = (""" ######
                #0 . #
                # . 1#
                ###### """)
        teams = [SimpleTeam(StoppingPlayer()), SimpleTeam(TestPlayer('<<<'))]
        # bot 1 eats all the food and the game stops
        gm = GameMaster(test_start, teams, 2, 100)
        gm.universe.teams[0].score = 2

        # this test viewer caches all events lists seen through observe
        class TestViewer(AbstractViewer):
            def __init__(self):
                self.cache = list()

            def observe(self, universe, game_state):
                self.cache.append(game_state)

        # run the game
        tv = TestViewer()
        gm.register_viewer(tv)
        gm.set_initial()
        gm.play()

        # check
        self.assertEqual(tv.cache[-1]["round_index"], 1)
        self.assertEqual(gm.universe.teams[0].score, 2)
        self.assertEqual(gm.universe.teams[1].score, 1)
        self.assertTrue(tv.cache[-1]["team_wins"] is not None)
        self.assertEqual(tv.cache[-1]["team_wins"], 0)
        self.assertEqual(gm.game_state["round_index"], 1)
Ejemplo n.º 4
0
    def test_win_on_timeout_team_1(self):
        test_start = (""" ######
                #0 ..#
                #.. 1#
                ###### """)
        # the game lasts two rounds, enough time for bot 1 to eat food
        NUM_ROUNDS = 2

        teams = [
            SimpleTeam(StoppingPlayer()),
            SimpleTeam(TestPlayer(
                '<<'))  # bot 1 moves west twice to eat the single food
        ]
        gm = GameMaster(test_start, teams, 2, game_time=NUM_ROUNDS)

        # this test viewer caches all events lists seen through observe
        class TestViewer(AbstractViewer):
            def __init__(self):
                self.cache = list()

            def observe(self, universe, game_state):
                self.cache.append(game_state)

        # run the game
        tv = TestViewer()
        gm.register_viewer(tv)
        gm.set_initial()
        gm.play()

        # check
        self.assertTrue(tv.cache[-1]["team_wins"] is not None)
        self.assertEqual(tv.cache[-1]["team_wins"], 1)
        self.assertEqual(gm.game_state["round_index"], NUM_ROUNDS)
Ejemplo n.º 5
0
    def test_lose_5_timeouts(self):
        # 0 must move back and forth because of random steps
        test_start = (
            """ ######
                #0 #.#
                ###  #
                ##. 1#
                ###### """
        )
        # players do nothing
        class TimeOutPlayer(AbstractPlayer):
            def get_move(self):
                raise PlayerTimeout

        teams = [
            SimpleTeam(TimeOutPlayer()),
            SimpleTeam(StoppingPlayer())
        ]
        # the game lasts one round, and then draws
        gm = GameMaster(test_start, teams, 2, 100, max_timeouts=5)

        # this test viewer caches all events lists seen through observe
        class TestViewer(AbstractViewer):
            def __init__(self):
                self.cache = list()
            def observe(self, universe, game_state):
                self.cache.append(game_state)

        # run the game
        tv = TestViewer()
        gm.register_viewer(tv)
        gm.set_initial()

        assert gm.universe.bots[0].current_pos == (1,1)

        gm.play()

        # check
        assert gm.game_state["max_timeouts"] == 5
        assert tv.cache[-1]["round_index"] == gm.game_state["max_timeouts"] - 1
        assert gm.universe.teams[0].score == 0
        assert gm.universe.teams[1].score == 0
        # the bot moves four times, so after the fourth time,
        # it is back on its original position
        assert gm.universe.bots[0].current_pos == (1,1)
        assert tv.cache[-1]["team_wins"] is not None
        assert tv.cache[-1]["team_wins"] == 1
Ejemplo n.º 6
0
#!/usr/bin/python
from pelita.game_master import GameMaster
from pelita.player import StoppingPlayer, SimpleTeam
from pelita.viewer import AsciiViewer
from players import RandomPlayer, NQRandomPlayer

if __name__ == '__main__':
    layout = (""" ##################
            #0#.  .  # .     #
            #2#####    #####1#
            #     . #  .  .#3#
            ################## """)
    gm = GameMaster(layout, 4, 200)
    gm.register_team(SimpleTeam(StoppingPlayer(), NQRandomPlayer()))
    gm.register_team(SimpleTeam(NQRandomPlayer(), NQRandomPlayer()))
    gm.register_viewer(AsciiViewer())
    gm.play()
Ejemplo n.º 7
0
#!/usr/bin/python
from pelita.game_master import GameMaster
from pelita.player import StoppingPlayer, SimpleTeam
from pelita.viewer import AsciiViewer
from players import BFSPlayer

if __name__ == '__main__':
    layout = (""" ##################
            #0#.  .  # .     #
            # #####    ##### #
            #     . #  .  .#1#
            ################## """)
    gm = GameMaster(layout, 2, 200)
    gm.register_team(SimpleTeam(BFSPlayer()))
    gm.register_team(SimpleTeam(StoppingPlayer()))
    gm.register_viewer(AsciiViewer())
    gm.play()
Ejemplo n.º 8
0
#!/usr/bin/python
from pelita.game_master import GameMaster
from pelita.player import StoppingPlayer, SimpleTeam
from pelita.viewer import AsciiViewer
from players import RandomPlayer, NQRandomPlayer

if __name__ == '__main__':
    layout = (""" ##################
            #0#.  .  # .     #
            #2#####    #####1#
            #     . #  .  .#3#
            ################## """)
    teams = [
        SimpleTeam(StoppingPlayer(), NQRandomPlayer()),
        SimpleTeam(StoppingPlayer(), NQRandomPlayer())
    ]
    gm = GameMaster(layout, teams, 4, 200)
    gm.register_viewer(AsciiViewer())
    gm.play()