def test_my_player_is_not_moving(self): my_team = SimpleTeam("test", MyPlayer(), MyPlayer()) test_layout = """ ############ # 0 # # 1 # # # # # # 2 . . 3 # ############ """ teams = [ # register my_team for bots 0, 2 my_team, # register a pre-defined team as an enemy SimpleTeam(RandomPlayer(), RandomPlayer()) ] gm = GameMaster(test_layout, teams, number_bots=4, game_time=5, seed=20) # play `game_time` rounds gm.play() # check the position of my bots self.assertEqual(gm.universe.bots[0].current_pos, (2, 1)) self.assertEqual(gm.universe.bots[2].current_pos, (2, 3))
def test_round_based_players(self): test_layout = (""" ############ #0 . . 1# #2 3# ############ """) movements_0 = [east, east] movements_1_0 = {0: west, 2: west} movements_1_1 = {2: west} teams = [ SimpleTeam(RoundBasedPlayer(movements_0), RoundBasedPlayer(movements_0)), SimpleTeam(RoundBasedPlayer(movements_1_0), RoundBasedPlayer(movements_1_1)) ] gm = GameMaster(test_layout, teams, 4, 3) self.assertEqual(gm.universe.bots[0].current_pos, (1, 1)) self.assertEqual(gm.universe.bots[1].current_pos, (10, 1)) self.assertEqual(gm.universe.bots[2].current_pos, (1, 2)) self.assertEqual(gm.universe.bots[3].current_pos, (10, 2)) gm.play() self.assertEqual(gm.universe.bots[0].current_pos, (3, 1)) self.assertEqual(gm.universe.bots[1].current_pos, (8, 1)) self.assertEqual(gm.universe.bots[2].current_pos, (3, 2)) self.assertEqual(gm.universe.bots[3].current_pos, (9, 2))
def test_my_player_is_not_moving(self): my_team = SimpleTeam("test", DrunkPlayer(), DrunkPlayer()) test_layout = """ ############ # 0 # # 1 # # # # # # 2 . . 3 # ############ """ gm = GameMaster(test_layout, number_bots=4, game_time=5, seed=20) # register my_team for bots 0, 2 gm.register_team(my_team) # register a pre-defined team as an enemy # First one moves left-down-down-left-left, second one left gm.register_team(SimpleTeam(TestPlayer("<vv<<"), TestPlayer("<<<<<"))) # play `game_time` rounds gm.play() # check the position of my bots # Tests fail, because our player is random # self.assertEqual(gm.universe.bots[0].current_pos, (2, 1)) # self.assertEqual(gm.universe.bots[2].current_pos, (2, 3)) # For reference, testing the test player self.assertEqual(gm.universe.bots[1].current_pos, (6, 3)) self.assertEqual(gm.universe.bots[3].current_pos, (4, 3))
def test_win_on_timeout_team_0(self): test_start = """ ###### #0 ..# #.. 1# ###### """ # the game lasts two rounds, enough time for bot 1 to eat food NUM_ROUNDS = 2 gm = GameMaster(test_start, 2, game_time=NUM_ROUNDS) # bot 1 moves east twice to eat the single food gm.register_team(SimpleTeam(TestPlayer(">>"))) gm.register_team(SimpleTeam(StoppingPlayer())) # 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"], 0) self.assertEqual(gm.game_state["round_index"], NUM_ROUNDS)
def test_my_player_is_not_moving(): my_team = SimpleTeam("test", KangarooPlayer(), KangarooPlayer()) test_layout = """ ############ # 0 # # 1 # # # # # # 2 . . 3 # ############ """ teams = [ # register my_team for bots 0, 2 my_team, # register a pre-defined team as an enemy # First one moves left-down-down-left-left, second one left SimpleTeam(SteppingPlayer("<vv<<"), SteppingPlayer("<<<<<")) ] gm = GameMaster(test_layout, teams, number_bots=4, game_time=5, seed=20) # play `game_time` rounds gm.play() # check the position of my bots # Tests fail, because our player is random # assert gm.universe.bots[0].current_pos == (2, 1) # assert gm.universe.bots[2].current_pos == (2, 3) # For reference, testing the test player assert gm.universe.bots[1].current_pos == (6, 3) assert gm.universe.bots[3].current_pos == (4, 3)
def test_time_spent(self): outer = self class TimeSpendingPlayer(AbstractPlayer): def get_move(self): time_spent_begin = self.time_spent() sleep_time = 0.1 time.sleep(sleep_time) time_spent_end = self.time_spent() outer.assertTrue(0 <= time_spent_begin < time_spent_end) time_diff = abs(time_spent_begin + sleep_time - time_spent_end) delta = 0.05 outer.assertTrue(time_diff < delta) return stop test_layout = ( """ ############ #0 #. .# 1# ############ """) gm = GameMaster(test_layout, 2, 1) gm.register_team(SimpleTeam(TimeSpendingPlayer())) gm.register_team(SimpleTeam(RandomPlayer())) gm.play()
def test_time_spent(self): outer = self class TimeSpendingPlayer(AbstractPlayer): def get_move(self): time_spent_begin = self.time_spent() sleep_time = 0.1 time.sleep(sleep_time) time_spent_end = self.time_spent() outer.assertTrue(0 <= time_spent_begin < time_spent_end) time_diff = abs(time_spent_begin + sleep_time - time_spent_end) delta = 0.05 outer.assertTrue(time_diff < delta) return stop test_layout = (""" ############ #0 #. .# 1# ############ """) team = [SimpleTeam(TimeSpendingPlayer()), SimpleTeam(RandomPlayer())] gm = GameMaster(test_layout, team, 2, 1) gm.play()
def test_lose_on_eating_all(self): test_start = ( """ ###### #0 . # # . 1# ###### """ ) # the game lasts one round, and then draws gm = GameMaster(test_start, 2, 100) # players do nothing gm.register_team(SimpleTeam(StoppingPlayer())) gm.register_team(SimpleTeam(TestPlayer([west, west, west]))) 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() self.round_ = list() def observe(self, round_, turn, universe, events): self.cache.append(events) self.round_.append(round_) # run the game tv = TestViewer() gm.register_viewer(tv) gm.set_initial() gm.play() # check self.assertEqual(tv.round_[-1], 1) self.assertEqual(gm.universe.teams[0].score, 2) self.assertEqual(gm.universe.teams[1].score, 1) self.assertTrue(TeamWins in tv.cache[-1]) self.assertEqual(tv.cache[-1].filter_type(TeamWins)[0], TeamWins(0))
def test_common_target(self): my_team = SimpleTeam("test", HunterPlayer(), HunterPlayer()) test_layout = ''' #################### # 1 0 .# #### ### # # . 2 # # 3 ## #################### ''' teams = [ # register my_team for bots 0, 2 my_team, # for now enemies don't move SimpleTeam(TestPlayer('<<<'), TestPlayer('<<<')) ] gm = GameMaster(test_layout, teams, number_bots=4, game_time=3, seed=20) # play `game_time` rounds gm.play() #pdb.set_trace() # test if both players seeked the food self.assertEqual(gm.universe.bots[0].current_pos, (7, 1)) self.assertEqual(gm.universe.bots[2].current_pos, (4, 2))
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 assert tv.cache[-1]["game_draw"] assert gm.game_state["round_index"] == NUM_ROUNDS
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(SteppingPlayer('<<')) # 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 assert tv.cache[-1]["team_wins"] is not None assert tv.cache[-1]["team_wins"] == 1 assert gm.game_state["round_index"] == NUM_ROUNDS
def test_round_based_players(self): test_layout = ( """ ############ #0 . . 1# #2 3# ############ """) movements_0 = [east, east] movements_1_0 = {0: west, 2: west} movements_1_1 = {2: west} teams = [ SimpleTeam(RoundBasedPlayer(movements_0), RoundBasedPlayer(movements_0)), SimpleTeam(RoundBasedPlayer(movements_1_0), RoundBasedPlayer(movements_1_1)) ] gm = GameMaster(test_layout, teams, 4, 3) assert gm.universe.bots[0].current_pos == (1, 1) assert gm.universe.bots[1].current_pos == (10, 1) assert gm.universe.bots[2].current_pos == (1, 2) assert gm.universe.bots[3].current_pos == (10, 2) gm.play() assert gm.universe.bots[0].current_pos == (3, 1) assert gm.universe.bots[1].current_pos == (8, 1) assert gm.universe.bots[2].current_pos == (3, 2) assert gm.universe.bots[3].current_pos == (9, 2)
def test_lose_on_eating_all(self): test_start = ( """ ###### #0 . # # . 1# ###### """ ) teams = [ SimpleTeam(StoppingPlayer()), SimpleTeam(SteppingPlayer('<<<')) ] # 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 assert tv.cache[-1]["round_index"] == 1 assert gm.universe.teams[0].score == 2 assert gm.universe.teams[1].score == 1 assert tv.cache[-1]["team_wins"] is not None assert tv.cache[-1]["team_wins"] == 0 assert gm.game_state["round_index"] == 1
def test_time_spent(self): class TimeSpendingPlayer(AbstractPlayer): def get_move(self): time_spent_begin = self.time_spent() sleep_time = 0.1 time.sleep(sleep_time) time_spent_end = self.time_spent() assert 0 <= time_spent_begin < time_spent_end time_diff = abs(time_spent_begin + sleep_time - time_spent_end) delta = 0.05 assert time_diff < delta return stop test_layout = ( """ ############ #0 #. .# 1# ############ """) team = [ SimpleTeam(TimeSpendingPlayer()), SimpleTeam(RandomPlayer()) ] gm = GameMaster(test_layout, team, 2, 1) gm.play()
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)
def test_stopping(self): test_layout = ( """ ############ #0#.23 .# 1# ############ """) round_counting = self.round_counting() team = [ Team(stopping), Team(round_counting) ] gm = GameMaster(test_layout, team, 4, 1) gm.play() assert gm.universe.bots[0].current_pos == (1, 1) assert gm.universe.bots[1].current_pos == (10, 1) assert round_counting._storage['rounds'] == 1 round_counting = self.round_counting() team = [ Team(stopping), Team(round_counting) ] gm = GameMaster(test_layout, team, 4, 3) gm.play() print(gm.universe.pretty) assert gm.universe.bots[0].current_pos == (1, 1) assert gm.universe.bots[1].current_pos == (10, 1) assert round_counting._storage['rounds'] == 3
def test_track(self): def trackingBot(turn, game): bot = game.team[turn] other = game.team[1 - turn] if bot.round == 0 and turn == 0: assert bot.track[0] == bot.position game.state = {} game.state[turn] = {} game.state[1 - turn] = {} game.state[turn]['track'] = [] game.state[1 - turn]['track'] = [] if bot.eaten or not game.state[turn]['track']: game.state[turn]['track'] = [bot.position] if other.eaten or not game.state[1 - turn]['track']: game.state[1 - turn]['track'] = [other.position] else: game.state[1 - turn]['track'].append(other.position) assert bot.track[0] == bot._initial_position assert bot.track == game.state[turn][ 'track'] # bot.round * 2 + 1 + turn assert bot.track[-1] == bot.position return randomBot(turn, game) layout = """ ############ ##02 .3 1# ############ #.# #.# ############ """ team = [Team(trackingBot), Team(trackingBot)] gm = GameMaster(layout, team, 4, 300) gm.play()
def test_lose_on_eating_all(self): test_start = ( """ ###### #0 . # # . 1# ###### """ ) # bot 1 eats all the food and the game stops gm = GameMaster(test_start, 2, 100) # players do nothing gm.register_team(SimpleTeam(StoppingPlayer())) gm.register_team(SimpleTeam(TestPlayer('<<<'))) 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)
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)
def test_draw_on_timeout(self): test_start = ( """ ###### #0 . # # . 1# ###### """) # the game lasts one round, and then draws gm = GameMaster(test_start, 2, 1) # players do nothing gm.register_team(SimpleTeam(StoppingPlayer())) gm.register_team(SimpleTeam(StoppingPlayer())) # this test viewer caches all events lists seen through observe class TestViewer(AbstractViewer): def __init__(self): self.cache = list() def observe(self, round_, turn, universe, events): self.cache.append(events) # run the game tv = TestViewer() gm.register_viewer(tv) gm.set_initial() gm.play() # check self.assertTrue(GameDraw in tv.cache[-1]) self.assertEqual(tv.cache[-1][0], GameDraw())
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 gm = GameMaster(test_start, 2, 2) gm.register_team(SimpleTeam(StoppingPlayer())) # bot 1 moves west twice to eat the single food gm.register_team(SimpleTeam(TestPlayer([west, west]))) # this test viewer caches all events lists seen through observe class TestViewer(AbstractViewer): def __init__(self): self.cache = list() def observe(self, round_, turn, universe, events): self.cache.append(events) # run the game tv = TestViewer() gm.register_viewer(tv) gm.set_initial() gm.play() # check self.assertTrue(TeamWins in tv.cache[-1]) self.assertEqual(tv.cache[-1][0], TeamWins(1))
def test_kill_before_seek_food(self): my_team = SimpleTeam("test", EaterPlayer(), EaterPlayer()) test_layout = """ ############## #.23 . # #### ## . # # 0. .# # 1 ##.. # ############## """ teams = [ # register my_team for bots 0, 2 my_team, # for now enemies don't move SimpleTeam(TestPlayer(3*'>'), TestPlayer(3*'<')) ] gm = GameMaster(test_layout, teams, number_bots=4, game_time=1, seed=20) # play `game_time` rounds gm.play() # test if both players killed enemy instead of seeking food self.assertEqual(gm.universe.bots[0].current_pos, (3, 4)) self.assertEqual(gm.universe.bots[2].current_pos, (3, 1)) self.assertEqual(gm.universe.teams[0].score, 10)
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)
def run_game(): layout = LAYOUT teams = [ SimpleTeam(StoppingPlayer(), StoppingPlayer()), SimpleTeam(StoppingPlayer(), StoppingPlayer()) ] gm = GameMaster(layout, teams, 4, 10) #gm.register_viewer(AsciiViewer()) gm.play()
def test_too_few_bots(self): test_layout = (""" ############ #0#. .# 1# ############ """) team = [Team(stopping), Team(stopping)] gm = GameMaster(test_layout, team, 2, 1) with pytest.raises(IndexError): gm.play()
def test_demo_players(self): test_layout = (""" ############ #0 #. .# 1# ############ """) team = [SimpleTeam(SpeakingPlayer()), SimpleTeam(RandomPlayer())] gm = GameMaster(test_layout, team, 2, 1) gm.play() self.assertTrue(gm.game_state["bot_talk"][0].startswith("Going")) self.assertEqual(gm.game_state["bot_talk"][1], "")
def test_demo_players(self): test_layout = (""" ############ #0#. .# 1# ############ """) team = [SimpleTeam(NQRandomPlayer()), SimpleTeam(NQRandomPlayer())] gm = GameMaster(test_layout, team, 2, 1) gm.play() self.assertEqual(gm.universe.bots[0].current_pos, (1, 1)) self.assertEqual(gm.universe.bots[1].current_pos, (9, 1))
def test_demo_players(self): test_layout = """ ############ #0#. .# 1# ############ """ gm = GameMaster(test_layout, 2, 1) gm.register_team(SimpleTeam(NQRandomPlayer())) gm.register_team(SimpleTeam(NQRandomPlayer())) gm.play() self.assertEqual(gm.universe.bots[0].current_pos, (1, 1)) self.assertEqual(gm.universe.bots[1].current_pos, (9, 1))
def test_demo_players(self): test_layout = """ ################## #0#. . # . # #2##### #####3# # . # . .#1# ################## """ gm = GameMaster(test_layout, 4, 200) gm.register_team(SimpleTeam(StoppingPlayer(), NQRandomPlayer())) gm.register_team(SimpleTeam(RandomPlayer(), BFSPlayer())) gm.play()
def test_must_not_move_after_last_timeout(self): # 0 must move back and forth because of random steps # but due to its last timeout, it should be disqualified # immediately test_start = ( """ ###### ##0.## # ## # ##. 1# ###### """ ) # players do nothing class TimeOutPlayer(AbstractPlayer): def get_move(self): raise PlayerTimeout class CheckTestPlayer(AbstractPlayer): def get_move(self): raise RuntimeError("This should never be called") teams = [ SimpleTeam(TimeOutPlayer()), SimpleTeam(CheckTestPlayer()) ] # the game lasts one round, and then draws gm = GameMaster(test_start, teams, 2, 100, max_timeouts=1) # 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() print(gm.universe.pretty) print(gm.game_state) # check assert gm.game_state["max_timeouts"] == 1 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 assert gm.universe.bots[0].current_pos == (2,1) assert tv.cache[-1]["team_wins"] is not None assert tv.cache[-1]["team_wins"] == 1 # the game ends in round 0 with bot_id 0 assert gm.game_state["round_index"] == 0 assert gm.game_state["bot_id"] == 0
def test_demo_players(self): test_layout = ( """ ############ #0 #. .# 1# ############ """) gm = GameMaster(test_layout, 2, 1) gm.register_team(SimpleTeam(SpeakingPlayer())) gm.register_team(SimpleTeam(RandomPlayer())) gm.play() self.assertTrue(gm.game_state["bot_talk"][0].startswith("Going")) self.assertEqual(gm.game_state["bot_talk"][1], "")
def test_path(self): test_layout = (""" ############ # . # .# ## # ## # # ## #0#. .##1# ############ """) team = [SimpleTeam(NQRandomPlayer()), SimpleTeam(NQRandomPlayer())] gm = GameMaster(test_layout, team, 2, 7) gm.play() self.assertEqual(gm.universe.bots[0].current_pos, (4, 3)) self.assertEqual(gm.universe.bots[1].current_pos, (10, 3))
def test_demo_players(self): test_layout = ( """ ############ #0#. .# 1# ############ """) team = [ SimpleTeam(NQRandomPlayer()), SimpleTeam(NQRandomPlayer()) ] gm = GameMaster(test_layout, team, 2, 1) gm.play() assert gm.universe.bots[0].current_pos == (1, 1) assert gm.universe.bots[1].current_pos == (9, 1)
def test_demo_players(self): test_layout = ( """ ############ #0 #. .# 1# ############ """) team = [ SimpleTeam(SpeakingPlayer()), SimpleTeam(RandomPlayer()) ] gm = GameMaster(test_layout, team, 2, 1) gm.play() assert gm.game_state["bot_talk"][0].startswith("Going") assert gm.game_state["bot_talk"][1] == ""
def test_unreachable_bot(self): test_layout = ( """ ############ # . 0#. 1# ############ """) game_master = GameMaster(test_layout, 2, 1, noise=False) bfs2 = BasicDefensePlayer() game_master.register_team(SimpleTeam(StoppingPlayer())) game_master.register_team(SimpleTeam(bfs2)) game_master.set_initial() game_master.play() self.assertTrue(bfs2.path is None)
def test_path(self): test_layout = ( """ ############ # . # .# ## # ## # # ## #0#. .##1# ############ """) gm = GameMaster(test_layout, 2, 7) gm.register_team(SimpleTeam(NQRandomPlayer())) gm.register_team(SimpleTeam(NQRandomPlayer())) gm.play() self.assertEqual(gm.universe.bots[0].current_pos, (4, 3)) self.assertEqual(gm.universe.bots[1].current_pos, (10, 3))
def test_too_few_bots(self): test_layout = ( """ ############ #0#. .# 1# ############ """) team = [ Team(stopping), Team(stopping) ] gm = GameMaster(test_layout, team, 2, 1) with pytest.raises(IndexError): gm.play()
def test_failing_player(self): class FailingPlayer(AbstractPlayer): def get_move(self): return 1 test_layout = (""" ###### #0 . # #.. 1# ###### """) teams = [SimpleTeam(FailingPlayer()), SimpleTeam(TestPlayer("^"))] gm = GameMaster(test_layout, teams, 2, 1) gm.play() self.assertEqual(gm.game_state["timeout_teams"], [1, 0])
def test_players(self): test_layout = (""" ############ # . # . ## # ## # ## # ## # # ## # ## # # ## # # # ## #0#. . 1# ############ """) for player in SANE_PLAYERS: team = [SimpleTeam(player()), SimpleTeam(NQRandomPlayer())] gm = GameMaster(test_layout, team, 2, 20) gm.play() assert gm.finished is True
def test_path(self): test_layout = ( """ ############ # . # .# ## # ## # # ## #0#. .##1# ############ """) team = [ SimpleTeam(NQRandomPlayer()), SimpleTeam(NQRandomPlayer()) ] gm = GameMaster(test_layout, team, 2, 7) gm.play() assert gm.universe.bots[0].current_pos == (4, 3) assert gm.universe.bots[1].current_pos == (10, 3)
def test_unreachable_border(self): test_layout = ( """ ############ #0 . #. 1# ############ """) game_master = GameMaster(test_layout, 2, 1, noise=False) bfs1 = BasicDefensePlayer() bfs2 = BasicDefensePlayer() game_master.register_team(SimpleTeam(bfs1)) game_master.register_team(SimpleTeam(bfs2)) game_master.set_initial() game_master.play() self.assertEqual(bfs1.path, [(5, 1), (4, 1), (3, 1)]) self.assertTrue(bfs2.path is None)
def test_failing_player(self): class FailingPlayer(AbstractPlayer): def get_move(self): return 1 test_layout = ( """ ###### #0 . # #.. 1# ###### """) teams = [SimpleTeam(FailingPlayer()), SimpleTeam(SteppingPlayer("^"))] gm = GameMaster(test_layout, teams, 2, 1) gm.play() assert gm.game_state["timeout_teams"] == [1, 0]
def test_failing_player(self): class FailingPlayer(AbstractPlayer): def get_move(self): return 1 test_layout = """ ###### #0 . # #.. 1# ###### """ gm = GameMaster(test_layout, 2, 1) gm.register_team(SimpleTeam(FailingPlayer())) gm.register_team(SimpleTeam(TestPlayer("^"))) gm.play() self.assertEqual(gm.game_state["timeout_teams"], [1, 0])
def test_too_many_moves(self): test_layout = ( """ ############ #0 . . 1# #2 3# ############ """) movements_0 = [east, east] movements_1 = [west, west] teams = [ SimpleTeam(TestPlayer(movements_0), TestPlayer(movements_0)), SimpleTeam(TestPlayer(movements_1), TestPlayer(movements_1)) ] gm = GameMaster(test_layout, teams, 4, 3) with pytest.raises(ValueError): gm.play()
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
def test_rnd(self): outer = self class RndPlayer(AbstractPlayer): def set_initial(self): original_seed = self.current_state["seed"] original_rand = self.rnd.randint(10, 100) outer.assertTrue(10 <= original_rand <= 100) # now check test_rnd = random.Random(original_seed + self._index) outer.assertEqual(test_rnd.randint(10, 100), original_rand) def get_move(self): outer.assertTrue(10 <= self.rnd.randint(10, 100) <= 100) return datamodel.stop class SeedTestingPlayer(AbstractPlayer): def __init__(self): # must be initialised before set_initial is called self.seed_offset = 120 def set_initial(self): original_seed = self.current_state["seed"] original_rand = self.rnd.randint(0, 100) # now check test_rnd = random.Random(original_seed + self.seed_offset) outer.assertEqual(test_rnd.randint(0, 100), original_rand) def get_move(self): outer.assertTrue(10 <= self.rnd.randint(10, 100) <= 100) return datamodel.stop test_layout = (""" ############ #02#. .#31# ############ """) teams = [ SimpleTeam(RndPlayer(), SeedTestingPlayer()), SimpleTeam(SeedTestingPlayer(), RndPlayer()) ] gm = GameMaster(test_layout, teams, 4, 1) gm.play()
def test_stopping(self): test_layout = (""" ############ #0#.23 .# 1# ############ """) round_counting = self.round_counting() team = [Team(stopping), Team(round_counting)] gm = GameMaster(test_layout, team, 4, 1) gm.play() assert gm.universe.bots[0].current_pos == (1, 1) assert gm.universe.bots[1].current_pos == (10, 1) assert round_counting._storage['rounds'] == 1 round_counting = self.round_counting() team = [Team(stopping), Team(round_counting)] gm = GameMaster(test_layout, team, 4, 3) gm.play() assert gm.universe.bots[0].current_pos == (1, 1) assert gm.universe.bots[1].current_pos == (10, 1) assert round_counting._storage['rounds'] == 3
def test_demo_players(self): test_layout = (""" ################ # # # # # # # 0 1 # # # # # # # #. .# ################ """) teams = [SimpleTeam(RandomPlayer()), SimpleTeam(RandomPlayer())] gm = GameMaster(test_layout, teams, 2, 5, seed=20) self.assertEqual(gm.universe.bots[0].current_pos, (4, 4)) self.assertEqual(gm.universe.bots[1].current_pos, (4 + 7, 4)) gm.play() pos_left_bot = gm.universe.bots[0].current_pos pos_right_bot = gm.universe.bots[1].current_pos # running again to test seed: teams = [SimpleTeam(RandomPlayer()), SimpleTeam(RandomPlayer())] gm = GameMaster(test_layout, teams, 2, 5, seed=20) gm.play() self.assertEqual(gm.universe.bots[0].current_pos, pos_left_bot) self.assertEqual(gm.universe.bots[1].current_pos, pos_right_bot) # running again with other seed: teams = [SimpleTeam(RandomPlayer()), SimpleTeam(RandomPlayer())] gm = GameMaster(test_layout, teams, 2, 5, seed=200) gm.play() # most probably, either the left bot or the right bot or both are at # a different position self.assertTrue(gm.universe.bots[0].current_pos != pos_left_bot or gm.universe.bots[1].current_pos != pos_right_bot)
def test_test_players(self): test_layout = (""" ############ #0 . . 1# #2 3# ############ """) movements_0 = [east, east] movements_1 = [west, west] teams = [ SimpleTeam(TestPlayer(movements_0), TestPlayer(movements_0)), SimpleTeam(TestPlayer(movements_1), TestPlayer(movements_1)) ] gm = GameMaster(test_layout, teams, 4, 2) self.assertEqual(gm.universe.bots[0].current_pos, (1, 1)) self.assertEqual(gm.universe.bots[1].current_pos, (10, 1)) self.assertEqual(gm.universe.bots[2].current_pos, (1, 2)) self.assertEqual(gm.universe.bots[3].current_pos, (10, 2)) gm.play() self.assertEqual(gm.universe.bots[0].current_pos, (3, 1)) self.assertEqual(gm.universe.bots[1].current_pos, (8, 1)) self.assertEqual(gm.universe.bots[2].current_pos, (3, 2)) self.assertEqual(gm.universe.bots[3].current_pos, (8, 2))
#!/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()
def test_game(self): test_start = (""" ###### #0 . # #.. 1# ###### """) number_bots = 2 # The problem here is that the layout does not allow us to specify a # different inital position and current position. When testing universe # equality by comparing its string representation, this does not matter. # But if we want to compare using the __eq__ method, but specify the # target as ascii encoded maze/layout we need to convert the layout to a # CTFUniverse and then modify the initial positions. For this we define # a closure here to quickly generate a target universe to compare to. # Also we adapt the score, in case food has been eaten def create_TestUniverse(layout, black_score=0, white_score=0): initial_pos = [(1, 1), (4, 2)] universe = CTFUniverse.create(layout, number_bots) universe.teams[0].score = black_score universe.teams[1].score = white_score for i, pos in enumerate(initial_pos): universe.bots[i].initial_pos = pos if not (1, 2) in universe.food_list: universe.teams[1].score += 1 if not (2, 2) in universe.food_list: universe.teams[1].score += 1 if not (3, 1) in universe.food_list: universe.teams[0].score += 1 return universe teams = [ SimpleTeam(TestPlayer('>-v>>>')), SimpleTeam(TestPlayer('<<-<<<')) ] gm = GameMaster(test_start, teams, number_bots, 200) gm.set_initial() gm.play_round() test_first_round = (""" ###### # 0. # #..1 # ###### """) self.assertEqual(create_TestUniverse(test_first_round), gm.universe) gm.play_round() test_second_round = (""" ###### # 0. # #.1 # ###### """) self.assertEqual(create_TestUniverse(test_second_round), gm.universe) gm.play_round() test_third_round = (""" ###### # . # #.0 1# ###### """) self.assertEqual( create_TestUniverse(test_third_round, black_score=gm.universe.KILLPOINTS), gm.universe) gm.play_round() test_fourth_round = (""" ###### #0 . # #. 1 # ###### """) self.assertEqual( create_TestUniverse(test_fourth_round, black_score=gm.universe.KILLPOINTS, white_score=gm.universe.KILLPOINTS), gm.universe) gm.play_round() test_fifth_round = (""" ###### # 0. # #.1 # ###### """) self.assertEqual( create_TestUniverse(test_fifth_round, black_score=gm.universe.KILLPOINTS, white_score=gm.universe.KILLPOINTS), gm.universe) print(gm.universe.pretty) gm.play_round() test_sixth_round = (""" ###### # 0 # #1 # ###### """) print(gm.universe.pretty) self.assertEqual( create_TestUniverse(test_sixth_round, black_score=gm.universe.KILLPOINTS, white_score=gm.universe.KILLPOINTS), gm.universe) teams = [ SimpleTeam(TestPlayer('>-v>>>')), SimpleTeam(TestPlayer('<<-<<<')) ] # now play the full game gm = GameMaster(test_start, teams, number_bots, 200) gm.play() test_sixth_round = (""" ###### # 0 # #1 # ###### """) self.assertEqual( create_TestUniverse(test_sixth_round, black_score=gm.universe.KILLPOINTS, white_score=gm.universe.KILLPOINTS), gm.universe)
def test_remote_viewer_may_not_change_gm(self): free_obj = Free self.mean_viewer_did_run = False class MeanViewer(AbstractViewer): def set_initial(self, universe): universe.teams[1].score = 50 def observe(self_, universe, game_state): self.mean_viewer_did_run = True universe.teams[0].score = 100 universe.bots[0].current_pos = (4, 4) universe.maze[0, 0] = free_obj game_state["team_wins"] = 0 test_start = (""" ###### #0 . # #.. 1# ###### """) number_bots = 2 gm = GameMaster(test_start, number_bots, 1) gm.register_team(SimpleTeam(TestPlayer([(0, 0)]))) gm.register_team(SimpleTeam(TestPlayer([(0, 0)]))) original_universe = gm.universe.copy() self.test_viewer_did_run = False test_self = self class TestViewer(AbstractViewer): def observe(self_, universe, game_state): self.test_viewer_did_run = True # universe should not have been altered test_self.assertEqual(original_universe, gm.universe) # there should only be a botmoves event test_self.assertEqual(len(game_state["bot_moved"]), 1) test_self.assertEqual(len(game_state["bot_moved"]), 1) # We need to be able to tell when our subscriber is able to receive # new events from the publisher. # Due to its completely asynchronous approach, zmq does not even # provide built-in methods to check whether two or more sockets # are connected, so we have to figure a way to find out. # The approach is as follows: When the publisher starts, it # sends only ‘sync’ messages without a pause. # When a subscriber is finally connected, it will receive this message and # set an instance variable (`has_sync`). The main thread checks whether # all variables of all subscribers have been set, will stop # sending ‘sync’ and move on. # No special thread synchronisation or locking is being used. The current # code is hopefully simple enough not to include any race conditions. class SyncedSubscriber(SimpleSubscriber): def sync(self): self.has_sync = True address = "ipc:///tmp/pelita-publisher-%s" % uuid.uuid4() mean_viewer = SyncedSubscriber(MeanViewer(), address) test_viewer = SyncedSubscriber(TestViewer(), address) # must be threads because we try to access shared state # in the mean_viewer_did_run variable # (and in a bad way) mean_viewer_thread = mean_viewer.autoplay_thread() test_viewer_thread = test_viewer.autoplay_thread() publisher_viewer = SimplePublisher(address) viewers = [mean_viewer, test_viewer] while not all( getattr(viewer, "has_sync", False) for viewer in viewers): publisher_viewer.socket.send_json({"__action__": "sync"}) # now we can register it and game_master takes care of sending messages gm.register_viewer(publisher_viewer) gm.set_initial() gm.play() # exit our threads publisher_viewer.socket.send_json({ "__action__": "exit", "__data__": {} }) # wait until threads stop mean_viewer_thread.join() test_viewer_thread.join() # must close the socket and terminate the context # else we may get an assertion failure in zmq publisher_viewer.socket.close() publisher_viewer.context.term() self.assertEqual(original_universe, gm.universe) # check, that the code was actually executed self.assertTrue(self.mean_viewer_did_run) self.assertTrue(self.test_viewer_did_run)