Amazing Learning
Table of Contents
The Challenge
- can I model this environment effectively?
- how can I use my model to plan a strategy?
- how can I translate my strategy into code?
The implementation
First I chose the Kruskal algorithm to generate a maze, with no particular reason, but with the intention of implementing also other algorithms: the same escape strategy may perform differently with different maze kind.
The maze will be defined within a rectangular grid of square cells which may be limited by walls; while the Kruskal algorithm may be used on any graph this will simplify the design of the game
I searched for a simple visualization solution which would allow:
- interaction from a python CLI
- the smallest list of dependencies
Installation
This game is designed to use only packages from standard python library; this installation procedure does just install one package
I assume there is already a python installation.
pip install git+https://github.com/noiseOnTheNet/maze_robot.git
Basic Usage
Launch a command python command line then type the following
A tk window with a 10×10 random maze appears: the maze is all connected.
The robot is represented by a circle while the exit is represented by a square
Coordinates start from 0,0 in the top left corner and grow moving right (x coordinate) or down (y coordinate)
The robot will always start at 0,0 while the exit is put randomly somewhere
In case you want to launch it within an ipython shell please execute the following command in advance:
# imports the robot object and 4 directions from maze_robot import Robot, N, S, E, W # create a new robot instance r = Robot() # shows the maze r.view()
%gui tk
Moving around the maze
You can move around your robot by using the move method, passing one direction (N, S, W, E) enumeration object
if there is no wall in that direction the operation succeeds returning a true value, otherwise a false value is returned
The robot has a “geolocation” functionality: it knows where it is in the grid. To get its position you can look at the “position” read-only attribute
r.move(N)
r.position # it returns a tuple of integers e.g. (1, 2)
Escaping the maze
You can guide the robot to the exit by moving it around until you reach the exit. The exit is sensed by the robot using the exit method
this method returns true if the robot is in the same grid cell as the exit.
But this is not the funniest way to use the robot: I think the funniest way is to see the robot find the exit by itself.
While you can see the whole maze, the robot does not. It just see the walls around its position.
r.exit()
r.walls # returns a tuple of directions where walls are e.g.(N, W, S)
Leaving a trace
If you want to see where your robot moves you can leave a graphical trace on the maze floor by using the
Now the robot will leave a red track behind. The robot cannot sense this track so it is only for you to see
When you want to stop littering the maze you can use
down_pen()
method.
r.down_pen()
up_pen()
r.up_pen()
Advanced Usage
As the main goal is to create algorithms it may be convenient to have a finer control on the environment
Changing Maze size
you can provide your own maze by using the
Have fun with larger mazes!
Warning: the current implementation of the maze generation is not the most efficient, larger mazes may be slow to create.
Maze
class
from maze_robot.robot import Maze maze = Maze.kruskal(cols=20,rows=10) r = Robot(maze) r.view()
Deterministic Random Maze Creation
If you want to test your algorithm on a specific maze you can provide a seed which will generate always the same maze.
from maze_robot.robot import Maze maze = Maze.kruskal(cols=20,rows=10, seed=0) r = Robot(maze) r.view()
Conclusions
So far student’s response looks positive: I will update this post when I collect more feedbacks