Skip to content

Instantly share code, notes, and snippets.

@mooreniemi
Created November 25, 2012 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooreniemi/4145131 to your computer and use it in GitHub Desktop.
Save mooreniemi/4145131 to your computer and use it in GitHub Desktop.
600x
class RectangularRoom(object):
"""
A RectangularRoom represents a rectangular region containing clean or dirty
tiles.
A room has a width and a height and contains (width * height) tiles. At any
particular time, each of these tiles is either clean or dirty.
"""
def __init__(self, width, height):
"""
Initializes a rectangular room with the specified width and height.
Initially, no tiles in the room have been cleaned.
width: an integer > 0
height: an integer > 0
"""
self.width = width
self.height = height
self.clean = []
def cleanTileAtPosition(self, pos):
"""
Mark the tile under the position POS as cleaned.
Assumes that POS represents a valid position inside this room.
pos: a Position
"""
point = (int(pos.getX()), int(pos.getY()))
if point not in self.clean:
self.clean.append(point)
def isTileCleaned(self, m, n):
"""
Return True if the tile (m, n) has been cleaned.
Assumes that (m, n) represents a valid tile inside the room.
m: an integer
n: an integer
returns: True if (m, n) is cleaned, False otherwise
"""
return (m,n) in self.clean
def getNumTiles(self):
"""
Return the total number of tiles in the room.
returns: an integer
"""
return self.width * self.height
def getNumCleanedTiles(self):
"""
Return the total number of clean tiles in the room.
returns: an integer
"""
return len(self.clean)
def getRandomPosition(self):
"""
Return a random position inside the room.
returns: a Position object.
"""
x = random.randint (0,self.width - 1)
y = random.randint (0,self.height - 1)
return Position (x, y)
def isPositionInRoom(self, pos):
"""
Return True if POS is inside the room.
pos: a Position object.
returns: True if POS is in the room, False otherwise.
"""
if pos.getX()<self.width and pos.getX() >= 0:
if pos.getY() < self.height and pos.getY() >=0:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment