From 6260156b45429c53af7f5366b5bf104b21d8669c Mon Sep 17 00:00:00 2001 From: Kemirdin Date: Tue, 9 Jan 2018 23:03:16 +0100 Subject: [PATCH 1/4] file added --- Kemirdin.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Kemirdin.md diff --git a/Kemirdin.md b/Kemirdin.md new file mode 100644 index 0000000..0129b18 --- /dev/null +++ b/Kemirdin.md @@ -0,0 +1,2 @@ +Hello Teacher +could you please accept my pull request? From 16110541c2fd4bd8a75295c71b77010482ea116f Mon Sep 17 00:00:00 2001 From: Kemirdin Date: Fri, 12 Jan 2018 21:25:19 +0100 Subject: [PATCH 2/4] ESLint Rules --- week3/final/robot-text/.eslintrc.json | 46 +++++++++++++++++++++++++++ week3/final/robot-text/robot.js | 9 +++--- 2 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 week3/final/robot-text/.eslintrc.json diff --git a/week3/final/robot-text/.eslintrc.json b/week3/final/robot-text/.eslintrc.json new file mode 100644 index 0000000..0a15453 --- /dev/null +++ b/week3/final/robot-text/.eslintrc.json @@ -0,0 +1,46 @@ +{ + "env": { + "browser": true, + "commonjs": true, + "es6": true, + "node": true + }, + "parserOptions": { + "ecmaFeatures": { + "jsx": true + }, + "sourceType": "module" + }, + "extends": [ + "eslint:recommended" + ], + "rules": { + "no-const-assign": "warn", + "no-this-before-super": "warn", + "no-undef": "warn", + "no-unreachable": "warn", + "no-unused-vars": "warn", + "constructor-super": "warn", + "valid-typeof": "warn", + "no-var": "warn", + "prefer-const": "warn", + "no-multiple-empty-lines": "warn", + "eol-last": [ + "error", + "always" + ], + "no-console": "off", + "camelcase": "warn", + "eqeqeq": [ + "error", + "always", + { + "null": "ignore" + } + ], + "semi": [ + "warn", + "always" + ] + } +} \ No newline at end of file diff --git a/week3/final/robot-text/robot.js b/week3/final/robot-text/robot.js index f2eb96d..dd71b39 100644 --- a/week3/final/robot-text/robot.js +++ b/week3/final/robot-text/robot.js @@ -2,10 +2,11 @@ 'use strict'; const board = [ - ['T', 'T', '.', 'F'], - ['T', '.', '.', '.'], - ['.', '.', '.', '.'], - ['R', '.', '.', 'W'] + ['.', '.', '.', '.', '.'], + ['.', '.', '.', '.', '.'], + ['R', '.', '.', '.', 'F'], + ['.', 'A', 'A', 'A', '.'], + ['.', '.', '.', '.', '.'] ]; const robot = { From a4bdffb889a28643499d632ae79e88b1840d3644 Mon Sep 17 00:00:00 2001 From: Kemirdin Date: Fri, 12 Jan 2018 22:14:22 +0100 Subject: [PATCH 3/4] modified code --- week3/final/robot-text/robot.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/week3/final/robot-text/robot.js b/week3/final/robot-text/robot.js index dd71b39..6e5c10d 100644 --- a/week3/final/robot-text/robot.js +++ b/week3/final/robot-text/robot.js @@ -2,11 +2,11 @@ 'use strict'; const board = [ - ['.', '.', '.', '.', '.'], - ['.', '.', '.', '.', '.'], - ['R', '.', '.', '.', 'F'], - ['.', 'A', 'A', 'A', '.'], - ['.', '.', '.', '.', '.'] + ['.', 'F', '.', '.', '.'], + ['.', '.', 'A', '.', '.'], + ['A', '.', 'A', '.', '.'], + ['.', 'A', '.', '.', '.'], + ['R', '.', '.', '.', '.'] ]; const robot = { @@ -99,7 +99,6 @@ render(); - move(); turn('right'); move(); move(); @@ -107,5 +106,10 @@ turn('left'); move(); move(); + move(); + move(); + turn('left'); + move(); + move(); })(); From 259c29873c9a4f9ac47f420f820f59897bdff3e9 Mon Sep 17 00:00:00 2001 From: Kemirdin Date: Fri, 12 Jan 2018 23:17:32 +0100 Subject: [PATCH 4/4] modified code --- week3/final/robot-text/robot.js | 213 +++++++++++++++++--------------- 1 file changed, 111 insertions(+), 102 deletions(-) diff --git a/week3/final/robot-text/robot.js b/week3/final/robot-text/robot.js index 6e5c10d..e8bc4f8 100644 --- a/week3/final/robot-text/robot.js +++ b/week3/final/robot-text/robot.js @@ -1,115 +1,124 @@ -(function () { - 'use strict'; - - const board = [ - ['.', 'F', '.', '.', '.'], - ['.', '.', 'A', '.', '.'], - ['A', '.', 'A', '.', '.'], - ['.', 'A', '.', '.', '.'], - ['R', '.', '.', '.', '.'] - ]; - - 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!'); - } - } +'use strict'; - 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; - } +const board = [ + ['.', '.', '.', '.', '.'], + ['.', '.', '.', '.', '.'], + ['R', '.', '.', '.', 'F'], + ['.', 'A', 'A', 'A', '.'], + ['.', '.', '.', '.', '.'] +]; + +const robot = { + x: 0, + y: 2, + dir: 'right', +}; - const cellContents = board[y][x]; +let moves = 0; +let flagReached = false; +let appleEaten = false; +let applesEaten = 0; - 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; - } +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("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; + appleEaten = false; - moves += 1; - render(); + 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; } - function turn(turnDirection) { - if (turnDirection !== 'left' && turnDirection !== 'right') { - console.log('ignoring invalid turn', turnDirection); + const cellContents = board[y][x]; + + 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; } - 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; + if (cellContents === 'A') { + appleEaten = true; + applesEaten++; } + } + + 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; + } +} + +render(); - turn('right'); - move(); - move(); - move(); - turn('left'); - move(); - move(); - move(); - move(); - turn('left'); - move(); - move(); - -})(); +// start of robot game instructions +turn('down'); +move(); +turn('left'); +move(); +move(); +move(); +move(); +turn('left'); +move(); +// end of robot game instructions \ No newline at end of file