I am applying to a university to study Computational Linguistics, and as I read, it would be recommended to have a background in Artificial Intelligence.
The Admission board asked me to prepare a portfolio of my works, and I am considering to add this solution to the portfolio.
I have been lazy to develop an AI to bypass the default obstacles, so I added mine to ease the movement of the robot.
/*
* robotNav.js
*
* The green key is located in a slightly more
* complicated room. You'll need to get the robot
* past these obstacles.
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function startLevel(map) {
map.placePlayer(0, map.getHeight() - 1);
var player = map.getPlayer();
map.defineObject('robot', {
'type': 'dynamic',
'symbol': 'R',
'color': 'gray',
'onCollision': function (player, me) {
me.giveItemTo(player, 'greenKey');
},
'behavior': function (me) {
for(i = 2; i<9; i++){
map.placeObject(map.getWidth() - 20, i, 'block');
}
for(i = 2; i<9; i++){
map.placeObject(map.getWidth() - 3, i, 'block');
}
if(me.canMove('down') && !me.canMove('left')){
me.move('down');
}else{
if(me.canMove('right') && !me.canMove('down')){
me.move('right');
}
if(me.canMove('up') && !me.canMove('right')){
if(me.canMove('left')){
me.move('up');
}else{
me.move('down');
}
}
if(!me.canMove('up') && me.canMove('right')){
me.move('right');
}
if(!me.canMove('up') && !me.canMove('right')){
me.move('down');
}
}
}
});
map.defineObject('barrier', {
'symbol': '░',
'color': 'purple',
'impassable': true,
'passableFor': ['robot']
});
map.placeObject(map.getWidth() - 1, map.getHeight() - 1, 'exit');
map.placeObject(1, 1, 'robot');
map.placeObject(map.getWidth() - 2, 8, 'greenKey');
map.placeObject(map.getWidth() - 2, 9, 'barrier');
for (var x = 0; x < map.getWidth(); x++) {
map.placeObject(x, 0, 'block');
if (x != map.getWidth() - 2) {
map.placeObject(x, 9, 'block');
}
}
for (var y = 1; y < 9; y++) {
map.placeObject(0, y, 'block');
map.placeObject(map.getWidth() - 1, y, 'block');
}
for (var i = 0; i < 4; i++) {
map.placeObject(20 - i, i + 1, 'block');
map.placeObject(35 - i, 8 - i, 'block');
}
}
function validateLevel(map) {
map.validateExactlyXManyObjects(1, 'exit');
map.validateExactlyXManyObjects(1, 'robot');
map.validateAtMostXObjects(1, 'greenKey');
}
function onExit(map) {
if (!map.getPlayer().hasItem('greenKey')) {
map.writeStatus("We need to get that key!");
return false;
} else {
return true;
}
}
This Solution is to the quiz #12 in Chapter 2 of Untrusted Game.
What I mean by ethical is that am I allowed to cheat my way out in the AI world, or am I required to solve the problem as it is without additions (the additions here are the two added walls).
EDIT:
Here is the default setup of the scene:
And here are my changes:
The '@' is the player, the small blue rectangle in the bottom-right corner is the goal, 'R' is the robot that must be programmed (it moves once the player moves), 'K' is the goal for the robot it also is the key that allows the player to open his goal, the robot must reach the key and move through the portal under it (no human is allowed to pass it) and provide the key to the player so he can win the quiz.
Another thing is that I am only allowed to edit the behaviour of the 'robot' object (at line 24).
'behavior': function (me) {....}
player
variable to store arbitrary state, e.g. via an object. The robot can erect barriers to seal off dead ends. \$\endgroup\$y
distance. No AI of any kind, just I :) \$\endgroup\$