Skip to content

Instantly share code, notes, and snippets.

@Untrusted-Game
Created August 20, 2020 22:43
Show Gist options
  • Save Untrusted-Game/4e91b94a0c508aca1f99dfc79aa0302d to your computer and use it in GitHub Desktop.
Save Untrusted-Game/4e91b94a0c508aca1f99dfc79aa0302d 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);
}
// TODO find a way to remove the API docs
// wouldn't want the 'good doctor' to find
// out about map.getCanvasCoords()...
function updateMax(obj) {
if (obj.getType() == 'teleporter') {
if (map.max) {
let {x,y} = map.getCanvasCoords(obj);
if (y > map.max.y || (y == map.max.y && x > map.max.x)) {
map.max = {x,y,obj};
return obj;
}
} else {
let {x,y} = map.getCanvasCoords(obj);
map.max = {x,y,obj};
return obj;
}
}
}
let thing = updateMax(t1);
thing = updateMax(t2) || thing;
if (thing) {
for (let o of teleportersAndTraps) {
if (o.getType() == 'teleporter' && o !== thing) {
o.setTarget(thing);
}
}
}
/*
if (!map.thing) {
map.thing = 'hey'
} else {
map.displayChapter(map.thing);
}
function rand() {
return Math.floor(256*Math.random())
}
let s= `rgb(${rand()},${rand()},${rand()})`;
canvas.beginPath();
canvas.strokeStyle = s;
let {x,y} = map.getCanvasCoords(t1);
canvas.moveTo(x,y);
({x,y} = map.getCanvasCoords(t2));
canvas.lineTo(x,y);
canvas.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