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
221 changes: 121 additions & 100 deletions week3/final/robot-text/robot.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,131 @@
(function () {
'use strict';

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

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

let flagReached = false;
let moves = 0;

board.reverse();

const trailIndicators = {
left: '←',
right: '→',
up: '↑',
down: '↓'
};

function render() {
console.log('\n ' + moves + ':');
for (let row = board.length - 1; row >= 0; row--) {
const cells = board[row];
let line = '';
for (let col = 0; col < cells.length; col++) {
line += ' ' + cells[col] + ' ';
}
console.log(line);
}
if (flagReached) {
console.log('\nHurray! Flag reached in ' + moves + ' steps!');
}
}

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

switch (robot.dir) {
case 'up':
y = y < board.length - 1 ? y + 1 : y;
break;
case 'down':
y = y > 0 ? y - 1 : y;
break;
case 'left':
x = x > 0 ? x - 1 : x;
break;
case 'right':
x = x < board[y].length - 1 ? x + 1 : x;
break;
}
'use strict';

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

const cellContents = board[y][x];
const robot = {
x: 0,
y: 2,
dir: 'right'
};

if (cellContents === '.' || cellContents === 'F') {
board[robot.y][robot.x] = trailIndicators[robot.dir];
robot.x = x;
robot.y = y;
board[y][x] = 'R';
if (cellContents === 'F') {
flagReached = true;
}
let flagReached = false;
let appleEaten = false;
let applesEaten = 0;
let moves = 0;

board.reverse();

const trailIndicators = {
left: '←',
right: '→',
up: '↑',
down: '↓'
};

function render() {
console.log('\n ' + moves + ':');
for (let row = board.length - 1; row >= 0; row--) {
const cells = board[row];
let line = '';
for (let col = 0; col < cells.length; col++) {
line += ' ' + cells[col] + ' ';
}
console.log(line);
}
if (appleEaten) {
console.log('\nYUM!');
}

if (flagReached) {
console.log('\nHurray! Flag reached in ' + moves + ' steps!');
console.log('\n'+ applesEaten + 'apples are eaten!');

}
}


moves += 1;
render();
}
function move() {
let x = robot.x;
let y = robot.y;
appleEaten = false;

function turn(turnDirection) {
if (turnDirection !== 'left' && turnDirection !== 'right') {
console.log('ignoring invalid turn', turnDirection);

switch (robot.dir) {
case 'up':
y = y < board.length - 1 ? y + 1 : y;
break;
case 'down':
y = y > 0 ? y - 1 : y;
break;
case 'left':
x = x > 0 ? x - 1 : x;
break;
case 'right':
x = x < board[y].length - 1 ? x + 1 : x;
break;
}

const cellContents = board[y][x];

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') {
applesEaten++;

if (applesEaten === 3) {
appleEaten = true;
}
}
}
moves += 1;
render();
}
switch (robot.dir) {
case 'up':
robot.dir = turnDirection === 'left' ? 'left' : 'right';
break;
case 'down':
robot.dir = turnDirection === 'left' ? 'right' : 'left';
break;
case 'left':
robot.dir = turnDirection === 'left' ? 'down' : 'up';
break;
case 'right':
robot.dir = turnDirection === 'left' ? 'up' : 'down';
break;

function turn(turnDirection) {
if (turnDirection !== 'left' && turnDirection !== 'right') {
console.log('ignoring invalid turn', turnDirection);
}
switch (robot.dir) {
case 'up':
robot.dir = turnDirection === 'left' ? 'left' : 'right';
break;
case 'down':
robot.dir = turnDirection === 'left' ? 'right' : 'left';
break;
case 'left':
robot.dir = turnDirection === 'left' ? 'down' : 'up';
break;
case 'right':
robot.dir = turnDirection === 'left' ? 'up' : 'down';
break;
}
}
}

render();

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


render();

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

})();