Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions week3/final/robot-text/robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
'use strict';

const board = [
['T', 'T', '.', 'F'],
['T', '.', '.', '.'],
['.', '.', '.', '.'],
['R', '.', '.', 'W']
['.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.'],
['R', '.', '.', '.', 'F'],
['.', 'A', 'A', 'A', '.'],
['.', '.', '.', '.', '.'],
];

const robot = {
x: 0,
y: 0,
dir: 'up',
y: 2,
dir: 'right',
};

let flagReached = false;
let moves = 0;
let appleEaten = false;
let applesEaten = 0;

board.reverse();

Expand All @@ -39,11 +42,15 @@
if (flagReached) {
console.log('\nHurray! Flag reached in ' + moves + ' steps!');
}
if (applesEaten) {
console.log("Number of apples eaten: " + applesEaten);
}
}

function move() {
let x = robot.x;
let y = robot.y;
let appleEaten = false;

switch (robot.dir) {
case 'up':
Expand All @@ -62,14 +69,19 @@

const cellContents = board[y][x];

if (cellContents === '.' || cellContents === 'F') {
if (cellContents === '.' || cellContents === 'F' || cellContents === 'A') {
board[robot.y][robot.x] = trailIndicators[robot.dir];
robot.x = x;
robot.y = y;
board[y][x] = 'R';
if (cellContents === 'F') {
flagReached = true;
}
if (cellContents === 'A') {
appleEaten = true;
applesEaten += 1;
console.log('\nYUM!');
}
}

moves += 1;
Expand Down Expand Up @@ -98,13 +110,14 @@

render();

turn("right");
move();
turn('right');
turn("left");
move();
move();
move();
turn('left');
move();
turn("left");
move();

})();