Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * robotMaze.js
- *
- * The blue key is inside a labyrinth, and extracting
- * it will not be easy.
- *
- * It's a good thing that you're a AI expert, or
- * we would have to leave empty-handed.
- */
- function startLevel(map) {
- // Hint: you can press R or 5 to "rest" and not move the
- // player, while the robot moves around.
- map.getRandomInt = function(min, max) {
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- map.placePlayer(map.getWidth()-1, map.getHeight()-1);
- var player = map.getPlayer();
- map.defineObject('robot', {
- 'type': 'dynamic',
- 'symbol': 'R',
- 'color': 'gray',
- 'onCollision': function (player, me) {
- me.giveItemTo(player, 'blueKey');
- },
- 'behavior': function (me) {
- var currentX = me.getX();
- var currentY = me.getY();
- var moves = map.getAdjacentEmptyCells(currentX, currentY);
- // remove dead ends
- addVisit(currentX, currentY);
- moves.sort((a, b) => {
- let visitsA = getVisits(a[0][0], a[0][1]);
- let visitsB = getVisits(b[0][0], b[0][1]);
- return visitsA - visitsB;
- }
- );
- console.log("@["+currentX+","+currentY+"]");
- console.log(moves);
- if(amIAboveTheKey()) {
- console.log("I am above the key");
- me.finale = 4;
- me.move('down');
- } else {
- if(me.finale--) {
- me.move('down');
- } else {
- me.move(moves[0][1]);
- }
- }
- if(isDeadEnd()) {
- //this is a dead end
- this.color = 'red';
- map.placeObject(currentX, currentY, 'block');
- } else if (isACorner()) {
- this.color = 'yellow';
- map.placeObject(currentX, currentY, 'block');
- } else {
- this.color = 'white';
- }
- function amIAboveTheKey() {
- me.keyLocation = me.findNearest('blueKey');
- console.log("location of the key:");
- console.log(me.keyLocation);
- if( me.keyLocation ) {
- if( currentX == me.keyLocation.x &&
- currentY == me.keyLocation.y-1) {
- return true;
- }
- }
- return false;
- }
- function getVisits(x, y){
- if( Array.isArray(me.visited) &&
- Array.isArray(me.visited[x]) &&
- me.visited[x][y] ) {
- //console.log("Cell ["+x+","+y+"] visited "
- // + me.visited[x][y].times + " times");
- return me.visited[x][y].times;
- } else {
- //console.log("Cell ["+x+","+y+"] visited "
- // + 0 + " times");
- return 0;
- }
- }
- function addVisit(currentX, currentY){
- if (!Array.isArray(me.visited)) {
- me.visited = new Array();
- }
- if (!Array.isArray(me.visited[currentX])) {
- me.visited[currentX] = new Array();
- }
- if (!me.visited[currentX][currentY]) {
- me.visited[currentX][currentY] = {};
- }
- if(!me.visited[currentX][currentY].times){
- console.log(me.visited[currentX][currentY]);
- me.visited[currentX][currentY].times = 1;
- console.log(me.visited[currentX][currentY]);
- } else {
- let t = me.visited[currentX][currentY].times;
- me.visited[currentX][currentY].times = t + 1;
- }
- map.writeStatus("@[" + currentX + "," + currentY + "]:" +
- me.visited[currentX][currentY].times
- );
- }
- function isDeadEnd(){
- return moves.length == 1;
- }
- function isAPassage(){
- return moves.length == 2;
- }
- function isACorner(){
- if (moves.length == 1) {
- if(me.canMove('left') && me.canMove('right')){
- return false;
- }
- if(me.canMove('up') && me.canMove('down')){
- return false;
- }
- return true;
- }
- return false;
- }
- }
- });
- map.defineObject('barrier', {
- 'symbol': '░',
- 'color': 'purple',
- 'impassable': true,
- 'passableFor': ['robot']
- });
- map.placeObject(0, map.getHeight() - 1, 'exit');
- map.placeObject(1, 1, 'robot');
- map.placeObject(map.getWidth() - 2, 8, 'blueKey');
- map.placeObject(map.getWidth() - 2, 9, 'barrier');
- var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
- autoGeneratedMaze.create( function (x, y, mapValue) {
- // don't write maze over robot or barrier
- if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
- return 0;
- } else if (mapValue === 1) { //0 is empty space 1 is wall
- map.placeObject(x,y, 'block');
- } else {
- map.placeObject(x,y,'empty');
- }
- });
- }
- function validateLevel(map) {
- map.validateExactlyXManyObjects(1, 'exit');
- map.validateExactlyXManyObjects(1, 'robot');
- map.validateAtMostXObjects(1, 'blueKey');
- }
- function onExit(map) {
- if (!map.getPlayer().hasItem('blueKey')) {
- map.writeStatus("We need to get that key!");
- return false;
- } else {
- return true;
- }
- }
Add Comment
Please, Sign In to add comment