Skip to content

Instantly share code, notes, and snippets.

Created December 18, 2014 02:04
Show Gist options
  • Save anonymous/aa978a77f0663d8f114a to your computer and use it in GitHub Desktop.
Save anonymous/aa978a77f0663d8f114a to your computer and use it in GitHub Desktop.
Solution to level 17 in Untrusted: http://alex.nisnevich.com/untrusted/
/***************
* 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);
}
} // end for loop
// find a teleporter that is in the bottom right corner...
// remember location in array:
var teleDestList = [];
var destmin = {
x: 36,
y: 18,
}
for (i = 0; i < teleportersAndTraps.length; i+=1) {
var tele = teleportersAndTraps[i];
if (tele.getType() == 'teleporter') {
//var pos = map.getCanvasCoords(tele);
if ( (tele.getX() > destmin.x) &&
(tele.getY() > destmin.y) ) {
teleDestList.push(i);
}
}
}
var teleDest = teleDestList[0];
// highlite target
map.setSquareColor(
teleportersAndTraps[teleDest].getX(),
teleportersAndTraps[teleDest].getY(),
'#9b0');
// now set all teleporters to the same target:
for (i = 0; i < teleportersAndTraps.length; i+=1) {
var tele = teleportersAndTraps[i];
// set all teleporters to the special target.
if (tele.getType() == 'teleporter') {
if (tele == teleportersAndTraps[teleDest] ) {
//handle self reference
tele.setTarget(teleportersAndTraps[teleDestList[1]]);
} else {
tele.setTarget(teleportersAndTraps[teleDest]);
}
}
}
// make some lines to see the connections
for (i = 0; i < teleportersAndTraps.length; i+=2) {
var t1 = teleportersAndTraps[i];
var t2 = teleportersAndTraps[i+1];
/**/
// only draw lines if both sides are teleporters
if ( (t1.getType() == 'teleporter') &&
(t2.getType() == 'teleporter') )
{
var start = map.getCanvasCoords(t1);
var end = map.getCanvasCoords(t2);
// using canvas to draw the line
var ctx = map.getCanvasContext();
ctx.beginPath();
ctx.strokeStyle = 'white';
ctx.lineWidth = 1;
ctx.moveTo(start.x, start.y);
ctx.lineTo(end.x, end.y);
ctx.stroke();
}
/**/
}
}
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