Created
December 17, 2019 21:09
-
-
Save Untrusted-Game/e603f2171acce2faa46f7aa45a1fa42a to your computer and use it in GitHub Desktop.
Solution to level 17 in Untrusted: http://alex.nisnevich.com/untrusted/
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
/*************** | |
* pointers.js * | |
*************** | |
* | |
* You! How are you still alive? | |
* | |
* Well, no matter. Good luck getting through this | |
* maze of rooms - you'll never see me or the Algorithm again! | |
*/ | |
function startLevel(map) { | |
function shuffle(o){ //v1.0 [http://bit.ly/1l6LGQT] | |
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), | |
x = o[--i], o[i] = o[j], o[j] = x); | |
return o; | |
}; | |
map.createFromGrid( | |
['+++++++++++++++++++++++++++++++++++++++++++++', | |
'++o *++++o *++++o *++++o *++++o *++++o *+++++', | |
'+* @ o++* o++* o++* o++* o++* o++++', | |
'++o *++++o *++++o *++++o *++++o *++++o *+++++', | |
'+++++++++++++++++++++++++++++++++++++++++++++', | |
'+++++* o++++* o++++* o++++* o++++* o++++* o++', | |
'++++o *++o *++o *++o *++o *++o *+', | |
'+++++* o++++* o++++* o++++* o++++* o++++* o++', | |
'+++++++++++++++++++++++++++++++++++++++++++++', | |
'++o *++++o *++++o *++++o *++++o *++++o *+++++', | |
'+* o++* o++* o++* o++* o++* o++++', | |
'++o *++++o *++++o *++++o *++++o *++++o *+++++', | |
'+++++++++++++++++++++++++++++++++++++++++++++', | |
'+++++* o++++* o++++* o++++* o++++* o++++* o++', | |
'++++o *++o *++o *++o *++o *++o *+', | |
'+++++* o++++* o++++* o++++* o++++* o++++* o++', | |
'+++++++++++++++++++++++++++++++++++++++++++++', | |
'++o *++++o *++++o *++++o *++++o *++++o *+++++', | |
'+* o++* o++* o++* o++* o++* E o++++', | |
'++o *++++o *++++o *++++o *++++o *++++o *+++++', | |
'+++++++++++++++++++++++++++++++++++++++++++++'], | |
{ | |
'@': 'player', | |
'E': 'exit', | |
'+': 'block', | |
'o': 'teleporter', | |
'*': 'trap', | |
}, 2, 2); | |
var canvas = map.getCanvasContext(); | |
var teleportersAndTraps = map.getDynamicObjects(); | |
teleportersAndTraps = shuffle(teleportersAndTraps); | |
for (i = 0; i < teleportersAndTraps.length; i+=2) { | |
var t1 = teleportersAndTraps[i]; | |
var t2 = teleportersAndTraps[i+1]; | |
// Point each teleporter to either another teleporter | |
// or a trap | |
if (t1.getType() == 'teleporter') { | |
t1.setTarget(t2); | |
} | |
if (t2.getType() == 'teleporter') { | |
t2.setTarget(t1); | |
} | |
// TODO find a way to remove the API docs | |
// wouldn't want the 'good doctor' to find | |
// out about map.getCanvasCoords()... | |
var exitCoords = t1.findNearest('exit'); | |
var telePos = [exitCoords.x + 2, exitCoords.y]; | |
map.setSquareColor(telePos[0], telePos[1], '#00f'); | |
for (j = 0; j < teleportersAndTraps.length; j++) { | |
var tn = teleportersAndTraps[j]; | |
if (tn.getX() === telePos[0] && | |
tn.getY() === telePos[1]) { | |
if (t1.getType() == 'teleporter' && | |
t1.getX() !== telePos[0] && | |
t1.getY() !== telePos[1]) { | |
t1.setTarget(tn); | |
} | |
if (t2.getType() == 'teleporter' && | |
t2.getX() !== telePos[0] && | |
t2.getY() !== telePos[1]) { | |
t2.setTarget(tn); | |
} | |
break; | |
} | |
} | |
} | |
} | |
function validateLevel(map) { | |
map.validateExactlyXManyObjects(1, 'exit'); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment