-
Notifications
You must be signed in to change notification settings - Fork 3
/
DTOs.py
119 lines (102 loc) · 4.22 KB
/
DTOs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import json
def obj_dict(obj):
return obj.__dict__
class Directions:
NORTH = 'North'
SOUTH = 'South'
EAST = 'East'
WEST = 'West'
STOP = 'Stop'
LEFT = {NORTH: WEST,
SOUTH: EAST,
EAST: NORTH,
WEST: SOUTH,
STOP: STOP}
RIGHT = dict([(y, x) for x, y in LEFT.items()])
REVERSE = {NORTH: SOUTH,
SOUTH: NORTH,
EAST: WEST,
WEST: EAST,
STOP: STOP}
class PublicGameState:
def __init__(self, gameState=None, jsonString=None):
self.gameField = [[]]
self.publicPlayers = []
if jsonString != None:
self._create_self_from_json(jsonString)
else:
layout = gameState.data.layout
food = gameState.data.food
capsules = gameState.data.capsules
height, width = layout.height, layout.width
self.gameField = [[PublicFields.EMPTY for x in range(width)] for y in range(height)]
for x in range(height):
for y in range(width):
if layout.walls[y][x]:
self.gameField[x][y] = PublicFields.WALL
elif food[y][x]:
self.gameField[x][y] = PublicFields.FOOD
for idx, val in enumerate(capsules):
self.gameField[val[1]][val[0]] = PublicFields.CAPSULE
for agent in gameState.data.agentStates[:]:
weakened = (agent.scaredTimer > 0)
self.publicPlayers.append(PublicPlayer(isPacman=agent.isPacman,
direction=agent.getDirection(),
position=agent.getPosition(),
weakened=weakened))
def _create_self_from_json(self, jsonString):
loadedJsonString = json.loads(jsonString)
for key, value in loadedJsonString.items():
if key == "publicPlayers":
self.publicPlayers = self._instance_players_out_of_json_string(value)
if key == "gameField":
self.gameField = self._instance_game_field_out_of_json_string(value)
@staticmethod
def _instance_game_field_out_of_json_string(jsonString):
myGameField = [[]]
for fieldRowElements in jsonString:
myGameField.append(fieldRowElements)
return myGameField
@staticmethod
def _instance_players_out_of_json_string(jsonString):
myPublicPlayers = []
for publicPlayersJsonString in jsonString:
myPublicPlayers.append(PublicPlayer(jsonString=publicPlayersJsonString))
return myPublicPlayers
def addTeamInfoToField(self, gameState, teamIndices):
print("Agent Indices")
for teamRedId in teamIndices[:]:
teamRedPosition = gameState.getAgentPosition(teamRedId)
teamRedAgentStatus = gameState.getAgentState(teamRedId)
self.gameField[teamRedPosition[0]][teamRedPosition[1]] = PublicPlayer(teamRed=True,
isPacman=teamRedAgentStatus.isPacman,
direction=teamRedAgentStatus.getDirection())
def __str__(self):
return json.dumps(self, default=obj_dict)
class PublicPlayer:
def __init__(self, isPacman=True, direction=Directions.NORTH, position=[0, 0], jsonString=None, weakened=False):
self.isPacman = isPacman
self.direction = direction
self.position = position
self.weakened = weakened
if (jsonString != None):
self.__dict__ = jsonString
def __str__(self):
returnVal = 'G'
if self.direction == Directions.NORTH:
returnVal = 'N'
if self.direction == Directions.SOUTH:
returnVal = 'S'
if self.direction == Directions.WEST:
returnVal = 'W'
if self.direction == Directions.EAST:
returnVal = 'E'
if self.isPacman:
return returnVal.lower()
return returnVal.upper()
class PublicFields:
EMPTY = " "
WALL = "%"
FOOD = "°"
CAPSULE = "o"
PLAYER = "P"