-
Notifications
You must be signed in to change notification settings - Fork 0
/
whboard.py
205 lines (169 loc) · 5.02 KB
/
whboard.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
from whblock import Block
from whpoint import Point
from whhunter import Hunter
import random
##############################################
# The Board
# Contains the actual map("cave") data
# and the wumpus status
##############################################
class Board():
def __init__(self, n, surr):
self.surr = surr
# Last
self.restart(n)
def restart(self, n):
self.size = n
self.wp = 0.06
self.pp = 0.06
numsq = n.x*n.y
self.numpit = int(numsq * self.pp)
self.numwalls = int(numsq * self.wp)
self.createMap()
self.goldTaken = False
self.running = True
# Create map objects
def createMap(self):
self.map = []
for i in range (0, self.size.x):
new = []
for j in range (0, self.size.y):
new.append(Block("empty", i, j))
self.map.append(new)
self.initWalls()
self.initPitfalls()
self.hdir = Point(0,1)
self.hpos = self.getRandomAvailable(True)
self.home = self.hpos.clone()
self.clearHome()
self.placeOnce("wumpus")
self.placeOnce("gold")
# Create the wumpus hunter based on
# which tiles are free
def createHunter(self):
return Hunter(self.hpos.clone(), self.hdir.clone(), self.size, self.surr)
def clearHome(self):
t = self.home
self.map[t.x+1][t.y].t = "empty"
self.map[t.x-1][t.y].t = "empty"
self.map[t.x+1][t.y+1].t = "empty"
self.map[t.x+1][t.y-1].t = "empty"
self.map[t.x-1][t.y+1].t = "empty"
self.map[t.x-1][t.y-1].t = "empty"
self.map[t.x][t.y+1].t = "empty"
self.map[t.x][t.y-1].t = "empty"
self.map[t.x][t.y].t = "empty"
def drawHunter(self):
self.drawTile(self.hpos.x, self.hpos.y, (200, 0, 0))
def drawHome(self):
self.drawTile(self.home.x, self.home.y, (200, 0, 200))
# Draw a tile
# x, y = position
# c = color
def drawTile(self, x, y, c):
pygame.draw.rect(window, c, (x*block, y*block, block, block))
# Create walls
# Insert randomly placed walls
def initWalls(self):
for i in range(self.size.x):
self.map[0][i] = Block("wall", 0, i)
self.map[self.size.x-1][i] = Block("wall", self.size.x-1, i)
for i in range(self.size.y):
self.map[i][0] = Block("wall", i, 0)
self.map[i][self.size.y-1] = Block("wall", i, self.size.y-1)
for i in range(self.numwalls):
self.setTile(random.randrange(0, self.size.x), random.randrange(0, self.size.y), "wall")
# Randomly place pitfalls on the map
def initPitfalls(self):
for i in range(self.numpit):
self.setTile(random.randrange(0, self.size.x), random.randrange(0, self.size.y), "pit")
# Set the type of a tile
# x, y = position
# t = type
# override = override tile if not empty
def setTile(self, x, y, t, override = False):
if override:
self.map[x][y].t = t
return True
elif self.map[x][y].t =="empty":
self.map[x][y].t = t
return True
return False
# Place a tile on first available spot
def placeOnce(self, t):
while True:
if self.setTile(random.randrange(1, self.size.x), random.randrange(1,self.size.y), t):
break
# Return a list of perceptions available to the hunter
# If the hunter bumps, it shouldn't get perception from the wall's tile
def percept(self):
tile = self.map[self.hpos.x][self.hpos.y]
percepts=[]
if tile.t == "wall":
percepts.append("bump")
return percepts
if tile.t == "gold":
percepts.append("gold")
self.goldTaken = True
for x in self.surr:
tile = self.map[self.hpos.x + x.x][self.hpos.y + x.y]
if tile.t == "emtpy" or tile.t == "gold" or tile.t == "wall":
continue
if tile.t == "wumpus":
if "smell" in percepts:
continue
percepts.append("smell")
elif tile.t == "pit":
if "pit" in percepts:
continue
percepts.append("breeze")
return percepts
# Get perception from a tile
def getPercept(self, x, y):
tile = self.map[x][y]
if tile.t != "empty" and tile.t != "gold" and tile.t != "wall":
return tile.t
return None
def getRandomAvailable(self, safety=False):
while True:
x = random.randrange(1, self.size.x)
y = random.randrange(1, self.size.y)
tile = self.map[x][y]
if tile.t == "empty":
if safety:
safe = True
for i in self.surr:
tr = self.getPercept(tile.p.x+i.x, tile.p.y+i.y)
if tr != None:
safe = False
if safe:
return Point(x, y)
else:
return Point(x, y)
def updateHunter(self, action):
cmd = action.split(" ")
if cmd[0] == "turn":
if cmd[1] == "left":
self.hdir.turnLeft()
elif cmd[1] == "right":
self.hdir.turnRight()
elif cmd[0] == "forward":
self.hpos.move(self.hdir)
elif cmd[0] == "back":
self.hpos.back(self.hdir)
elif cmd[0] == "grab":
if self.map[self.hpos.x][self.hpos.y].t == "gold":
self.goldTaken = True
self.map[self.hpos.x][self.hpos.y].t = "empty"
elif cmd[0] == "climb":
if self.hpos.x == self.home.x and self.hpos.y == self.hpos.y:
self.running = False
self.updateState()
def updateState(self):
tile = self.map[self.hpos.x][self.hpos.y]
if tile.t == "wumpus":
self.gameOver()
elif tile.t == "pit":
self.gameOver()
def gameOver(self):
self.running = False