# solution

In the lecture notes, there are two class definitions including “`Field“` and “`OddField“` where drunks can walk. For this problem, you need to define a “`DirtyField“`.

In the “`__init__“` method, the “`xRange“` and “`yRange“` values are used to specify the boundaries of the field (just like the “`OddField“` defined in the lecture notes). The “`dirtyTiles“` is used to denote the number of dirty tiles in the fields. You need to create the dirtyTiles by randomly selecting locations in an “`DirtyField“` object. The dirty locations should be different from each other in the same DirtyField.

Note that drunks don’t like dirty tiles. When a drunk moves in the field and happens to touch a dirty tile, the drunk cannot stop the move and would keep moving till he or she is able to step on a clean tile. The sequence of the actions occurred during the move would be counted as a single move in the dirty field at one time step.

Below provides some skeleton code for you to extend the two methods defined in “`DirtyField“`.

class Location(object):

def __init__(self, x, y):

“””x and y are numbers”””

self.x = x

self.y = y

class Field(object):

def __init__(self):

self.drunks = {}

class OddField(Field):

def __init__(self, numHoles=1000,

xRange=100, yRange=100):

Field.__init__(self)

self.wormholes = {}

for w in range(numHoles):

x = random.randint(-xRange, xRange)

y = random.randint(-yRange, yRange)

newX = random.randint(-xRange, xRange)

newY = random.randint(-yRange, yRange)

newLoc = Location(newX, newY)

self.wormholes[(x, y)] = newLoc

class DirtyField(Field):

def __init__(self, dirtyTiles = 1000,

xRange = 100, yRange = 100):

Field.__init__(self)

pass

if drunk in self.drunks:

raise ValueError(‘Duplicate drunk’)

else:

self.drunks[drunk] = loc

def moveDrunk(self, drunk):

Field.moveDrunk(self, drunk)

pass

start = Location(0, 0)

f = DirtyField()

homer = SouthDrunk(0.9)