diff --git a/week3/final/robot-text/robot.js b/week3/final/robot-text/robot.js index f2eb96d..10bd5ab 100644 --- a/week3/final/robot-text/robot.js +++ b/week3/final/robot-text/robot.js @@ -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(); @@ -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': @@ -62,7 +69,7 @@ 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; @@ -70,6 +77,11 @@ if (cellContents === 'F') { flagReached = true; } + if (cellContents === 'A') { + appleEaten = true; + applesEaten += 1; + console.log('\nYUM!'); + } } moves += 1; @@ -98,13 +110,14 @@ render(); + turn("right"); move(); - turn('right'); + turn("left"); move(); move(); move(); - turn('left'); move(); + turn("left"); move(); })();