Last active
August 29, 2015 14:03
-
-
Save Narvey/7125e31843ddb809bcf2 to your computer and use it in GitHub Desktop.
This is a general solution to ANY maze (except for the assumption of the location of the key, but if you comment that out, it works anywhere.) **editing to remove extra newlines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* robotMaze.js | |
* | |
* The blue key is inside a labyrinth, and extracting | |
* it will not be easy. | |
* | |
* It's a good thing that you're a AI expert, or | |
* we would have to leave empty-handed. | |
*/ | |
function startLevel(map) { | |
// Hint: you can press R or 5 to "rest" and not move the | |
// player, while the robot moves around. | |
map.getRandomInt = function(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
map.placePlayer(map.getWidth()-1, map.getHeight()-1); | |
var player = map.getPlayer(); | |
map.defineObject('robot', { | |
'type': 'dynamic', | |
'symbol': 'R', | |
'color': 'gray', | |
'onCollision': function (player, me) { | |
me.giveItemTo(player, 'blueKey'); | |
}, | |
'behavior': function (me) { | |
//head straight down once you get the key | |
if(me.hasItem('blueKey')){ | |
me.move('down'); | |
return; | |
} | |
//otherwise, keep your right hand on the wall and don't let go. | |
if(me.face==null)me.face='down'; | |
if(me.face=='down') | |
{ | |
if(me.canMove('left')){ | |
me.move('left'); | |
me.face='left'; | |
} | |
else if(me.canMove('down')) me.move('down'); | |
else if(me.canMove('right')) | |
{ | |
me.move('right'); | |
me.face='right'; | |
} | |
else if(me.canMove('up')) | |
{ | |
me.move('up'); | |
me.face='up'; | |
} | |
} | |
if(me.face=='left') | |
{ | |
if(me.canMove('up')){ | |
me.move('up'); | |
me.face='up'; | |
} | |
else if(me.canMove('left')) me.move('left'); | |
else if(me.canMove('down')) | |
{ | |
me.move('down'); | |
me.face='down'; | |
} | |
else if(me.canMove('right')) | |
{ | |
me.move('right'); | |
me.face='right'; | |
} | |
} | |
if(me.face=='up') | |
{ | |
if(me.canMove('right')){ | |
me.move('right'); | |
me.face='right'; | |
} | |
else if(me.canMove('up')) me.move('up'); | |
else if(me.canMove('left')) | |
{ | |
me.move('left'); | |
me.face='left'; | |
} | |
else if(me.canMove('down')) | |
{ | |
me.move('down'); | |
me.face='down'; | |
} | |
} | |
if(me.face=='right') | |
{ | |
if(me.canMove('down')){ | |
me.move('down'); | |
me.face='down'; | |
} | |
else if(me.canMove('right')) me.move('right'); | |
else if(me.canMove('up')) | |
{ | |
me.move('up'); | |
me.face='up'; | |
} | |
else if(me.canMove('left')) | |
{ | |
me.move('left'); | |
me.face='left'; | |
} | |
} | |
/* | |
Notes: | |
if me.face=='down', we want to make sure there is a block to our left. | |
scratch that, keep following the right-hand wall until the key is reached. | |
*/ | |
} | |
}); | |
map.defineObject('barrier', { | |
'symbol': '░', | |
'color': 'purple', | |
'impassable': true, | |
'passableFor': ['robot'] | |
}); | |
map.placeObject(0, map.getHeight() - 1, 'exit'); | |
map.placeObject(1, 1, 'robot'); | |
map.placeObject(map.getWidth() - 2, 8, 'blueKey'); | |
map.placeObject(map.getWidth() - 2, 9, 'barrier'); | |
var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10); | |
autoGeneratedMaze.create( function (x, y, mapValue) { | |
// don't write maze over robot or barrier | |
if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) { | |
return 0; | |
} else if (mapValue === 1) { //0 is empty space 1 is wall | |
map.placeObject(x,y, 'block'); | |
} else { | |
map.placeObject(x,y,'empty'); | |
} | |
}); | |
} | |
function validateLevel(map) { | |
map.validateExactlyXManyObjects(1, 'exit'); | |
map.validateExactlyXManyObjects(1, 'robot'); | |
map.validateAtMostXObjects(1, 'blueKey'); | |
} | |
function onExit(map) { | |
if (!map.getPlayer().hasItem('blueKey')) { | |
map.writeStatus("We need to get that key!"); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment