-
Notifications
You must be signed in to change notification settings - Fork 10
re-engineered 5 row robot game: Jams #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jamsrahimi
wants to merge
1
commit into
remarcmij:master
Choose a base branch
from
jamsrahimi:Jamshid-Robot
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this was a test with 'down'? When you submit a pull request make sure you take out all your debug code.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I tried to decode the concept behind the directions, it did not make sense. I did not notice the debug code was there. Wow I just notice what the direction mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the
rightandleftare relative turn directions.up,down,leftandrightare the absolute (compass) directions for the robot. Is this now your current understanding?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is my current understanding now. But the direction cannot change from → to ←? It can only turn to either left or right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turn left or right twice in a row?
See the diagram in the README. You did see that, did you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing the arrow keys in keyboard, as a 'confirmation bias' I had assumed to see a down or up turn. Now I understand it completely after seeing the README just now. I had seen it on your youtube recording.