From 8fb7ee29c5fa1ca98e2b0f4b8154e83889484faa Mon Sep 17 00:00:00 2001 From: anasmos Date: Sat, 13 Jan 2018 00:39:19 +0100 Subject: [PATCH] modified file bobot.js --- week3/final/robot-text/robot.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/week3/final/robot-text/robot.js b/week3/final/robot-text/robot.js index f2eb96d..07ef41e 100644 --- a/week3/final/robot-text/robot.js +++ b/week3/final/robot-text/robot.js @@ -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(); @@ -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': @@ -62,11 +69,15 @@ 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 === 'A') { + appleEaten = true; + applesEaten ++; + } if (cellContents === 'F') { flagReached = true; } @@ -98,13 +109,13 @@ render(); - move(); turn('right'); move(); + turn('left'); + move(); move(); move(); - turn('left'); move(); + turn('left'); move(); - })();