def test_surround_player_with_conquers(self): # fmt: off gm = create_map_with([ [1, 1, 1, E, E], [1, E, 1, E, E], [E, E, E, E, E], [E, E, E, 2, 2], [E, E, E, 2, 2], ]) # fmt: on p1 = PlayerState(id=1, name="p1", game_map=gm, position=Position(1, 3), direction=Direction(Direction.RIGHT)) p2 = PlayerState(id=2, name="p2", game_map=gm, position=Position(4, 4), direction=Direction(Direction.LEFT)) gs = GameState(gm, [p1, p2]) gs.apply_action(0, p2, Action.FORWARD) gs.apply_action(0, p2, Action.FORWARD) gs.apply_action(0, p2, Action.TURN_RIGHT) gs.apply_action(0, p2, Action.FORWARD) self.assertEqual(Position(2, 2), p2.position) self.assertEqual(5, len(p2.tail)) gs.apply_action(0, p1, Action.FORWARD) # walk on tail, p2 killed self.assertTrue(p2.killed) self.assertFalse(p1.killed) self.assertEqual(0, p2.stats.stats[PlayerStats.CONQUERED])
def test_with_direction(self): data = """ WWWWWW W1 3W WD DW WD DW W4 2W WWWWWW\r\n """ gc = GameConfig.from_str(data) self.assertEqual( gc.spawn_positions, [Position(1, 1), Position(4, 4), Position(4, 1), Position(1, 4)]) self.assertEqual( gc.spawn_directions, [ Direction(Direction.DOWN), Direction(Direction.UP), Direction(Direction.DOWN), Direction(Direction.UP) ], )
def test_add_player_should_respect_orientation(self): gc = GameConfig(GameMap(20), spawn_positions=[Position(1, 1)], spawn_directions=[Direction(Direction.LEFT)]) gs = GameState(gc, players=None) self.assertEqual(len(gs.players), 0) gs.add_player("Karl Marx") self.assertEqual(Direction(Direction.LEFT), gs.players[0].direction)
def test_init_with_direction(self): game_map = GameMap(5) pos = Position(1, 1) ps = PlayerState(id=1, name="dummy", game_map=game_map, position=pos, direction=Direction(Direction.DOWN)) self.assertEqual(ps.direction, Direction(Direction.DOWN))
def direction_to(self, target: Position) -> Optional[Direction]: if self.y > target.y: return Direction(Direction.UP) elif self.y < target.y: return Direction(Direction.DOWN) elif self.x > target.x: return Direction(Direction.LEFT) elif self.x < target.x: return Direction(Direction.RIGHT) else: return None
def test_fill_basic_2(self): # fmt: off game_map = create_map_with([[E, 1, 1, E, E], [E, 1, 1, E, E], [E, E, E, E, E], [E, E, E, E, E], [E, E, E, E, E]]) # fmt: on ps = PlayerState(id=1, name="dummy", game_map=game_map, position=Position(2, 2), direction=Direction(Direction.DOWN)) gs = GameState(game_map, [ps]) gs.apply_action(0, ps, Action.FORWARD) gs.apply_action(1, ps, Action.TURN_RIGHT) gs.apply_action(2, ps, Action.TURN_LEFT) gs.apply_action(3, ps, Action.TURN_LEFT) gs.apply_action(4, ps, Action.FORWARD) gs.apply_action(4, ps, Action.FORWARD) gs.apply_action(5, ps, Action.TURN_LEFT) gs.apply_action(6, ps, Action.FORWARD) gs.apply_action(7, ps, Action.TURN_LEFT) # fmt: off self.assertEqual( game_map, create_map_with([[E, 1, 1, E, E], [E, 1, 1, 1, E], [1, 1, 1, 1, E], [1, 1, 1, 1, E], [E, E, E, E, E]])) # fmt: on self.assertEqual(ps.score, 9)
def test_is_closed_tail(self): # fmt: off gm = create_map_with([[1, E, 1], [E, E, E], [1, E, E]]) # fmt: on # player without tail player = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1)) self.assertEqual(player.tail, [Position(1, 1)]) self.assertFalse(GameState.is_closed_tail(player, gm)) # player with tail player.tail = [Position(1, 1), Position(2, 1)] self.assertFalse(GameState.is_closed_tail(player, gm)) player.tail = [Position(1, 1), Position(2, 1), Position(3, 1)] player.direction = Direction(Direction.UP) self.assertTrue(GameState.is_closed_tail(player, gm)) player.tail = [Position(1, 1), Position(2, 1), Position(2, 2)] self.assertFalse(GameState.is_closed_tail(player, gm)) player.tail = [ Position(1, 1), Position(2, 1), Position(2, 2), Position(1, 2), Position(1, 1) ] self.assertTrue(GameState.is_closed_tail(player, gm))
def test_move_player(self): gm = GameMap(5) player = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1)) player.direction = Direction(Direction.RIGHT) gs = GameState(gm, [player]) gs.move_player(player, Action.FORWARD) gs.move_player(player, Action.TURN_RIGHT) gs.move_player(player, Action.TURN_RIGHT) gs.move_player(player, Action.TURN_LEFT) self.assertEqual(Position(1, 3), player.position)
def test_fill_kill_other_player_tail(self): # fmt: off game_map = create_map_with([[E, E, E, E], [E, E, E, E], [0, 0, 0, 0], [E, 1, E, E]]) # fmt: on p0 = PlayerState(id=0, name="dummy", game_map=game_map, position=Position(1, 3)) p1 = PlayerState(id=1, name="dummy", game_map=game_map, position=Position(2, 4)) gs = GameState(game_map, [p0, p1]) p0.tail = [ p0.spawn_position.copy(), Position(1, 2), Position(1, 1), Position(2, 1), Position(3, 1), Position(4, 1), Position(4, 2), ] p0.position = p0.tail[-1].copy() p0.direction = Direction(Direction.DOWN) p1.tail = [ p1.spawn_position.copy(), Position(2, 3), Position(2, 2), Position(3, 2), Position(3, 3), Position(3, 4), ] p1.position = p1.tail[-1].copy() p1.direction = Direction(Direction.DOWN) # print(game_state_to_dict(0, 0, gs, 999)['game']['pretty_map']) gs.apply_action(0, p0, Action.FORWARD) # print(game_state_to_dict(0, 0, gs, 999)['game']['pretty_map']) self.assertTrue(p1.killed)
def dict_to_player_state(data: Dict, game_map: GameMap) -> PlayerState: position = dict_to_position(data["position"]) ps = PlayerState(id=data["id"], name=data["name"], game_map=game_map, position=position, init=False) ps.active = data["active"] ps.killed = data["killed"] ps.position = position ps.spawn_position = dict_to_position(data["spawn_position"]) ps.direction = Direction(data["direction"]) ps.spawn_direction = Direction(data["spawn_direction"]) ps.tail = [dict_to_position(p) for p in data["tail"]] ps.score = data["score"] ps.stats = dict_to_player_stats(data["stats"]) for hist in data["history"]: dt = datetime.fromisoformat(hist["timestamp"]) ps.history.append( HistoryItem(game_tick=hist["tick"], message=hist["message"], ts=dt)) return ps
def test_capturing_a_blitzium(self): # fmt: off gm = create_map_with([[0, 0, 0], [E, E, 0], [0, 0, 0]]) # fmt: on gm.set_tile(Position(2, 2), GameMap.BLITZIUM) p0 = PlayerState(id=0, name="p0", game_map=gm, position=Position(1, 1), direction=Direction(Direction.DOWN)) gs = GameState(gm, [p0]) gs.apply_action(0, p0, Action.FORWARD) self.assertFalse(PlayerStats.BLITZIUMS in p0.stats.stats) gs.apply_action(0, p0, Action.FORWARD) self.assertFalse(p0.killed) self.assertEqual(1, p0.stats.stats[PlayerStats.BLITZIUMS])
def test_capturing_a_black_hole_suicide(self): # fmt: off gm = create_map_with([[0, 0, 0], [E, E, 0], [0, 0, 0]]) # fmt: on gm.set_tile(Position(2, 2), GameMap.BLACK_HOLE) p0 = PlayerState(id=0, name="p0", game_map=gm, position=Position(1, 1), direction=Direction(Direction.DOWN)) gs = GameState(gm, [p0]) gs.apply_action(0, p0, Action.FORWARD) self.assertFalse(p0.killed) gs.apply_action(0, p0, Action.FORWARD) self.assertTrue(p0.killed) self.assertEqual(1, p0.stats.stats[PlayerStats.SUICIDES])
def test_walk_on_asteroids_suicide(self): gm = GameMap(5) p1 = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1)) self.assertEqual(p1.direction, Direction.RIGHT) gs = GameState(gm, players=[p1]) # validate that a turn left should walk on asteroids pos = p1.position.copy().move( Direction(Direction.RIGHT).change_direction(Action.TURN_LEFT)) self.assertEqual(pos, Position(1, 0)) self.assertTrue(gm.is_asteroids(pos)) gs.apply_action(1, p1, Action.TURN_LEFT) # walk on tail = suicide, respawn self.assertEqual(p1.position, Position(1, 1)) self.assertEqual(len(p1.tail), 1) self.assertTrue(p1.killed) self.assertEqual(1, p1.stats.stats[PlayerStats.SUICIDES])
def test_surround_player(self): # fmt: off gm = create_map_with([ [1, 1, 1], [1, E, 1], [E, E, E], ]) # fmt: on p1 = PlayerState(id=1, name="p1", game_map=gm, position=Position(1, 3), direction=Direction(Direction.RIGHT)) p2 = PlayerState(id=2, name="p2", game_map=gm, position=Position(2, 2)) gs = GameState(gm, [p1, p2]) gs.apply_action(0, p1, Action.FORWARD) gs.apply_action(0, p1, Action.FORWARD) gs.apply_action(0, p1, Action.TURN_LEFT) self.assertTrue(p2.killed) self.assertFalse(p1.killed)
def test_conquer_some_player_tile(self): # fmt: off gm = create_map_with([ [1, 1, E], [1, 2, 2], [E, 2, 2], ]) # fmt: on p1 = PlayerState(id=1, name="p1", game_map=gm, position=Position(1, 1)) p2 = PlayerState(id=2, name="p2", game_map=gm, position=Position(2, 2)) gs = GameState(gm, [p1, p2]) p1.position = Position(2, 2) p1.tail = [Position(1, 2), Position(2, 2)] p2.position = Position(2, 3) p2.tail = [Position(2, 3)] p1.direction = Direction(Direction.RIGHT) gs.apply_action(0, p1, Action.TURN_LEFT) self.assertFalse(p2.killed) self.assertFalse(p1.killed)
def test_respawn_on_someone_tail(self): gm = GameMap(5) p1 = PlayerState(id=1, name="p1", game_map=gm, position=Position(1, 1)) p2 = PlayerState(id=2, name="p2", game_map=gm, position=Position(1, 2)) gs = GameState(gm, [p1, p2]) # p1: spawn position = 1,1, current position=3,1, tail [2,1 and 3,1] # p2: spawn position = 1,2, current position=2,2, tail [1,1, 1,2 and 2,2] p1.position = Position(3, 1) p1.tail = [Position(2, 1), Position(3, 1)] gm.set_tile(Position(1, 1), GameMap.EMPTY, 2) p2.position = Position(2, 2) p2.tail = [Position(1, 1), Position(1, 2), Position(2, 2)] p2.direction = Direction(Direction.UP) gs.apply_action(0, p2, Action.FORWARD) self.assertTrue(p1.killed) self.assertFalse(p2.killed) # respawn gs.respawn_player(p1) self.assertFalse(p1.killed) self.assertTrue(p2.killed)
def test_game_state_to_json_player(self): gm = create_map_with([[E, E, E], [E, E, E], [E, E, E]]) gm.set_tile(Position(1, 3), GameMap.BLACK_HOLE) gm.set_tile(Position(2, 3), GameMap.BLITZIUM) gm.set_tile(Position(3, 3), GameMap.PLANET) gm.set_tile(Position(3, 2), GameMap.PLANET, 0) p0 = PlayerState(0, "p0", gm, Position(1, 1), direction=Direction(Direction.UP)) p0.killed = True p0.position = Position(3, 1) p0.direction = Direction(Direction.RIGHT) p0.tail = [p0.spawn_position, Position(2, 1), p0.position] p0.score = 123.4 p0.stats.kill_player("p1") p0.stats.killed_by_player("p2") p0.stats.add_stat(PlayerStats.SUICIDES) p0.history.append( HistoryItem(11, "message-11", datetime.datetime(1900, 1, 1, 13, 14, 15, 555))) p0.history.append( HistoryItem(10, "message-10", datetime.datetime(1900, 1, 1, 13, 14, 15, 444))) p1 = PlayerState(1, "p1", gm, position=Position(1, 2), direction=Direction(Direction.DOWN)) p1.active = False p1.direction = Direction(Direction.LEFT) p2 = PlayerState(2, "p2", gm, Position(2, 2), direction=Direction(Direction.RIGHT)) p2.direction = Direction(Direction.LEFT) gs = GameState(gm, [p0, p1, p2]) self.maxDiff = 4000 expected = { "type": "tick", "game": { "map": [ ["W", "W", "W", "W", "W"], ["W", "C-0", " ", " ", "W"], ["W", "C-1", "C-2", "%-0", "W"], ["W", "!", "$", "%", "W"], ["W", "W", "W", "W", "W"], ], "player_id": 1, # fmt: off "pretty_map": '[[W W W W W ]\n' ' [W C0T0 T0 P0 W ]\n' ' [W C1P1 C2P2 %0 W ]\n' ' [W ! $ % W ]\n' ' [W W W W W ]]\n' 'DISCLAMER: this map does not have the same content as ' 'the json game map. Symbols are combined to help you ' 'visualize every spot on this turn. Hyphen are also ' 'removed. See the documentation for the json map ' 'symbol signification.\n' 'Symbols added or modified on this map for ' 'visualization purpose are:\n' 'Px: Position of player x - Cx: Conquered by player x ' '- Tx: Tail of player x', # fmt: on "tick": 123, "ticks_left": 500, }, "players": [ { "active": True, "killed": True, "position": { "x": 3, "y": 1 }, "spawn_position": { "x": 1, "y": 1 }, "direction": "RIGHT", "spawn_direction": "UP", "tail": [{ "x": 1, "y": 1 }, { "x": 2, "y": 1 }, { "x": 3, "y": 1 }], "id": 0, "name": "p0", "score": 123.4, "stats": { PlayerStats.CONQUERED: 1, PlayerStats.KILLS: 1, PlayerStats.KILLED: 1, PlayerStats.SUICIDES: 1, PlayerStats.NEMESIS: "p2", "players_killed": { "p1": 1 }, "killed_by_players": { "p2": 1 }, }, "history": [ { "timestamp": "1900-01-01T13:14:15.000555", "tick": 11, "message": "message-11" }, { "timestamp": "1900-01-01T13:14:15.000444", "tick": 10, "message": "message-10" }, ], }, { "active": False, "killed": False, "position": { "x": 1, "y": 2 }, "spawn_position": { "x": 1, "y": 2 }, "direction": "LEFT", "spawn_direction": "DOWN", "tail": [{ "x": 1, "y": 2 }], "id": 1, "name": "p1", "score": 0, "stats": { PlayerStats.CONQUERED: 1, "players_killed": {}, "killed_by_players": {} }, "history": [], }, { "active": True, "killed": False, "position": { "x": 2, "y": 2 }, "spawn_position": { "x": 2, "y": 2 }, "direction": "LEFT", "spawn_direction": "RIGHT", "tail": [{ "x": 2, "y": 2 }], "id": 2, "name": "p2", "score": 0, "stats": { PlayerStats.CONQUERED: 1, "players_killed": {}, "killed_by_players": {} }, "history": [], }, ], } # players have an id message = game_state_to_json(game_tick=123, player_id=1, game_state=gs, ticks_left=500) # print(message) data = json.loads(message) self.assertEqual(expected, data)