-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathWorld.py
More file actions
173 lines (142 loc) · 5.54 KB
/
World.py
File metadata and controls
173 lines (142 loc) · 5.54 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
__author__ = 'philippe'
import random
from Tkinter import *
master = Tk()
# probability to run the selected action
prob_move = 1.0
triangle_size = 0.1
cell_score_min = -0.2
cell_score_max = 0.2
Width = 100
(x, y) = (5, 5)
actions = ["up", "down", "left", "right"]
board = Canvas(master, width=x*Width, height=y*Width)
player = (0, y-1)
score = 1
restart = False
walk_reward = -0.04
walls = [(1, 1), (1, 2), (2, 1), (2, 2)]
specials = [(4, 1, "red", -1), (4, 0, "green", 1)]
cell_scores = {}
cell_action_scores = {}
def create_triangle(i, j, action):
if action == actions[0]:
return board.create_polygon((i+0.5-triangle_size)*Width, (j+triangle_size)*Width,
(i+0.5+triangle_size)*Width, (j+triangle_size)*Width,
(i+0.5)*Width, j*Width,
fill="white", width=1)
elif action == actions[1]:
return board.create_polygon((i+0.5-triangle_size)*Width, (j+1-triangle_size)*Width,
(i+0.5+triangle_size)*Width, (j+1-triangle_size)*Width,
(i+0.5)*Width, (j+1)*Width,
fill="white", width=1)
elif action == actions[2]:
return board.create_polygon((i+triangle_size)*Width, (j+0.5-triangle_size)*Width,
(i+triangle_size)*Width, (j+0.5+triangle_size)*Width,
i*Width, (j+0.5)*Width,
fill="white", width=1)
elif action == actions[3]:
return board.create_polygon((i+1-triangle_size)*Width, (j+0.5-triangle_size)*Width,
(i+1-triangle_size)*Width, (j+0.5+triangle_size)*Width,
(i+1)*Width, (j+0.5)*Width,
fill="white", width=1)
def render_grid():
global specials, walls, Width, x, y, player
# For each cell
for i in range(x):
for j in range(y):
# Create a square (white by default)
cell = board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill="white", width=1)
cell_scores[(i,j)] = cell
temp = {}
# For each action, create a little triangle
for action in actions:
temp[action] = create_triangle(i, j, action)
cell_action_scores[(i,j)] = temp
# Create special cells
for (i, j, c, w) in specials:
board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill=c, width=1)
# Create walls
for (i, j) in walls:
board.create_rectangle(i*Width, j*Width, (i+1)*Width, (j+1)*Width, fill="black", width=1)
render_grid()
# Update cell color to reflect V values
def set_cell_score(state, val):
global cell_score_min, cell_score_max
cell = cell_scores[state]
green_dec = int(min(255, max(0, (val - cell_score_min) * 255.0 / (cell_score_max - cell_score_min))))
green = hex(green_dec)[2:]
red = hex(255-green_dec)[2:]
if len(red) == 1:
red += "0"
if len(green) == 1:
green += "0"
color = "#" + red + green + "00"
board.itemconfigure(cell, fill=color)
# Update triangle colors to reflect Q values
def set_cell_action_score(state, action, val):
global cell_score_min, cell_score_max
triangle = cell_action_scores[state][action]
green_dec = int(min(255, max(0, (val - cell_score_min) * 255.0 / (cell_score_max - cell_score_min))))
green = hex(green_dec)[2:]
red = hex(255-green_dec)[2:]
if len(red) == 1:
red += "0"
if len(green) == 1:
green += "0"
color = "#" + red + green + "00"
board.itemconfigure(triangle, fill=color)
def try_move(dx, dy):
global player, x, y, score, walk_reward, me, restart
if restart == True:
restart_game()
# Add a bit of randomness
if random.random() > prob_move:
if dx == 0:
dx = -1 if random.random() > 0.5 else 1
dy = 0
else:
dx = 0
dy = -1 if random.random() > 0.5 else 1
new_x = player[0] + dx
new_y = player[1] + dy
score += walk_reward
if (new_x >= 0) and (new_x < x) and (new_y >= 0) and (new_y < y) and not ((new_x, new_y) in walls):
board.coords(me, new_x*Width+Width*2/10, new_y*Width+Width*2/10, new_x*Width+Width*8/10, new_y*Width+Width*8/10)
player = (new_x, new_y)
for (i, j, c, w) in specials:
if new_x == i and new_y == j:
score -= walk_reward
score += w
if score > 0:
print "Success! score: ", score
else:
print "Fail! score: ", score
restart = True
return
#print "score: ", score
def call_up(event):
try_move(0, -1)
def call_down(event):
try_move(0, 1)
def call_left(event):
try_move(-1, 0)
def call_right(event):
try_move(1, 0)
def restart_game():
global player, score, me, restart
player = (0, y-1)
score = 1
restart = False
board.coords(me, player[0]*Width+Width*2/10, player[1]*Width+Width*2/10, player[0]*Width+Width*8/10, player[1]*Width+Width*8/10)
def has_restarted():
return restart
master.bind("<Up>", call_up)
master.bind("<Down>", call_down)
master.bind("<Right>", call_right)
master.bind("<Left>", call_left)
me = board.create_rectangle(player[0]*Width+Width*2/10, player[1]*Width+Width*2/10,
player[0]*Width+Width*8/10, player[1]*Width+Width*8/10, fill="orange", width=1, tag="me")
board.grid(row=0, column=0)
def start_game():
master.mainloop()