forked from sunsky426/SGAI-DRHAT-Outbreak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
321 lines (284 loc) · 13 KB
/
main.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
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
import pygame
from Board import Board
import PygameFunctions as PF
import random as rd
from constants import *
import time
# Player role variables
player_role = Role.government # Valid options are Role.government and Role.zombie
roleToRoleNum = {Role.government: 1, Role.zombie: -1}
# Create the game board
GameBoard = Board((ROWS, COLUMNS), BORDER, CELL_DIMENSIONS, player_role)
GameBoard.populate()
# Self play variables
alpha = 0.1
gamma = 0.6
epsilon = 0.1
epochs = 1000
epochs_ran = 0
Original_Board = GameBoard.clone(GameBoard.States, GameBoard.player_role)
# Initialize variables
running = True
take_action = []
playerMoved = False
font = pygame.font.SysFont("Comic Sans", 20)
start = False
#Initial player score
player_score = 0
Data = [[[], [], [], [], [], []]] #[[kills], [heals], [meds], [zombies/population], [anxiety], [outrage]]
Turn = 0
while running:
#displays the main menu until user hits start or quit
if start == False:
try:
start = PF.disp_title_screen()
#Throws exception when user quits program using in-game button
except pygame.error:
print("Data: ", Data)
print("Game closed by user")
break
elif start == True:
P = PF.run(GameBoard)
if SELF_PLAY:
if( #if the game is over
not GameBoard.containsPerson(bool(player_role.value))
or GameBoard.outrage >= 100
):
running = PF.display_lose_screen() #display the lose screen showing "play again" or "quit", if "play again" :
for state in GameBoard.States: #reset the board
state.person = None
state.safeSpace = False
GameBoard.populate()
start = False
Data.append([[], [], [], [], [], []])
Turn = 0
continue
# Event Handling
for event in P:
if event.type == pygame.MOUSEBUTTONUP:
x, y = pygame.mouse.get_pos()
action = PF.get_action(GameBoard, x, y)
print(action)
if(action == "Distrb Med"):
take_action.append("Distrb Med")
Data[-1][2].append(Turn)
time.sleep(0.1)
GameBoard.med()
take_action = []
elif(
type(action) == Action
and len(take_action) == 0
):
# only allow healing by itself (prevents things like ['move', (4, 1), 'heal'])
take_action.append(action)
elif action == "reset move":
take_action = []
elif type(action) is tuple:
idx = GameBoard.toIndex(action)
# action is a coordinate
if idx < (GameBoard.rows * GameBoard.columns) and idx > -1:
if Action.move not in take_action and len(take_action) == 0:
# make sure that the space is not an empty space or a space of the opposite team
# since cannot start a move from those invalid spaces
if (
GameBoard.States[idx].person is not None
and GameBoard.States[idx].person.isZombie
== bool(player_role.value)
):
take_action.append(Action.move)
else:
continue
take_action.append(action)
if event.type == pygame.QUIT:
running = False
# Display the current action
PF.screen.blit(
font.render("Your move is currently:", True, PF.WHITE),
(800, 400),
)
PF.screen.blit(font.render(f"{take_action}", True, PF.WHITE), (800, 450))
# Action handling
if len(take_action) > 2:
#slightly increases anxiety every turn
GameBoard.anxiety += 1
directionToMove = PF.direction(take_action[1], take_action[2])
print("Implementing", take_action[0], "to", directionToMove)
result = GameBoard.act[take_action[0]](take_action[1], directionToMove)
print(f"did it succeed? {result}")
if result == Result.success:
player_score += PF.get_reward(take_action[0])
#if it succeeds, the player gets a reward corresponding to their action
if take_action[0] == Action.kill: #add to data
Data[-1][0].append(Turn)
elif take_action[0] == Action.heal:
Data[-1][1].append(Turn)
Data[-1][3].append(round(GameBoard.num_zombies()/GameBoard.population, 2))
Data[-1][4].append(GameBoard.anxiety)
Data[-1][5].append(GameBoard.outrage)
if result != Result.invalid:
playerMoved = True
take_action = []
#Display the player's current score
PF.screen.blit(font.render("Score: " + str(player_score), True, PF.WHITE),(900,500))
if playerMoved:
Turn+=1
# Intermission
PF.run(GameBoard)
pygame.display.update()
time.sleep(0.1)
print("Enemy turn")
# Computer turn
playerMoved = False
take_action = []
#if player_role == Role.government:
# possible_actions = [Action.move, Action.bite]
# computer_role = Role.zombie
#else:
## possible_actions = [Action.move, Action.heal, Action.kill]
# computer_role = Role.government
computer_role = Role.zombie
possible_act_coords = []
#check if possible to bite
action = Action.bite
possible_direction = [member for name, member in Direction.__members__.items()]
while len(possible_act_coords) == 0 and len(possible_direction) != 0:
direction = rd.choice(possible_direction)
possible_direction.remove(direction)
possible_act_coords = GameBoard.get_possible_moves(
action, direction, computer_role
)
if len(possible_act_coords) == 0:
#try moving a zombie close enough to player
action = Action.move
human_coords = []
zombie_coords = []
best_dist = float("inf")
for idx in range(len(GameBoard.States)):
state = GameBoard.States[idx]
if state.person is not None:
if state.person.isZombie == True:
zombie_coords.append(GameBoard.toCoord(idx))
else:
human_coords.append(GameBoard.toCoord(idx))
for zombie in zombie_coords:
for victim in human_coords:
dist = (zombie[0] - victim[0])**2 + (zombie[1] - victim[1])**2
if dist < best_dist:
dir = PF.direction(zombie, victim)
B = GameBoard.clone(GameBoard.States, computer_role)
bruh = B.act[Action.move](zombie, dir)
if bruh != Result.invalid:
print(bruh)
direction = dir
best_dist = dist
possible_act_coords = []
possible_act_coords.append(zombie)
print("WOW", zombie, victim, dir)
if len(possible_act_coords) == 0:
#just make a random move
possible_direction = [member for name, member in Direction.__members__.items()]
action = Action.move
#cycles through directions
while len(possible_act_coords) == 0 and len(possible_direction) != 0:
direction = rd.choice(possible_direction)
possible_direction.remove(direction)
possible_act_coords = GameBoard.get_possible_moves(
action, direction, computer_role
)
# no valid moves, player wins
#Displays two buttons and allows the player to play again on a new randomized map
if (
len(possible_direction) == 0
and len(possible_act_coords) == 0
):
running = PF.display_win_screen()
for state in GameBoard.States:
state.person = None
state.safeSpace = False
GameBoard.populate()
start = False
continue
# Select the destination coordinates
move_coord = rd.choice(possible_act_coords)
# Implement the selected action
print(action, direction, move_coord)
print(GameBoard.act[action](move_coord, direction))
print("stopping")
# Update the display
pygame.display.update()
pygame.time.wait(75)
else:
if epochs_ran % 100 == 0:
print("Board Reset!")
GameBoard = Original_Board # reset environment
for event in P:
i = 0
r = rd.uniform(0.0, 1.0)
st = rd.randint(0, len(GameBoard.States) - 1)
state = GameBoard.QTable[st]
if r < gamma:
while GameBoard.States[st].person is None:
st = rd.randint(0, len(GameBoard.States) - 1)
else:
biggest = None
for x in range(len(GameBoard.States)):
arr = GameBoard.QTable[x]
exp = sum(arr) / len(arr)
if biggest is None:
biggest = exp
i = x
elif biggest < exp and player_role == Role.government:
biggest = exp
i = x
elif biggest > exp and player_role != Role.government:
biggest = exp
i = x
state = GameBoard.QTable[i]
b = 0
j = 0
ind = 0
for v in state:
if v > b and player_role == Role.government:
b = v
ind = j
elif v < b and player_role != Role.government:
b = v
ind = j
j += 1
action_to_take = ACTION_SPACE[ind]
old_qval = b
old_state = i
# Update
# Q(S, A) = Q(S, A) + alpha[R + gamma * max_a Q(S', A) - Q(S, A)]
reward = GameBoard.act(old_state, action_to_take)
ns = reward[1]
NewStateAct = GameBoard.QGreedyat(ns)
NS = GameBoard.QTable[ns][NewStateAct[0]]
GameBoard.QTable[i] = GameBoard.QTable[i] + alpha * (reward[0] + gamma * NS) - GameBoard.QTable[i]
if GameBoard.num_zombies() == 0:
print("winCase")
take_action = []
print("Enemy turn")
ta = ""
if player_role == Role.government:
r = rd.randint(0, 5)
while r == 4:
r = rd.randint(0, 5)
while r == 4:
r = rd.randint(0, 5)
ta = ACTION_SPACE[r]
else:
r = rd.randint(0, 4)
ta = ACTION_SPACE[r]
poss = GameBoard.get_possible_moves(ta, Role.zombie)
if len(poss) > 0:
r = rd.randint(0, len(poss) - 1)
a = poss[r]
GameBoard.act[ta](a)
if GameBoard.num_zombies() == GameBoard.population:
print("loseCase")
if event.type == pygame.QUIT:
running = False
Data = Data[0:-1]
print("Data: ", Data)
pygame.display.quit()