How does overriding one method of Field add wormholes, and why should plot styles be set once rather than in every call?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Location and Field are given. Define HoleField(Field) whose __init__(self, holes) takes a list of pairs ((x, y), (newX, newY)): initialise the field the usual way, then store in self.wormholes a dictionary mapping each (x, y) to Location(newX, newY).
import random
class Location(object):
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, deltaX, deltaY):
return Location(self.x + deltaX, self.y + deltaY)
def getX(self):
return self.x
def getY(self):
return self.y
def distFrom(self, other):
xDist = self.x - other.getX()
yDist = self.y - other.getY()
return (xDist**2 + yDist**2)**0.5
def __str__(self):
return '<' + str(self.x) + ', ' + str(self.y) + '>'
class Drunk(object):
def __init__(self, name=None):
self.name = name
def __str__(self):
if self.name != None:
return self.name
return 'Anonymous'
class UsualDrunk(Drunk):
def takeStep(self):
stepChoices = [(0, 1), (0, -1), (1, 0), (-1, 0)]
return random.choice(stepChoices)
class Field(object):
def __init__(self):
self.drunks = {}
def addDrunk(self, drunk, loc):
if drunk in self.drunks:
raise ValueError('Duplicate drunk')
else:
self.drunks[drunk] = loc
def getLoc(self, drunk):
if drunk not in self.drunks:
raise ValueError('Drunk not in field')
return self.drunks[drunk]
def moveDrunk(self, drunk):
if drunk not in self.drunks:
raise ValueError('Drunk not in field')
xDist, yDist = drunk.takeStep()
self.drunks[drunk] = self.drunks[drunk].move(xDist, yDist)