-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.py
More file actions
269 lines (234 loc) · 10.5 KB
/
layout.py
File metadata and controls
269 lines (234 loc) · 10.5 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
# layout.py
# ---------
# Licensing Information: You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).
import util
from util import manhattanDistance
from game import Grid
# from capsule import DefaultCapsule
import os
import random
import itertools
VISIBILITY_MATRIX_CACHE = {}
PROB_FOOD_LEFT = 0.4
PROB_LEFT_TOP = 0.6
PROB_OPPOSITE_CORNERS = 0.7
PROB_FOOD_RED = 0.3
PROB_GHOST_RED = 0.6
PROB_BOTH_TOP = PROB_LEFT_TOP * (1 - PROB_OPPOSITE_CORNERS)
PROB_BOTH_BOTTOM = (1 - PROB_LEFT_TOP) * (1 - PROB_OPPOSITE_CORNERS)
PROB_ONLY_LEFT_TOP = PROB_LEFT_TOP * PROB_OPPOSITE_CORNERS
PROB_ONLY_LEFT_BOTTOM = (1 - PROB_LEFT_TOP) * PROB_OPPOSITE_CORNERS
class Layout:
"""
A Layout manages the static information about the game board.
"""
def __init__(self, layoutText=None, seed=None, width=None, height=None, vpi=False):
if layoutText:
self.width = len(layoutText[0])
self.height= len(layoutText)
self.walls = Grid(self.width, self.height, False)
self.redWalls = Grid(self.width, self.height, False)
self.blueWalls = Grid(self.width, self.height, False)
self.food = Grid(self.width, self.height, False)
self.capsules = []
self.agentPositions = []
self.numGhosts = 0
self.processLayoutText(layoutText)
self.layoutText = layoutText
self.totalFood = len(self.food.asList())
elif vpi:
layoutText = generateVPIHuntersBoard(seed)
self.__init__(layoutText)
else:
layoutText = generateRandomHuntersBoard(seed, width, height)
self.__init__(layoutText)
def getNumGhosts(self):
return self.numGhosts
def initializeVisibilityMatrix(self):
global VISIBILITY_MATRIX_CACHE
if functools.reduce(str.__add__, self.layoutText) not in VISIBILITY_MATRIX_CACHE:
from game import Directions
vecs = [(-0.5,0), (0.5,0),(0,-0.5),(0,0.5)]
dirs = [Directions.NORTH, Directions.SOUTH, Directions.WEST, Directions.EAST]
vis = Grid(self.width, self.height, {Directions.NORTH:set(), Directions.SOUTH:set(), Directions.EAST:set(), Directions.WEST:set(), Directions.STOP:set()})
for x in range(self.width):
for y in range(self.height):
if self.walls[x][y] == False:
for vec, direction in zip(vecs, dirs):
dx, dy = vec
nextx, nexty = x + dx, y + dy
while (nextx + nexty) != int(nextx) + int(nexty) or not self.walls[int(nextx)][int(nexty)] :
vis[x][y][direction].add((nextx, nexty))
nextx, nexty = x + dx, y + dy
self.visibility = vis
VISIBILITY_MATRIX_CACHE[functools.reduce(str.__add__, self.layoutText)] = vis
else:
self.visibility = VISIBILITY_MATRIX_CACHE[functools.reduce(str.__add__, self.layoutText)]
def isWall(self, pos):
x, col = pos
return self.walls[x][col]
def getRandomLegalPosition(self):
x = random.choice(range(self.width))
y = random.choice(range(self.height))
while self.isWall( (x, y) ):
x = random.choice(range(self.width))
y = random.choice(range(self.height))
return (x,y)
def getRandomCorner(self):
poses = [(1,1), (1, self.height - 2), (self.width - 2, 1), (self.width - 2, self.height - 2)]
return random.choice(poses)
def getFurthestCorner(self, pacPos):
poses = [(1,1), (1, self.height - 2), (self.width - 2, 1), (self.width - 2, self.height - 2)]
dist, pos = max([(manhattanDistance(p, pacPos), p) for p in poses])
return pos
def isVisibleFrom(self, ghostPos, pacPos, pacDirection):
row, col = [int(x) for x in pacPos]
return ghostPos in self.visibility[row][col][pacDirection]
def __str__(self):
return "\n".join(self.layoutText)
def deepCopy(self):
return Layout(self.layoutText[:])
def processLayoutText(self, layoutText):
"""
Coordinates are flipped from the input format to the (x,y) convention here
The shape of the maze. Each character
represents a different type of object.
% - Wall
. - Food
o - Capsule
G - Ghost
P - Pacman
B - Blue Wall
R - Red Wall
Other characters are ignored.
"""
maxY = self.height - 1
for y in range(self.height):
for x in range(self.width):
layoutChar = layoutText[maxY - y][x]
self.processLayoutChar(x, y, layoutChar)
self.agentPositions.sort()
self.agentPositions = [ ( i == 0, pos) for i, pos in self.agentPositions]
def processLayoutChar(self, x, y, layoutChar):
if layoutChar == '%':
self.walls[x][y] = True
elif layoutChar == 'B':
self.blueWalls[x][y] = True
elif layoutChar == 'R':
self.redWalls[x][y] = True
elif layoutChar == '.':
self.food[x][y] = True
elif layoutChar == 'o':
self.capsules.append(DefaultCapsule(x, y))
elif layoutChar == 'P':
self.agentPositions.append( (0, (x, y) ) )
elif layoutChar in ['G']:
self.agentPositions.append( (1, (x, y) ) )
self.numGhosts += 1
elif layoutChar in ['1', '2', '3', '4']:
self.agentPositions.append( (int(layoutChar), (x,y)))
self.numGhosts += 1
def getLayout(name, back = 2):
if name.endswith('.lay'):
layout = tryToLoad('layouts/' + name)
if layout == None: layout = tryToLoad(name)
else:
layout = tryToLoad('layouts/' + name + '.lay')
if layout == None: layout = tryToLoad(name + '.lay')
if layout == None and back >= 0:
curdir = os.path.abspath('.')
os.chdir('..')
layout = getLayout(name, back -1)
os.chdir(curdir)
return layout
def tryToLoad(fullname):
if(not os.path.exists(fullname)): return None
f = open(fullname)
try: return Layout([line.strip() for line in f])
finally: f.close()
def generateVPIHuntersBoard(seed=None):
width = 11
height = 11
foodHouseLeft = util.flipCoin(PROB_FOOD_LEFT)
layoutTextGrid = [[' ' for _ in range(width)] for _ in range(height)]
layoutTextGrid[0] = ['%' for _ in range(width)]
layoutTextGrid[-1] = layoutTextGrid[0][:]
for i in range(height):
layoutTextGrid[i][0] = layoutTextGrid[i][-1] = '%'
possibleLocations = pickPossibleLocations(width, height)
# (foodX, foodY), (ghostX, ghostY) = tuple(random.sample(possibleLocations, 2))
bottomLeft, topLeft, bottomRight, topRight = tuple(possibleLocations)
foodX, foodY = topLeft
ghostX, ghostY = topRight
if not util.flipCoin(PROB_FOOD_LEFT):
(foodX, foodY), (ghostX, ghostY) = (ghostX, ghostY), (foodX, foodY)
layoutTextGrid[-foodY-1][foodX] = '.'
layoutTextGrid[-ghostY-1][ghostX] = 'G'
for foodWallX, foodWallY in buildHouseAroundCenter(foodX, foodY):
if util.flipCoin(PROB_FOOD_RED):
layoutTextGrid[-foodWallY-1][foodWallX] = 'R'
else:
layoutTextGrid[-foodWallY-1][foodWallX] = 'B'
for ghostWallX, ghostWallY in buildHouseAroundCenter(ghostX, ghostY):
if util.flipCoin(PROB_GHOST_RED):
layoutTextGrid[-ghostWallY-1][ghostWallX] = 'R'
else:
layoutTextGrid[-ghostWallY-1][ghostWallX] = 'B'
layoutTextGrid[5][5] = 'P'
layoutTextRowList = [''.join(row) for row in layoutTextGrid]
return layoutTextRowList
def generateRandomHuntersBoard(seed=None, width=None, height=None):
"""Note that this is constructing a string, so indexing is [-y-1][x] rather than [x][y]"""
random.seed(seed)
leftHouseTop = util.flipCoin(PROB_LEFT_TOP)
if not width or not height:
width = random.randrange(11, 20, 4)
height = random.randrange(11, 16, 4)
layoutTextGrid = [[' ' for _ in range(width)] for _ in range(height)]
layoutTextGrid[0] = ['%' for _ in range(width)]
layoutTextGrid[-1] = layoutTextGrid[0][:]
for i in range(height):
layoutTextGrid[i][0] = layoutTextGrid[i][-1] = '%'
possibleLocations = pickPossibleLocations(width, height)
# (foodX, foodY), (ghostX, ghostY) = tuple(random.sample(possibleLocations, 2))
bottomLeft, topLeft, bottomRight, topRight = tuple(possibleLocations)
if leftHouseTop:
foodX, foodY = topLeft
ghostX, ghostY = bottomRight if util.flipCoin(PROB_OPPOSITE_CORNERS) else topRight
else:
foodX, foodY = bottomLeft
ghostX, ghostY = topRight if util.flipCoin(PROB_OPPOSITE_CORNERS) else bottomRight
if not util.flipCoin(PROB_FOOD_LEFT):
(foodX, foodY), (ghostX, ghostY) = (ghostX, ghostY), (foodX, foodY)
layoutTextGrid[-foodY-1][foodX] = '.'
layoutTextGrid[-ghostY-1][ghostX] = 'G'
for foodWallX, foodWallY in buildHouseAroundCenter(foodX, foodY):
if util.flipCoin(PROB_FOOD_RED):
layoutTextGrid[-foodWallY-1][foodWallX] = 'R'
else:
layoutTextGrid[-foodWallY-1][foodWallX] = 'B'
for ghostWallX, ghostWallY in buildHouseAroundCenter(ghostX, ghostY):
if util.flipCoin(PROB_GHOST_RED):
layoutTextGrid[-ghostWallY-1][ghostWallX] = 'R'
else:
layoutTextGrid[-ghostWallY-1][ghostWallX] = 'B'
layoutTextGrid[-2][1] = 'P'
layoutTextRowList = [''.join(row) for row in layoutTextGrid]
return layoutTextRowList
def pickPossibleLocations(width, height):
# return list(itertools.product(range(3, width - 3, 4), range(3, height - 3, 4)))
return [(3, 3), (3, height - 4), (width - 4, 3), (width - 4, height - 4)]
def buildHouseAroundCenter(x, y):
return set(itertools.product([x-1, x, x+1], [y-1, y, y+1])) - {(x, y), (x, y-1)}
if __name__ == '__main__':
lay = Layout(generateVPIHuntersBoard())
print(lay)