PageRenderTime 194ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_player.py

https://github.com/pv/pelita
Python | 196 lines | 189 code | 3 blank | 4 comment | 0 complexity | 357c4ff09568c7fefbf04e32511d5d74 MD5 | raw file
  1. import unittest
  2. from pelita.player import *
  3. from pelita.datamodel import create_CTFUniverse, north, stop, east
  4. from pelita.game_master import GameMaster
  5. from pelita.viewer import AsciiViewer
  6. class TestAbstractPlayer(unittest.TestCase):
  7. def test_convenience(self):
  8. test_layout = (
  9. """ ##################
  10. #0#. . # . #
  11. #2##### #####1#
  12. # . # . .#3#
  13. ################## """)
  14. game_master = GameMaster(test_layout, 4, 200, noise=False)
  15. universe = game_master.universe
  16. player_0 = StoppingPlayer()
  17. player_1 = TestPlayer([stop, north])
  18. player_2 = StoppingPlayer()
  19. player_3 = StoppingPlayer()
  20. game_master.register_team(SimpleTeam(player_0, player_2))
  21. game_master.register_team(SimpleTeam(player_1, player_3))
  22. game_master.set_initial()
  23. self.assertEqual(universe.bots[0], player_0.me)
  24. self.assertEqual(universe.bots[1], player_1.me)
  25. self.assertEqual(universe.bots[2], player_2.me)
  26. self.assertEqual(universe.bots[3], player_3.me)
  27. self.assertEqual(universe, player_1.current_uni)
  28. self.assertEqual([universe.bots[0]], player_2.other_team_bots)
  29. self.assertEqual([universe.bots[1]], player_3.other_team_bots)
  30. self.assertEqual([universe.bots[2]], player_0.other_team_bots)
  31. self.assertEqual([universe.bots[3]], player_1.other_team_bots)
  32. self.assertEqual([universe.bots[i] for i in (0, 2)], player_0.team_bots)
  33. self.assertEqual([universe.bots[i] for i in (1, 3)], player_0.enemy_bots)
  34. self.assertEqual([universe.bots[i] for i in (1, 3)], player_1.team_bots)
  35. self.assertEqual([universe.bots[i] for i in (0, 2)], player_1.enemy_bots)
  36. self.assertEqual([universe.bots[i] for i in (0, 2)], player_2.team_bots)
  37. self.assertEqual([universe.bots[i] for i in (1, 3)], player_2.enemy_bots)
  38. self.assertEqual([universe.bots[i] for i in (1, 3)], player_3.team_bots)
  39. self.assertEqual([universe.bots[i] for i in (0, 2)], player_3.enemy_bots)
  40. self.assertEqual(universe.bots[1].current_pos, player_1.current_pos)
  41. self.assertEqual(universe.bots[1].initial_pos, player_1.initial_pos)
  42. self.assertEqual(universe.teams[0], player_0.team)
  43. self.assertEqual(universe.teams[0], player_2.team)
  44. self.assertEqual(universe.teams[1], player_1.team)
  45. self.assertEqual(universe.teams[1], player_3.team)
  46. self.assertEqual({(0, 1): (1, 2), (0, 0): (1, 1)},
  47. player_0.legal_moves)
  48. self.assertEqual({(0, 1): (16, 3), (0, -1): (16, 1), (0, 0): (16, 2)},
  49. player_1.legal_moves)
  50. self.assertEqual({(0, 1): (1, 3), (0, -1): (1, 1), (0, 0): (1, 2)},
  51. player_2.legal_moves)
  52. self.assertEqual({(0, -1): (16, 2), (0, 0): (16, 3)},
  53. player_3.legal_moves)
  54. game_master.play_round(0)
  55. game_master.play_round(1)
  56. self.assertEqual(universe, player_1.current_uni)
  57. self.assertEqual((16, 1), player_1.current_pos)
  58. self.assertEqual((16, 2), player_1.previous_pos)
  59. self.assertNotEqual(player_1.current_uni, player_1.universe_states[-2])
  60. class TestNQRandom_Player(unittest.TestCase):
  61. def test_demo_players(self):
  62. test_layout = (
  63. """ ############
  64. #0#1 # #
  65. ############ """)
  66. gm = GameMaster(test_layout, 2, 1)
  67. gm.register_team(SimpleTeam(NQRandomPlayer()))
  68. gm.register_team(SimpleTeam(NQRandomPlayer()))
  69. gm.play()
  70. self.assertEqual(gm.universe.bots[0].current_pos, (1, 1))
  71. self.assertEqual(gm.universe.bots[1].current_pos, (4, 1))
  72. class TestBFS_Player(unittest.TestCase):
  73. def test_demo_players(self):
  74. test_layout = (
  75. """ ##################
  76. #0#. . # . #
  77. #2##### #####3#
  78. # . # . .#1#
  79. ################## """)
  80. gm = GameMaster(test_layout, 4, 200)
  81. gm.register_team(SimpleTeam(StoppingPlayer(), NQRandomPlayer()))
  82. gm.register_team(SimpleTeam(RandomPlayer(), BFSPlayer()))
  83. gm.play()
  84. def test_adjacency_bfs(self):
  85. test_layout = (
  86. """ ##################
  87. #0#. . # . #
  88. # ##### ##### #
  89. # . # . .#1#
  90. ################## """)
  91. game_master = GameMaster(test_layout, 2, 200)
  92. bfs = BFSPlayer()
  93. stopping = StoppingPlayer()
  94. game_master.register_team(SimpleTeam(bfs))
  95. game_master.register_team(SimpleTeam(stopping))
  96. game_master.set_initial()
  97. path_target = [(11, 3), (10, 3), (10, 2), (9, 2), (8, 2), (7, 2), (7,
  98. 3), (6, 3), (5, 3), (4, 3), (3, 3), (2, 3), (1, 3), (1,2)]
  99. self.assertEqual(path_target, bfs.current_path)
  100. for i in range(len(path_target)):
  101. path_target.pop()
  102. game_master.play_round(i)
  103. self.assertEqual(path_target, bfs.current_path)
  104. game_master.play_round(i)
  105. self.assertEqual([(14, 3), (13, 3)], bfs.current_path)
  106. game_master.play_round(i+1)
  107. self.assertEqual([(14, 3)], bfs.current_path)
  108. game_master.play_round(i+2)
  109. self.assertEqual([], bfs.current_path)
  110. def test_unreachable(self):
  111. test_layout = (
  112. """ ############
  113. #0. #.1#
  114. ############ """)
  115. game_master = GameMaster(test_layout, 2, 200)
  116. bfs1 = BFSPlayer()
  117. bfs2 = BFSPlayer()
  118. game_master.register_team(SimpleTeam(bfs1))
  119. game_master.register_team(SimpleTeam(bfs2))
  120. game_master.set_initial()
  121. game_master.play_round(0)
  122. self.assertEqual(0, len(bfs1.current_path))
  123. self.assertEqual(0, len(bfs2.current_path))
  124. class TestSimpleTeam(unittest.TestCase):
  125. def test_simple_team(self):
  126. class BrokenPlayer(AbstractPlayer):
  127. pass
  128. self.assertRaises(TypeError, SimpleTeam, BrokenPlayer())
  129. def test_init(self):
  130. self.assertRaises(ValueError, SimpleTeam)
  131. object_which_is_neither_string_nor_team = 5
  132. self.assertRaises(AttributeError, SimpleTeam, object_which_is_neither_string_nor_team)
  133. team0 = SimpleTeam("my team")
  134. self.assertEqual(team0.team_name, "my team")
  135. self.assertEqual(len(team0._players), 0)
  136. team1 = SimpleTeam("my team", TestPlayer([]))
  137. self.assertEqual(team1.team_name, "my team")
  138. self.assertEqual(len(team1._players), 1)
  139. team2 = SimpleTeam("my other team", TestPlayer([]), TestPlayer([]))
  140. self.assertEqual(team2.team_name, "my other team")
  141. self.assertEqual(len(team2._players), 2)
  142. team3 = SimpleTeam(TestPlayer([]))
  143. self.assertEqual(team3.team_name, "")
  144. self.assertEqual(len(team3._players), 1)
  145. team4 = SimpleTeam(TestPlayer([]), TestPlayer([]))
  146. self.assertEqual(team4.team_name, "")
  147. self.assertEqual(len(team4._players), 2)
  148. def test_bot_ids(self):
  149. layout = (
  150. """ ####
  151. #01#
  152. #### """
  153. )
  154. dummy_universe = create_CTFUniverse(layout, 2)
  155. team1 = SimpleTeam(TestPlayer([north]), TestPlayer([east]))
  156. self.assertRaises(ValueError, team1._set_bot_ids, [1, 5, 10])
  157. team1._set_bot_ids([1,5])
  158. team1._set_initial(dummy_universe)
  159. self.assertEqual(team1._get_move(1, dummy_universe), north)
  160. self.assertEqual(team1._get_move(5, dummy_universe), east)
  161. self.assertRaises(KeyError, team1._get_move, 6, dummy_universe)
  162. team2 = SimpleTeam(TestPlayer([north]), TestPlayer([east]))
  163. team2._set_bot_ids([1])
  164. team2._set_initial(dummy_universe)
  165. self.assertEqual(team2._get_move(1, dummy_universe), north)
  166. self.assertRaises(KeyError, team2._get_move, 0, dummy_universe)
  167. self.assertRaises(KeyError, team2._get_move, 2, dummy_universe)