-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.py
More file actions
375 lines (315 loc) · 18.6 KB
/
GUI.py
File metadata and controls
375 lines (315 loc) · 18.6 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from tkinter import*
import json
import gym
import gym_zgame
from gym_zgame.envs.enums.PLAYER_ACTIONS import LOCATIONS, DEPLOYMENTS
class GUI():
def __init__(self, root, zgame):
self.GAME_ID = zgame.GAME_ID
print('Starting new game with human play!')
#Variables for tkinter GUI
self.root = root
#Variables for Game
self.env = zgame.env
self.env.reset()
self.DATA_LOG_FILE_NAME = zgame.DATA_LOG_FILE_NAME
self.turn = zgame.turn
self.max_turns = zgame.max_turns
self.neighborhoods, self.score, self.total_score, self.fear, self.resources, self.orig_alive, self.orig_dead = self.env.render(mode='human')
self.current_events = self.return_events()
self.temp_data = {}
self.turn_description_info = []
self.turn_desc_log_index = -1
self.deployments_action = []
self.locations_action = []
#Constants
self.HEIGHT = 720
self.WIDTH = 1280
canvas = Canvas(self.root, height= self.HEIGHT, width= self.WIDTH)
canvas.pack()
self.create_screen()
def create_screen(self):
#Creates the background for the GUI
back_frame = Frame(self.root, bg = "#263D42", bd = 5) #hex teal blue
back_frame.place(relx = 0.5, rely= 0.01, relwidth= 1, relheight= 0.97, anchor = 'n')
TitleCard = Label(back_frame, text="Zombies or Flu", font=('Courier', 18))
TitleCard.place(relx=0.5, rely=0.001, relwidth=0.49, relheight=0.075, anchor='n') # header with game title
###Notification Bar###
self.NotifBar = Button(back_frame, font=('Courier', 9), command = lambda: self.open_log())
self.NotifBar.place(relx=0.5, rely=0.08, relwidth=0.49, relheight=0.025, anchor='n')
#Label for turn counter
Turn = Label(back_frame, text = ' Turn: {0} of {1}'.format(self.turn, self.max_turns) , font = ('Courier', 8))
Turn.place(relx = 0.75, rely = 0.001, relwidth = 0.1, relheight = 0.05)
#Label for total score
Score = Label(back_frame, text = 'Turn Score:{0}(Total Score: {1})'.format(self.score, self.total_score), font = ('Courier', 7), justify = 'left')
Score.place(relx = 0.853, rely = 0.001, relwidth = 0.135, relheight = 0.05)
#Label for fear counter
Fear = Label(back_frame, text = ' Fear: {}'.format(self.fear), font = ('Courier', 10))
Fear.place(relx =0.75, rely = 0.055, relwidth = 0.119, relheight = 0.05)
#Label for resources
Resource = Label(back_frame, text = ' Resource: {}'.format(self.resources), font = ('Courier', 10))
Resource.place(relx=0.872, rely=0.055, relwidth=0.117, relheight=0.05)
#Frame to hold the 25 Deployment buttons
DeployFrame = Label(back_frame, bg = 'gray', bd = 5)
DeployFrame.place(relwidth= 0.25, relheight= 0.99)
###The 25 Deployment Buttons ###
for i in range(25):
text = "{} {}".format(DEPLOYMENTS(i).name.ljust(38), i)
button = Button(DeployFrame, text = text, font = ('Courier', 8), command=lambda x=i:self.add_deployment(x), anchor = "w")
button.place(rely = i * 0.04, relwidth = 1)
self.create_neighborhoods()
def create_neighborhoods(self):
### The 9 Neighborhoods ###
#Include city stats
#Information is what's printed for each neighborhood
#It should be in the form of [statistic_name, statistc data for neighborhoodNW, N, NE, W, ...
self.information = [["Active"] + [self.env.city.show_data(nbh.local_fear, nbh.num_active) for nbh in self.neighborhoods],
["Sickly"] + [self.env.city.show_data(nbh.local_fear, nbh.num_sickly) for nbh in self.neighborhoods],
["Zombies"] + [self.env.city.show_data(nbh.local_fear, nbh.num_zombie) for nbh in self.neighborhoods],
["Dead"] + [self.env.city.show_data(nbh.local_fear, nbh.num_dead) for nbh in self.neighborhoods],
["Living at Start"] + [nbh.orig_alive for nbh in self.neighborhoods],
["Dead at Start"] + [nbh.orig_dead for nbh in self.neighborhoods],
["Local Fear"] + [nbh.local_fear for nbh in self.neighborhoods]]
def format_deployments(nbh_index):
deployments = [deployment.value for deployment in self.neighborhoods[nbh_index].deployments if deployment.value != 0]
max_per_line = 8
print(deployments)
if len(deployments) < max_per_line:
return 'Deployments: ' + str(deployments)
else:
num_split = len(deployments) // max_per_line
text = ''
if len(deployments) % max_per_line==0:
for i in range(num_split):
text += str(deployments[i*max_per_line:(i+1)*max_per_line]) + '\n'
else:
for i in range(num_split):
text += str(deployments[i*max_per_line:(i+1)*max_per_line]) + '\n'
text += str(deployments[num_split*max_per_line:])
return 'Deployments: ' + text
#Formats the information to display
formatted_information = []
for i in range(len(self.neighborhoods)):
text = ''
for elem in self.information:
text += '{}: {}'.format(elem[0], elem[i+1]) + '\n'
text += 'Events: {}\n'.format(self.current_events[LOCATIONS(i).name])
text += format_deployments(i)
formatted_information.append(text)
CityFrame = Frame(self.root, bg = 'gray', bd = 5)
CityFrame.place(relx = 0.255, rely = 0.13, relwidth = 0.725, relheight = 0.83) #Frame to hold all 9 City labels
NWest = Button(CityFrame, text = formatted_information[0], command=lambda x=LOCATIONS.NW.value:self.add_location(x), bd = 5)
NWest.place(relwidth = 0.33, relheight = 0.33)
North = Button(CityFrame, text = formatted_information[1], command=lambda x=LOCATIONS.N.value:self.add_location(x), bd = 5)
North.place(relx = 0.335, relwidth = 0.33, relheight = 0.33)
NEast = Button(CityFrame, text = formatted_information[2], command=lambda x=LOCATIONS.NE.value:self.add_location(x), bd = 5)
NEast.place(relx = 0.669, relwidth = 0.33, relheight = 0.33)
West = Button(CityFrame, text = formatted_information[3], command=lambda x=LOCATIONS.W.value:self.add_location(x), bd = 5)
West.place(rely = 0.335, relwidth = 0.33, relheight = 0.33)
Center = Button(CityFrame, text = formatted_information[4], command=lambda x=LOCATIONS.CENTER.value:self.add_location(x), bd = 5)
Center.place(relx = 0.335, rely = 0.335, relwidth = 0.33, relheight = 0.33)
East = Button(CityFrame, text = formatted_information[5], command=lambda x=LOCATIONS.E.value:self.add_location(x), bd = 5)
East.place(relx = 0.669, rely = 0.335, relwidth = 0.33, relheight = 0.33)
SWest = Button(CityFrame, text = formatted_information[6], command=lambda x=LOCATIONS.SW.value:self.add_location(x), bd = 5)
SWest.place(rely = 0.67, relwidth = 0.33, relheight = 0.33)
South = Button(CityFrame, text = formatted_information[7], command=lambda x=LOCATIONS.S.value:self.add_location(x), bd = 5)
South.place(relx = 0.335, rely = 0.67, relwidth = 0.33, relheight = 0.33)
SEast = Button(CityFrame, text = formatted_information[8], command=lambda x=LOCATIONS.SE.value:self.add_location(x), bd = 5)
SEast.place(relx = 0.669, rely = 0.67, relwidth = 0.33, relheight = 0.33)
def add_deployment(self, deployment):
if self.turn < self.max_turns:
if len(self.deployments_action) < 2:
#Adds deployments to deployments list cant exceed 2 actions per turn
num_d = len(self.deployments_action)
num_l = len(self.locations_action)
if num_d == 0:
self.deployments_action.append(deployment)
elif num_d == 1 and num_l < 2 :
self.deployments_action.append(deployment)
elif num_d == 1 and num_l == 2 :
self.deployments_action.append(deployment)
self._do_turn()
print(self.deployments_action)
self.NotifBar['text'] = "Deployment: " + DEPLOYMENTS(deployment).name
else:
self.NotifBar['text'] = "Please select a location."
def add_location(self, location):
if self.turn < self.max_turns:
if len(self.locations_action) < 2:
#Adds locations to locations list cant exceed 2 actions per turn
num_d = len(self.deployments_action)
num_l = len(self.locations_action)
if num_l == 0:
self.locations_action.append(location)
elif num_l == 1 and num_d < 2 :
self.locations_action.append(location)
elif num_l == 1 and num_d == 2 :
self.locations_action.append(location)
self._do_turn()
print(self.locations_action)
self.NotifBar['text'] = " Location: " + LOCATIONS(location).name
else:
self.NotifBar['text'] = "Please select a deployment."
def open_log(self):
#Creates the data logs
###The notif bar opens the data log
if self.turn > 0:
top = Toplevel()
top.title('Log')
data_log = self.get_turn_desc()
turn_num = Label(top, text = "End of Turn: {}".format(data_log[self.turn_desc_log_index]["Global"][0]), bd = 5)
turn_num.grid(row=0)
#Global stats
score_turn_desc = Label(top, text = 'Total Score: {} {}'.format(data_log[self.turn_desc_log_index]["Global"][1],
data_log[self.turn_desc_log_index]["delta_Global"][1] if data_log[self.turn_desc_log_index]["delta_Global"][1] < 0 else "+"
+ str(data_log[self.turn_desc_log_index]["delta_Global"][1])), bd = 5)
score_turn_desc.grid(row = 1, column = 0)
fear_turn_desc = Label(top, text = 'Fear: {} {}'.format(data_log[self.turn_desc_log_index]["Global"][2],
data_log[self.turn_desc_log_index]["delta_Global"][2] if data_log[self.turn_desc_log_index]["delta_Global"][2] < 0 else "+"
+ str(data_log[self.turn_desc_log_index]["delta_Global"][2])), bd = 5)
fear_turn_desc.grid(row = 1, column = 1)
resource_turn_desc = Label(top, text = 'Resources: {} {}'.format(data_log[self.turn_desc_log_index]["Global"][3],
data_log[self.turn_desc_log_index]["delta_Global"][3] if data_log[self.turn_desc_log_index]["delta_Global"][3] < 0 else "+"
+ str(data_log[self.turn_desc_log_index]["delta_Global"][3])), bd = 5)
resource_turn_desc.grid(row = 1, column = 2)
def format_for_grid(nbh_name):
var_names = ["Active","Sickly","Zombies","Dead","Living at Start","Dead at Start","Local Fear"]
text = ''
for i in range(len(data_log[self.turn_desc_log_index][nbh_name])):
delta = data_log[self.turn_desc_log_index]["delta_"+nbh_name][i] if data_log[self.turn_desc_log_index]["delta_"+nbh_name][i] <= 0 else "+" + str(data_log[self.turn_desc_log_index]["delta_"+nbh_name][i])
if delta == 0:
delta = ''
text += '{}: {}'.format(var_names[i], data_log[self.turn_desc_log_index][nbh_name][i]).ljust(30)
text += '{} \n'.format(delta)
return text
NWest_turn_desc = Label(top, text = format_for_grid(LOCATIONS.NW.name), bd = 5, justify = "left")
NWest_turn_desc.grid(row = 2, column = 0)
North_turn_desc = Label(top, text = format_for_grid(LOCATIONS.N.name), bd = 5, justify = "left")
North_turn_desc.grid(row = 2, column = 1)
NEast_turn_desc = Label(top, text = format_for_grid(LOCATIONS.NE.name), bd = 5, justify = "left")
NEast_turn_desc.grid(row = 2, column = 2)
West_turn_desc = Label(top, text = format_for_grid(LOCATIONS.W.name), bd = 5, justify = "left")
West_turn_desc.grid(row = 3, column = 0)
Center_turn_desc = Label(top, text = format_for_grid(LOCATIONS.CENTER.name), bd = 5, justify = "left")
Center_turn_desc.grid(row = 3, column = 1)
East_turn_desc = Label(top, text = format_for_grid(LOCATIONS.E.name), bd = 5, justify = "left")
East_turn_desc.grid(row = 3, column = 2)
SWest_turn_desc = Label(top, text = format_for_grid(LOCATIONS.SW.name), bd = 5, justify = "left")
SWest_turn_desc.grid(row = 4, column = 0)
South_turn_desc = Label(top, text = format_for_grid(LOCATIONS.S.name), bd = 5, justify = "left")
South_turn_desc.grid(row = 4, column = 1)
SEast_turn_desc = Label(top, text = format_for_grid(LOCATIONS.SE.name), bd = 5, justify = "left")
SEast_turn_desc.grid(row = 4, column = 2)
action_turn_desc_text = "You deployed {} in {} \n".format(DEPLOYMENTS(data_log[self.turn_desc_log_index]["actions"][0][0]).name,LOCATIONS(data_log[self.turn_desc_log_index]["actions"][1][0]).name)
action_turn_desc_text += "You then deployed {} in {}".format(DEPLOYMENTS(data_log[self.turn_desc_log_index]["actions"][0][1]).name,LOCATIONS(data_log[self.turn_desc_log_index]["actions"][1][1]).name)
action_turn_desc = Label(top, text = action_turn_desc_text, bd = 5)
action_turn_desc.grid(row = 5, column = 0, columnspan = 2)
def format_events(events):
text = ''
for items in events.items():
text += '{}\n'.format(items)
return text
events_turn_desc_text = "Events:\n{}".format(format_events(data_log[self.turn_desc_log_index]["events"]))
events_turn_desc = Label(top, text = events_turn_desc_text, bd = 5)
events_turn_desc.grid(row = 2, column = 4, sticky = N)
prev_button = Button(top, text = "Previous", command=lambda: self.prev_log(), bd = 5)
prev_button.grid(row = 6, column = 0)
next_button = Button(top, text = "Next", command=lambda:self.next_log(), bd = 5)
next_button.grid(row = 6, column = 1)
else:
self.NotifBar['text'] = "No Turn Description Available"
def prev_log(self):
#Button to iterate through past data logs
if len(self.turn_description_info)*-1 < self.turn_desc_log_index:
self.turn_desc_log_index -= 1
self.open_log()
def next_log(self):
current_index = self.turn_desc_log_index
if current_index < -1:
self.turn_desc_log_index += 1
self.open_log()
def _get_turn_desc_data(self):
##Gets data for the data log
#Capture Global Data
turn_desc_data = {}
turn_desc_data["Global"] = [self.turn, self.total_score,self.fear,self.resources]
#Capture Neighborhood Data
for i in range(len(self.neighborhoods)):
nbh = self.neighborhoods[i]
turn_desc_data[nbh.location.name] = [stat[i+1] for stat in self.information]
return turn_desc_data
def _create_turn_desc(self, prev_stats, curr_stats):
turn_desc_container = {}
#Calculates the changes and adds them to the dictionary along with the statistics for that turn
#To add: events
for k, v in prev_stats.items():
turn_desc_container["delta_"+k] = [curr_stats[k][i]-v[i] for i in range(len(v))]
turn_desc_container.update(prev_stats)
turn_desc_container.update({"actions" : [self.deployments_action]+[self.locations_action]})
turn_desc_container.update({"events" : self.current_events})
self.turn_description_info.append(turn_desc_container)
def get_turn_desc(self):
return self.turn_description_info
def _do_turn(self):
self.temp_data = self._get_turn_desc_data()
actions = self.env.encode_raw_action(location_1=LOCATIONS(self.locations_action[0]),
deployment_1=DEPLOYMENTS(self.deployments_action[0]),
location_2=LOCATIONS(self.locations_action[1]),
deployment_2=DEPLOYMENTS(self.deployments_action[1]))
observation, reward, done, info = self.env.step(actions)
# Write action and stuff out to disk.
data_to_log = {
'game_id': str(self.GAME_ID),
'step': self.turn,
'actions': actions,
'reward': reward,
'game_done': done,
'game_info': {k.replace('.', '_'): v for (k, v) in info.items()},
'raw_state': observation
}
with open(self.DATA_LOG_FILE_NAME, 'a') as f_:
f_.write(json.dumps(data_to_log) + '\n')
# Update counter
self.turn += 1
self.update_screen()
self._create_turn_desc(self.temp_data,self._get_turn_desc_data())
self.turn_desc_log_index = -1
self.deployments_action = []
self.locations_action = []
if done:
self.done()
def return_events(self):
self.env.city.update_event_states()
events_currently_going_on = {
"NW" : [],
"N" : [],
"NE" : [],
"W" : [],
"CENTER" : [],
"E" : [],
"SW" : [],
"S" : [],
"SE" : []
}
nbhs = list(events_currently_going_on.keys())
for i in range(len(self.neighborhoods)):
if self.neighborhoods[i].gathering_enabled:
events_currently_going_on[nbhs[i]].append("Gatherings")
if self.neighborhoods[i].panic_enabled:
events_currently_going_on[nbhs[i]].append("Panic")
if self.neighborhoods[i].swarm_enabled:
events_currently_going_on[nbhs[i]].append("Swarm")
return events_currently_going_on
def update_screen(self):
self.current_events = self.return_events()
self.neighborhoods, self.score, self.total_score, self.fear, self.resources, self.orig_alive, self.orig_dead = self.env.render(mode='human')
self.create_screen()
def summary_screen(self):
#self.root.winfo_children()[0].quit()
pass
def done(self):
print("Episode finished after {} turns".format(self.turn))
self.summary_screen()
self._cleanup()
def _cleanup(self):
self.env.close()