-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridWorld.py
179 lines (142 loc) · 5.81 KB
/
GridWorld.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
# Grid World game
# Import libraries used for this program
import pygame
import numpy as np
#%%
class GridWorld():
# Rendering?
rendering = False
# Images
filenames = ['person.png', 'key.png', 'door.png', 'death.png']
images = [pygame.image.load(file) for file in filenames]
# Colors
goodColor = (30, 192, 30)
badColor = (192, 30, 30)
pathColor = (225, 220, 225)
wallColor = (157, 143, 130)
def __init__(self, state=None):
pygame.init()
self.reward = 0
if state is None:
self.x, self.y, self.has_key, self.board, self.score = self.new_game()
else:
x, y, has_key, board, score = state
self.x, self.y, self.has_key, self.board, self.score = x, y, has_key, board.copy(), score
def get_state(self):
return (self.x, self.y, self.has_key)
def step(self, action):
# Move character
if not self.game_over(self.x, self.y, self.has_key, self.board):
self.x, self.y, self.has_key, self.board, self.score, self.reward = self.move(self.x, self.y, self.has_key, self.board, self.score, action)
# return observation, reward, done
done = self.game_over(self.x, self.y, self.has_key, self.board)
return ((self.x, self.y, self.has_key), self.reward, done)
def render(self):
if not self.rendering:
self.init_render()
# Clear the screen
self.screen.fill((187,173,160))
border = 3
pygame.draw.rect(self.screen, (187,173,160), pygame.Rect(100,0,600,600))
for i in range(10):
for j in range(10):
val = self.board[i,j]
col = self.wallColor if val & 8 else self.pathColor
pygame.draw.rect(self.screen, col, pygame.Rect(100+60*i+border,60*j+border,60-2*border,60-2*border))
if val>0:
x = 105 + 60*i
y = 5 + 60*j
if val & 4:
self.screen.blit(self.images[2], (x, y))
if val & 2:
self.screen.blit(self.images[1], (x, y))
if val & 1:
if self.game_over(self.x, self.y, self.has_key, self.board) and not self.won(self.x, self.y, self.has_key, self.board):
self.screen.blit(self.images[3], (x, y))
else:
self.screen.blit(self.images[0], (x, y))
text = self.scorefont.render("{:}".format(self.score), True, (0,0,0))
self.screen.blit(text, (790-text.get_width(), 10))
# Draw game over or you won
if self.game_over(self.x, self.y, self.has_key, self.board):
if self.won(self.x, self.y, self.has_key, self.board):
msg = 'Congratulations!'
col = self.goodColor
else:
msg = 'Game over!'
col = self.badColor
text = self.bigfont.render(msg, True, col)
textpos = text.get_rect(centerx=self.background.get_width()/2)
textpos.top = 300
self.screen.blit(text, textpos)
# Display
pygame.display.flip()
def reset(self):
self.x, self.y, self.has_key, self.board, self.score = self.new_game()
def close(self):
pygame.quit()
def init_render(self):
self.screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption('Grid World')
self.background = pygame.Surface(self.screen.get_size())
self.rendering = True
self.clock = pygame.time.Clock()
# Set up game
self.bigfont = pygame.font.Font(None, 80)
self.scorefont = pygame.font.Font(None, 30)
def game_over(self, x, y, has_key, board):
# Are we on a death square?
if board[x,y] & 8:
return True
# Are we on the door with the key?
if board[x,y] & 4 and not np.any(board & 2):
return True
return False
def won(self, x, y, has_key, board):
# Are we on the door with the key?
if board[x,y] & 4 and not np.any(board & 2):
return True
return False
def move(self, x, y, has_key, board, score, direction='left'):
newx, newy = x, y
if direction=='left':
if x>0:
newx = x-1
elif direction=='right':
if x<9:
newx = x+1
elif direction=='up':
if y>0:
newy = y-1
elif direction=='down':
if y<9:
newy = y+1
reward = -1
# Update position
board[x,y] -= 1
board[newx, newy] += 1
self.x, self.y = newx, newy
# Take key
if board[newx, newy] & 2:
board[newx, newy] -= 2
reward = 50
has_key = True
# On door with key?
if board[newx, newy] & 4 and not np.any(board & 2):
reward = 100
# On death?
if board[newx, newy] & 8:
reward = -100
score += reward
return (newx, newy, has_key, board, score, reward)
def new_game(self):
board = np.loadtxt('board.txt', dtype=int).T
if board.shape != (10,10) or np.sum(board==2) != 1 or np.sum(board==4) != 1:
raise Exception('board.txt corrupt')
start_x, start_y = np.where(board == 0)
i = np.random.randint(len(start_x))
x, y = start_x[i], start_y[i]
board[x, y] = 1
score = 0
has_key = False
return (x, y, has_key, board, score)