From c663ffd43f3997b377f1df94bc7265324875bd03 Mon Sep 17 00:00:00 2001 From: jamsrahimi Date: Fri, 12 Jan 2018 22:01:15 +0100 Subject: [PATCH] re-engineered 5 row robot game: Jams --- week3/final/robot-text/robot.js | 192 +++++++++++++++++--------------- 1 file changed, 102 insertions(+), 90 deletions(-) diff --git a/week3/final/robot-text/robot.js b/week3/final/robot-text/robot.js index f2eb96d..036096b 100644 --- a/week3/final/robot-text/robot.js +++ b/week3/final/robot-text/robot.js @@ -1,110 +1,122 @@ -(function () { - 'use strict'; +'use strict'; - const board = [ - ['T', 'T', '.', 'F'], - ['T', '.', '.', '.'], - ['.', '.', '.', '.'], - ['R', '.', '.', 'W'] +const board = [ + ['.', '.', '.', '.', '.'], + ['.', '.', '.', '.', '.'], + ['R', '.', '.', '.', 'F'], + ['.', 'A', 'A', 'A', '.'], + ['.', '.', '.', '.', '.'] ]; - const robot = { - x: 0, - y: 0, - dir: 'up', - }; +const robot = { + x: 0, + y: 2, + dir: 'right', +}; - let flagReached = false; - let moves = 0; +let moves = 0; +let flagReached = false; +let appleEaten = false; +let applesEaten = 0; - board.reverse(); +board.reverse(); - const trailIndicators = { - left: '←', - right: '→', - up: '↑', - down: '↓' - }; +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 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("YUM!"); + } + if (flagReached) { + console.log('\nHurray! Flag reached in ' + moves + ' steps!'); + console.log(applesEaten + " apples were eaten!"); } +} - function move() { - let x = robot.x; - let y = robot.y; +function move() { + let x = robot.x; + let y = robot.y; + appleEaten = false; - 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; - } + 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]; + const cellContents = board[y][x]; - 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; - } + if (cellContents === '.' || cellContents === 'A' || 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; } - - moves += 1; - render(); + if (cellContents === 'A') { + appleEaten = true; + applesEaten++; + } + } - 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; - } - } + moves += 1; render(); +} + +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; + } +} - move(); - turn('right'); - move(); - move(); - move(); - turn('left'); - move(); - move(); +render(); -})(); +turn('down'); +move(); +turn('left'); +move(); +move(); +move(); +move(); +turn('left'); +move();