Skip to content

Instantly share code, notes, and snippets.

@Untrusted-Game
Created January 24, 2021 00:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Untrusted-Game/887801e730badb0ddd0d19751ba9425c to your computer and use it in GitHub Desktop.
Save Untrusted-Game/887801e730badb0ddd0d19751ba9425c to your computer and use it in GitHub Desktop.
Solution to level 6 in Untrusted: http://alex.nisnevich.com/untrusted/
/****************
* drones101.js *
****************
*
* Do you remember, my dear Professor, a certain introductory
* computational rationality class you taught long ago? Assignment
* #2, behavior functions of autonomous agents? I remember that one
* fondly - but attack drones are so much easier to reason about
* when they're not staring you in the face, I would imagine!
*/
function startLevel(map) {
function moveToward(obj, type) {
var target = obj.findNearest(type);
var leftDist = obj.getX() - target.x;
var upDist = obj.getY() - target.y;
var direction;
if (upDist == 0 && leftDist == 0) {
return;
} if (upDist > 0 && upDist >= leftDist) {
direction = 'up';
} else if (upDist < 0 && upDist < leftDist) {
direction = 'down';
} else if (leftDist > 0 && leftDist >= upDist) {
direction = 'left';
} else {
direction = 'right';
}
if (obj.canMove(direction)) {
obj.move(direction);
}
}
map.defineObject('attackDrone', {
'type': 'dynamic',
'symbol': 'd',
'color': 'red',
'onCollision': function (player) {
player.killedBy('an attack drone');
},
'behavior': function (me) {
moveToward(me, 'player');
}
});
map.placePlayer(1, 1);
map.placeObject(map.getWidth()-2, 12, 'attackDrone');
map.placeObject(map.getWidth()-1, 12, 'exit');
map.placeObject(map.getWidth()-1, 11, 'block');
map.placeObject(map.getWidth()-2, 11, 'block');
map.placeObject(map.getWidth()-1, 13, 'block');
map.placeObject(map.getWidth()-2, 13, 'block');
map.defineObject('friendlyDrone', {
'type': 'dynamic',
'symbol': 'd',
'color': 'lime',
'behavior': function (me) {
moveToward(me, 'attackDrone');
}
});
map.placeObject(map.getWidth()-1, 0, 'friendlyDrone');
map.placeObject(map.getWidth()-1, map.getHeight()-1, 'friendlyDrone');
map.placeObject(0, 0, 'friendlyDrone');
map.placeObject(0, map.getHeight()-1, 'friendlyDrone');
/*
This is certainly not the shortest solution, of course
and likely not be expected one, either. However,
as in any other art (and make no mistake, coding is an art)
it's fun to use your opponent's code against them ;-)
Twisted Code (sometimes called macks2008 or macks2010) was here
*/
}
function validateLevel(map) {
map.validateExactlyXManyObjects(1, 'exit');
}
@macks2008
Copy link

Not to oversimplify: while coding is no doubt an art in my humble opinion (there are many solutions that achieve the same or extremely similar goals, for instance), it also has its foundations in science.

@macks2008
Copy link

in fact... most arts have their foundation in science in some way or another. We discovered paint by smashing things together, and analyzing the result of smashing things (e.g. "fascinating, if I grind this rock against this surface, it makes the surface brown"). Perhaps programming just developed much faster because we had robots to make all the "Paint" (semiconductor chips) and people like Alan Turing had too much free time on their hands?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment