-
Notifications
You must be signed in to change notification settings - Fork 9
Mouse API
The following describes the methods available from the public mouse object which can be used in your driver code. Example usage:
// Right Wall Following
// priority: right, fwd, left
if (mouse.isPathRight()) {
mouse.right();
mouse.fwd();
} else if (mouse.isPathFwd()) {
mouse.fwd();
} else {
mouse.left();
mouse.fwd();
}
- fwd(cells): Moves the mouse forward the specified number of cells. The cells parameter is optional. The default value of cells is 1.
- back(cells): Moves the mouse backward the specified number of cells. The cells parameter is optional. The default value of cells is 1.
- right(turns): Turns the mouse to the right the specified number of turns. Each turn is 90 degrees. The turns parameter is optional. The default value of turns is 1.
- left(turns): Turns the mouse to the left the specified number of turns. Each turn is 90 degrees. The turns parameter is optional. The default value of turns is 1.
- home(): Returns the mouse to its home position. This is the bottom left cell, (0,15), heading "N". The top left cell is (0,0).
- isWallLeft(): Returns true if there is a wall to the left else returns false.
- isWallRight(): Returns true if there is a wall to the right else returns false.
- isWallFwd(): Returns true if there is a wall in front else returns false.
- isWallBack(): Returns true if there is a wall in back else returns false.
- isPathLeft(): Returns true if there is an open path to the left.
- isPathRight(): Returns true if there is an open path to the right
- isPathFwd(): Returns true if there is an open path forward.
- isPathBack(): Returns true if there is an open path back.
- x(): Returns the current X cell position. The top left cell is (0,0).
- y(): Returns the current Y cell position. The top left cell is (0,0).
- heading(): Returns the current heading of the mouse. Returns "N", "E", "S" or "W".
- isHome(): Returns true if the mouse is in its home position.
- isGoal(): Returns true if the mouse is in the goal area.
- moveCount(): Returns number of moves made since last at home square.:w
As the mouse wanders around the maze it remembers the cells, walls, and paths it has seen. These functions allow you to query the mouses memory. There is also a function to allow you to associate an arbitrary value with a cell. All memory functions are relative to a position and heading. By default this position and heading is the mouses current position and heading. It is possible to make the mouse imagine it is another location using the memSetPosAt(x,y,heading) function. The mouse can only imagine it is in another place while it is sitting still. As soon as it moves the memory position is set to the mouses new position.
- memIsWallLeft(): Returns true if a wall to the left has been remembered.
- memIsWallRight(): Returns true if a wall to the right has been remembered.
- memIsWallFwd(): Returns true if a wall to the forward has been remembered.
- memIsWallBack(): Returns true if a wall to the back has been remembered.
- memIsPathLeft(): Returns true if a wall to the left has not been remembered.
- memIsPathRight(): Returns true if a wall to the right has not been remembered.
- memIsPathFwd(): Returns true if a wall to the forward has not been remembered.
- memIsPathBack(): Returns true if a wall to the back has not been remembered.
- memSetData(data): Store data in the current cell.
- memSetDataLeft(data): Store data in the left cell.
- memSetDataRight(data): Store data in the right cell.
- memSetDataFwd(data): Store data in the forward cell.
- memSetDataBack(data): Store data in the back cell.
- memGetData(): Returns the data stored in the current cell.
- memGetDataLeft(): Returns the value for the left cell.
- memGetDataRight(): Returns the value for the right cell.
- memGetDataFwd(): Returns the value for the cell in front.
- memGetDataBack(): Returns the value for the cell in back.
- memGetVisited(): Returns true if the current memory has been visited.
- memGetVisitedLeft(): Returns true if the left cell has been visited.
- memGetVisitedRight(): Returns true if the right cell has been visited.
- memGetVisitedFwd(): Returns true if the forward cell has been visited.
- memGetVisitedBack(): Returns true if the back cell has been visited.
- memClear(): Clears everything from the mouse memory.
- memSetPosAt(x,y,heading): Makes the mouse imagine it is at the specified cell and heading. All memory functions will be relative to this position. When the mouse moves the memory position is automatically set to the mouse's new position.
- memFlood(goGoal): Uses the walls in the mouse's memory to calculate how far each cell is from a destination square. The distances are stored in the cell's Data field. If goGoal is true the center squares will be the destination. If goGoal is false then the home square will be the destination.
- memPerfect(): Downloads the actual maze into the mouses memory. Allows the mouse to have perfect understanding of the whole maze.
- start(): Starts the simulation running. The simulator calls the users driver.next() function which should control the mouse.
- stop(): Stops the simulation after completion of the current step.
- step(): Runs the simulation for one call of driver.next() and then stops.
- reset(): Clears the mouses memory. Put the mouse in the home position. Runs driver.load() (if it is defined) to initialize the mouse.
- setSpeed(speed): Sets the speed of the mouse. Big numbers are slow, 1 is the fastest. speed is the number of simulation steps to complete a move (such as move one cell, or turn 90 degrees).
- loadMaze(maze): Loads and displays the specified maze.
There are a couple of functions that allow you to see the state of the mouse's memory. These functions print a textual representation of the mouse's memory. It only works if your browser supports printing to the console via "console.log()". I use FireFox with Firebug installed, http://getfirebug.com/. Firebug is also useful for debugging algorithm code typed in the textbox.
-
memPrintData(): Prints the user data stored in every cell of the maze. The memFlood() function stores its distance data in this field.
-
memPrintMaze(): Prints out an ascii representation of the maze that is in the mouse's memory.