How can I get this robot to finish the maze?
Rather than make a simple AI, it is much easier to have the Robot mirror your movements using the following code:
if(me.getY()>map.getPlayer().getY()-9)
me.move('up');
else if(me.getY()<map.getPlayer().getY()-9)
me.move('down');
else if(me.getX()>map.getPlayer().getX())
me.move('left');
else if(me.getX()<map.getPlayer().getX())
me.move('right');
Then move up and left until the Robot is mirroring your movements. Guide him through the maze as if you are controlling him. Once he has the key and is out, move right one, and go all the way up to collide with the R
and get the key.
*
*Note your maze will likely be different, but guiding the robot is simple when his controls match yours.
You don't need a fancy AI to solve the problem. Just use a simple "hug the wall to your right" strategy, and you can escape any maze. It simply takes a little longer.
var dirs = ['left', 'up', 'right', 'down'];
if(map.rdir === undefined)
map.rdir = 0;
// The good old hug-the-wall-to-your-right algorithm
var trythese = [map.rdir + 1, map.rdir, map.rdir - 1, map.rdir + 2];
for(var i=0; i<4; i++) {
var d = (trythese[i] + 4) % 4;
if(me.canMove(dirs[d])) {
me.move(dirs[d])
map.rdir = d;
break;
}
}
Note that I am storing an extra property on map
(map.rdir
) to do this. This is totally permissible since JavaScript allows the use of arbitrary properties on objects.
Disclaimer: This is most probably very far from the intended solution, but it is still one.
Take advantage of the fact that a maze is randomly generated.
Thanks to the way the maze is generated, there is a very reliable chance that by restarting the maze just a few times, in one of those mazes, the simple code
if(me.canMove('down')) me.move('down');
else me.move('right');
is enough to get the robot out of the maze.