-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyBot.py
86 lines (70 loc) · 2.98 KB
/
MyBot.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
# MyBot.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 random
from pacman.game import Directions
import pacman.util as util # Free utility functions like Stack or Queue !
from pacman.capture import GameState
from pacman.captureAgents import CaptureAgent
#################
# Team creation #
#################
def createTeam(firstIndex, secondIndex, isRed):
"""
This function should return a list of two agents that will form the
team, initialized using firstIndex and secondIndex as their agent
index numbers. isRed is True if the red team is being created, and
will be False if the blue team is being created.
"""
# The following line is an example only; feel free to change it.
return [AgentOne(firstIndex), AgentTwo(secondIndex)]
##########
# Agents #
##########
class AgentOne(CaptureAgent):
"""
A Dummy agent to serve as an example of the necessary agent structure.
You should look at baselineTeam.py for more details about how to
create an agent as this is the bare minimum.
"""
def registerInitialState(self, gameState: GameState):
"""
This method handles the initial setup of the
agent to populate useful fields (such as what team
we're on).
A distanceCalculator instance caches the maze distances
between each pair of positions, so your agents can use:
self.distancer.getDistance(p1, p2)
IMPORTANT: This method may run for at most 5 seconds.
"""
'''
Make sure you do not delete the following line. If you would like to
use Manhattan distances instead of maze distances in order to save
on initialization time, please take a look at
CaptureAgent.registerInitialState in captureAgents.py.
'''
CaptureAgent.registerInitialState(self, gameState)
'''
Your initialization code goes here, if you need any.
'''
def chooseAction(self, gameState: GameState) -> str:
"""
Picks among legal actions randomly.
"""
actions = gameState.getLegalActions(self.index)
return random.choice(actions)
class AgentTwo(CaptureAgent):
def registerInitialState(self, gameState: GameState):
CaptureAgent.registerInitialState(self, gameState)
def chooseAction(self, gameState: GameState) -> str:
actions = gameState.getLegalActions(self.index)
return random.choice(actions)