Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Problem Set 6: Simulating robots
- # Name:
- # Collaborators:
- # Time:
- import math
- import random
- import ps6_visualize
- import pylab
- # === Provided classes
- class Position(object):
- """
- A Position represents a location in a two-dimensional room.
- """
- def __init__(self, x, y):
- """
- Initializes a position with coordinates (x, y).
- """
- self.x = x
- self.y = y
- def getX(self):
- return self.x
- def getY(self):
- return self.y
- def getNewPosition(self, angle, speed):
- """
- Computes and returns the new Position after a single clock-tick has
- passed, with this object as the current position, and with the
- specified angle and speed.
- Does NOT test whether the returned position fits inside the room.
- angle: float representing angle in degrees, 0 <= angle < 360
- speed: positive float representing speed
- Returns: a Position object representing the new position.
- """
- old_x, old_y = self.getX(), self.getY()
- # Compute the change in position
- delta_y = speed * math.cos(math.radians(angle))
- delta_x = speed * math.sin(math.radians(angle))
- # Add that to the existing position
- new_x = old_x + delta_x
- new_y = old_y + delta_y
- return Position(new_x, new_y)
- def __eq__(self, pos):
- if self.getX() == pos.getX() and self.getY() == pos.getY():
- return True
- else:
- return False
- # === Problems 1
- 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 = int(width)
- self.height = int(height)
- # Tiles are a list of positions
- self.tiles = []
- for i in range(self.width):
- for j in range(self.height):
- self.tiles.append(Position(i,j))
- self.dirty_tiles = self.tiles[:]
- 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
- """
- if pos in self.dirty_tiles:
- self.dirty_tiles.remove(pos)
- def getDirtyTiles(self):
- """
- Returns a list of dirty tiles
- """
- d_tiles_list = []
- for tiles in self.dirty_tiles:
- # yield tiles.getX(), tiles.getY()
- d_tiles_list.append(str(tiles.getX())+","+str(tiles.getY()))
- return d_tiles_list
- def isTileCleaned(self, pos):
- """
- 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
- """
- #is_clean = Position(m, n)
- return pos not in self.dirty_tiles
- def getNumTiles(self):
- """
- Return the total number of tiles in the room.
- returns: an integer
- """
- return len(self.tiles)
- def getNumCleanedTiles(self):
- """
- Return the total number of clean tiles in the room.
- returns: an integer
- """
- return len(self.tiles) - len(self.dirty_tiles)
- def getRandomPosition(self):
- """
- Return a random position inside the room.
- returns: a Position object.
- """
- return self.tiles[random.randint(0,len(self.tiles))]
- 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.
- """
- # Check for position out of rectangle bounds
- if pos.getX() > self.width or pos.getY() > self.height:
- return False
- if pos.getX() < 0 or pos.getY() < 0:
- return False
- return True
- class Robot(object):
- """
- Represents a robot cleaning a particular room.
- At all times the robot has a particular position and direction in the room.
- The robot also has a fixed speed.
- Subclasses of Robot should provide movement strategies by implementing
- updatePositionAndClean(), which simulates a single time-step.
- """
- def __init__(self, room, speed, name="Little Jimmy"):
- """
- Initializes a Robot with the given speed in the specified room. The
- robot initially has a random direction and a random position in the
- room. The robot cleans the tile it is on.
- room: a RectangularRoom object.
- speed: a float (speed > 0)
- """
- self.name = name
- self.room = room
- self.speed = speed
- self.position = Position(0,0)
- self.direction = 0
- def getRobotPosition(self):
- """
- Return the position of the robot.
- returns: a Position object giving the robot's position.
- """
- return self.position
- def getRobotDirection(self):
- """
- Return the direction of the robot.
- returns: an integer d giving the direction of the robot as an angle in
- degrees, 0 <= d < 360.
- """
- return self.direction
- def setRobotPosition(self, position):
- """
- Set the position of the robot to POSITION.
- position: a Position object.
- """
- self.position = position
- def setRobotDirection(self, direction):
- """
- Set the direction of the robot to DIRECTION.
- direction: integer representing an angle in degrees
- """
- self.direction = direction
- def updatePositionAndClean(self):
- """
- Simulate the raise passage of a single time-step.
- Move the robot to a new position and mark the tile it is on as having
- been cleaned.
- """
- while True:
- self.setRobotDirection(random.randint(0,359))
- self.position = self.position.getNewPosition(self.getRobotDirection,1)
- current_tile = Position(x=float(self.position.getX()), y=float(self.position.getY()))
- print "Full steam ahead", self.position.getX(), self.position.getX(), "@",self.direction
- if self.room.isPositionInRoom(self.position):
- print self.name,"takes a skip and a hop."
- if not self.room.isTileCleaned(current_tile):
- self.room.cleanTileAtPosition(current_tile)
- print self.name,"pirouettes, unleashing a fury of dust."
- break
- print "Bump."
Advertisement
Add Comment
Please, Sign In to add comment