getting right starter files

This commit is contained in:
Arjun Patel
2019-02-20 15:24:24 -08:00
parent 48d11417be
commit ade293719f
358 changed files with 8770 additions and 4878 deletions
+28 -17
View File
@@ -4,7 +4,7 @@
# 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
# ([email protected]) and Dan Klein ([email protected]).
@@ -16,27 +16,28 @@ from game import Agent
from game import Directions
import random
class KeyboardAgent(Agent):
"""
An agent controlled by the keyboard.
"""
# NOTE: Arrow keys also work.
WEST_KEY = 'a'
EAST_KEY = 'd'
WEST_KEY = 'a'
EAST_KEY = 'd'
NORTH_KEY = 'w'
SOUTH_KEY = 's'
STOP_KEY = 'q'
def __init__( self, index = 0 ):
def __init__(self, index=0):
self.lastMove = Directions.STOP
self.index = index
self.keys = []
def getAction( self, state):
def getAction(self, state):
from graphicsUtils import keys_waiting
from graphicsUtils import keys_pressed
keys = list(keys_waiting()) + list(keys_pressed())
keys = keys_waiting() + keys_pressed()
if keys != []:
self.keys = keys
@@ -48,7 +49,8 @@ class KeyboardAgent(Agent):
if self.lastMove in legal:
move = self.lastMove
if (self.STOP_KEY in self.keys) and Directions.STOP in legal: move = Directions.STOP
if (self.STOP_KEY in self.keys) and Directions.STOP in legal:
move = Directions.STOP
if move not in legal:
move = random.choice(legal)
@@ -58,27 +60,36 @@ class KeyboardAgent(Agent):
def getMove(self, legal):
move = Directions.STOP
if (self.WEST_KEY in self.keys or 'Left' in self.keys) and Directions.WEST in legal: move = Directions.WEST
if (self.EAST_KEY in self.keys or 'Right' in self.keys) and Directions.EAST in legal: move = Directions.EAST
if (self.NORTH_KEY in self.keys or 'Up' in self.keys) and Directions.NORTH in legal: move = Directions.NORTH
if (self.SOUTH_KEY in self.keys or 'Down' in self.keys) and Directions.SOUTH in legal: move = Directions.SOUTH
if (self.WEST_KEY in self.keys or 'Left' in self.keys) and Directions.WEST in legal:
move = Directions.WEST
if (self.EAST_KEY in self.keys or 'Right' in self.keys) and Directions.EAST in legal:
move = Directions.EAST
if (self.NORTH_KEY in self.keys or 'Up' in self.keys) and Directions.NORTH in legal:
move = Directions.NORTH
if (self.SOUTH_KEY in self.keys or 'Down' in self.keys) and Directions.SOUTH in legal:
move = Directions.SOUTH
return move
class KeyboardAgent2(KeyboardAgent):
"""
A second agent controlled by the keyboard.
"""
# NOTE: Arrow keys also work.
WEST_KEY = 'j'
EAST_KEY = "l"
WEST_KEY = 'j'
EAST_KEY = "l"
NORTH_KEY = 'i'
SOUTH_KEY = 'k'
STOP_KEY = 'u'
def getMove(self, legal):
move = Directions.STOP
if (self.WEST_KEY in self.keys) and Directions.WEST in legal: move = Directions.WEST
if (self.EAST_KEY in self.keys) and Directions.EAST in legal: move = Directions.EAST
if (self.NORTH_KEY in self.keys) and Directions.NORTH in legal: move = Directions.NORTH
if (self.SOUTH_KEY in self.keys) and Directions.SOUTH in legal: move = Directions.SOUTH
if (self.WEST_KEY in self.keys) and Directions.WEST in legal:
move = Directions.WEST
if (self.EAST_KEY in self.keys) and Directions.EAST in legal:
move = Directions.EAST
if (self.NORTH_KEY in self.keys) and Directions.NORTH in legal:
move = Directions.NORTH
if (self.SOUTH_KEY in self.keys) and Directions.SOUTH in legal:
move = Directions.SOUTH
return move