import math import random ##import ps2_visualize ##import pylab # === Provided class Position 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: number 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() angle = float(angle) # 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 __str__(self): return "(%0.2f, %0.2f)" % (self.x, self.y) 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.tiles = [[0 for x in range(height)] for x in range(width)] self.width = width self.height = height self.numTiles = int(width * height) self.cleanTiles = 0 ## assumes all are initially are dirty for w in range(width): for h in range(height): self.tiles[w][h] = 1 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 """ xPos = int(pos.getX()) yPos = int(pos.getY()) self.cleanTiles += 1 self.tiles[xPos][yPos] = 0 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 self.tiles[m][n] == 0 def getNumTiles(self): """ Return the total number of tiles in the room. returns: an integer """ return self.numTiles def getNumCleanedTiles(self): """ Return the total number of clean tiles in the room. returns: an integer """ numTiles = 0 ##for x in range(0, self.width): ## for y in range(0, self.height): ## if self.tiles[x][y] == 0: ## self.cleanTiles += 1 numTiles = self.cleanTiles self.cleanTiles = 0 return numTiles def getRandomPosition(self): """ Return a random position inside the room. returns: a Position object. """ xPos = int(random.randrange(0, self.width+1)) yPos = int(random.randrange(0, self.height+1)) return Position(xPos, yPos) 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. """ xPos = pos.getX() print "(%i, %i)" % (xPos, self.width) yPos = pos.getY() print "(%i, %i)" % (yPos, self.height) return xPos < self.width and xPos >= 0 and yPos < self.height and yPos >= 0 newRoom = RectangularRoom(10, 5) randPos = Position(5.71, 16.95) print newRoom.isPositionInRoom(randPos) print newRoom.cleanTileAtPosition(randPosition) print newRoom.getNumCleanedTiles() print 0 < 0.6 ##print randPosition
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question