-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathghostbustersGameSimple.py
162 lines (135 loc) · 5.42 KB
/
ghostbustersGameSimple.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
# ghostbustersGameSimple.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 sys
import layout
import logicAgents
import hybridAgents
import warnings
from game import Actions
from game import Directions
class GhostbustersGameSimple:
def __init__(self, layoutName):
board = layout.getLayout(layoutName)
self.walls = board.walls
self.boardText = board.layoutText
for i in xrange(len(self.boardText)):
self.boardText[i] = list(self.boardText[i])
self.goal = None
for x in xrange(board.food.width):
for y in xrange(board.food.height):
if board.food[x][y]:
self.goal = (x,y)
self.currentState = board.agentPositions[0][1]
self._agentAlive = True
self.ghosts = []
for agents in board.agentPositions[1:]:
self.ghosts.append(agents[1])
def getStartState(self):
return self.currentState
def getActions(self, state):
"""
Given a state, returns available actions.
Returns a list of actions
"""
actions = []
for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:
x,y = state
dx, dy = Actions.directionToVector(action)
nextx, nexty = int(x + dx), int(y + dy)
if not self.walls[nextx][nexty]:
actions.append(action)
return actions
def getResult(self, state, action):
"""
Given a state and an action, returns resulting state and a cost of 1, which is
the incremental cost of expanding to that successor.
Returns (next_state, cost)
"""
x,y = state
dx, dy = Actions.directionToVector(action)
nextx, nexty = int(x + dx), int(y + dy)
if (not self.walls[nextx][nexty]) and (not self.walls[nextx][nexty]):
nextState = (nextx, nexty)
cost = 1
return (nextState, cost)
else:
warnings.warn("Warning: checking the result of an invalid state, action pair.")
return (state,0)
def goalTest(self, state):
goal = (state == self.goal)
if not goal:
for ghost in self.ghosts:
if state == ghost:
goal = True
break
return goal
def currentPercept(self):
# PKE meter is True if ghost is next door
pkeMeter = False
(x,y) = self.currentState
if (x-1,y) in self.ghosts:
pkeMeter = True
elif (x+1,y) in self.ghosts:
pkeMeter = True
elif (x,y-1) in self.ghosts:
pkeMeter = True
elif (x,y+1) in self.ghosts:
pkeMeter = True
percept = {'pacman':self.currentState, 'pkeReading': pkeMeter, 'walls':self.walls}
return percept
def _updateBoardText(self):
height = len(self.boardText) # Board y coordinate is inverted
# Clear old pacman and ghosts
for i in xrange(len(self.boardText)):
row = self.boardText[i]
for j in xrange(len(row)):
if row[j] == 'P' or row[j] == 'G':
self.boardText[i][j] = ' '
# Set current ghosts
for ghost in self.ghosts:
self.boardText[height-1-ghost[1]][ghost[0]] = 'G'
# Set current pacman
if self.boardText[height-1-self.currentState[1]][self.currentState[0]] == 'G':
self.boardText[height-1-self.currentState[1]][self.currentState[0]] = 'X'
else:
self.boardText[height-1-self.currentState[1]][self.currentState[0]] = 'P'
def printBoard(self):
for rowText in self.boardText:
print (''.join(rowText))
def run(self, agent):
print ("Initial board")
self.printBoard()
while self._agentAlive:
if self.goalTest(self.currentState):
"Pacman wins!"
break;
percept = self.currentPercept()
print ("percept={}".format(percept))
action = agent.getAction(percept)
if action == None:
warnings.warn("Warning invalid action returned by agent: "+str(action))
break
(nextState, _) = self.getResult(self.currentState, action)
self.currentState = nextState
self._updateBoardText()
print ()
print (action)
self.printBoard()
if __name__ == '__main__':
if len(sys.argv) >= 2:
layoutName = sys.argv[1]
else:
layoutName = "line"
game = GhostbustersGameSimple(layoutName)
agent = hybridAgents.HybridAgent(game)
game.run(agent)