-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameBoard.py
More file actions
132 lines (113 loc) · 4.1 KB
/
GameBoard.py
File metadata and controls
132 lines (113 loc) · 4.1 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
import PacMan as P
import Ghost as G
import copy
class Dot(object):
# Constructor assigns location to dot, and boolean for if it is a Big Dot
def __init__(self, x, y, powerDot):
self.powerDot = powerDot
self.location = x, y
def location(self):
return self.location
def powerDot(self):
return self.powerDot
def __repr__(self):
string = "Dot location: " , self.location[0] , ", " , self.location[1]
return str(string)
class GameBoard(object):
#Constructor (Work in progress)
def __init__(self, startState):
self.board = copy.deepcopy(startState)
self.dots = self.setDots(startState)
self.ghostSpawnPt = self.findGhostSpawnPt(startState)
self.pacManSpawnPt = self.findPacManSpawnPt(startState)
self.height = len(startState)
self.length = len(startState[0])
self.dotsLeft = self.calculateDotsLeft(startState)
return
def __getitem__(self, item):
x = item[0]
y = item[1]
return self.board[x][y]
def findGhostSpawnPt(self, state):
for i in range(len(state)):
for j in range(len(state[i])):
if state[i][j] == 'G':
return i, j
# If it didn't find the location
print("Could not find ghost spawn point in calculateLocation")
return None
def findPacManSpawnPt(self, state):
spawnLoc = None
for i in range(len(state)):
for j in range(len(state[i])):
if state[i][j] == 'P':
spawnLoc = i, j
return spawnLoc
def setDots(self, state):
dots = []
for x, i in enumerate(state):
for y, j in enumerate(state[x]):
if j is '.':
dots.append(Dot(x, y, False))
if j is 'o':
dots.append(Dot(x, y, True))
return dots
def __str__(self):
value = ''
for i in range(len(self.board)):
for j in range(len(self.board[i])):
value += self.board[i][j]
value += '\n'
return value
# Calculates and returns the number of dots in a given state.
# Should only be called by constructor (dotsLeft updated in takeAction)
def calculateDotsLeft(self, state):
dots = 0
for i in range(len(state)):
for j in range(len(state[i])):
if state[i][j] == '.':
dots += 1
return dots
def reset(self, pacMan, ghosts):
for ghost in ghosts:
self.board[ghost.location[0]][ghost.location[1]] = ' '
self.board[self.ghostSpawnPt[0]][self.ghostSpawnPt[1]] = 'G'
self.board[pacMan.location[0]][pacMan.location[1]] = ' '
self.board[pacMan.respawn[0]][pacMan.respawn[1]] = 'p'
pacMan.location = pacMan.respawn
def removeDot(self, x, y):
for dot in self.dots:
if(dot.location[0] == x and dot.location[1] == y):
self.dots.remove(dot)
def getDots(self):
return self.dots
def move(self, target, x, y):
tarX = target.location[0]
tarY = target.location[1]
if isinstance(target, P.PacMan):
if self.board[x][y] is '.':
self.dotsLeft = self.dotsLeft - 1
self.removeDot(x, y)
self.board[x][y] = 'p'
self.board[tarX][tarY] = ' '
target.location = x,y
else:
self.board[x][y] = 'p'
self.board[tarX][tarY] = ' '
target.location = x,y
else:
if self.board[x][y] is '.':
self.board[x][y] = 'g'
if target.onDot:
self.board[tarX][tarY] = '.'
target.onDot = False
else: self.board[tarX][tarY] = ' '
target.onDot = True
target.location = x,y
else:
self.board[x][y] = 'g'
if target.onDot:
self.board[tarX][tarY] = '.'
target.onDot = False
else: self.board[tarX][tarY] = ' '
target.location = x,y