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
33 changes: 22 additions & 11 deletions week3/final/robot-text/robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
'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 appleEaten = false;
let applesEaten = 0;
let moves = 0;

board.reverse();
Expand All @@ -37,13 +40,17 @@
console.log(line);
}
if (flagReached) {
console.log('\nHurray! Flag reached in ' + moves + ' steps!');
console.log('\nHurray! Flag reached in ' + moves + ' steps!' + 'I ate '+ applesEaten +' Apples');
}
if (appleEaten) {
console.log('YUM!');
}
}

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

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

const cellContents = board[y][x];

if (cellContents === '.' || cellContents === 'F') {
if (cellContents === '.' || cellContents === 'F' || cellContents==='A') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a space before and after the ===. This happens automatically if you use the Format Document command in VSCode.

board[robot.y][robot.x] = trailIndicators[robot.dir];
robot.x = x;
robot.y = y;
board[y][x] = 'R';
if (cellContents === 'A') {
appleEaten = true;
applesEaten ++;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is customary to append the ++ (or --) directly to the variable to which it applies (without an intervening space).

}
if (cellContents === 'F') {
flagReached = true;
}
Expand Down Expand Up @@ -98,13 +109,13 @@

render();

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

})();