forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Class5 tic tac toe #7
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
aguevara1
wants to merge
5
commits into
gh-pages
Choose a base branch
from
class5-ticTacToe
base: gh-pages
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -12,9 +12,11 @@ let board = [ | |
| [' ', ' ', ' '] | ||
| ]; | ||
|
|
||
| //Global variable | ||
| let playerTurn = 'X'; | ||
|
|
||
| function printBoard() { | ||
| //join() joins all elements of array into a string and prints each element of the board | ||
| const printBoard= () => { | ||
| console.log(' 0 1 2'); | ||
| console.log('0 ' + board[0].join(' | ')); | ||
| console.log(' ---------'); | ||
|
|
@@ -23,27 +25,86 @@ function printBoard() { | |
| console.log('2 ' + board[2].join(' | ')); | ||
| } | ||
|
|
||
| function horizontalWin() { | ||
| // Your code here | ||
| /* function checks for horizontal wins. Returns true if some() function gets at least 1 element of horizontal | ||
| array to equal true for the 3 cases. If doesn't get one to return true will return falsey*/ | ||
| const horizontalWin= () => { | ||
| const horizontal = [0, 1, 2]; | ||
| let result = horizontal.some(indices => { | ||
| return board[indices][0] == playerTurn && board[indices][1] == playerTurn && board[indices][2] == playerTurn | ||
| }); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| function verticalWin() { | ||
| // Your code here | ||
|
|
||
| /* function checks for vertical wins. Returns true if some() function gets at least 1 element of vertical | ||
| array to equal true for the 3 cases. If doesn't get one to return true will return falsey*/ | ||
| const verticalWin= () => { | ||
| const vertical = [0, 1, 2]; | ||
| let result1 = vertical.some(indx => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be const, also just return the function, this isn't necessary |
||
| return board[0][indx] == playerTurn && board[1][indx] == playerTurn && board[2][indx] == playerTurn | ||
| }); | ||
|
|
||
| return result1; | ||
|
|
||
| } | ||
|
|
||
| function diagonalWin() { | ||
| // Your code here | ||
|
|
||
| /* function checking for the 2 ways to win diagonally. */ | ||
| const diagonalWin= () => { | ||
| /* if center space is equal to playerTurn then check the other 2. | ||
| Will return true if either of the 2 sets will evaluate to true, if not will | ||
| return falsey. | ||
| */ | ||
| if (board[1][1] == playerTurn){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
| return board[0][0] == playerTurn && board[2][2] == playerTurn || | ||
| board[0][2] == playerTurn && board[2][0] == playerTurn; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function checkForWin() { | ||
| // Your code here | ||
|
|
||
| /* will call each of these functions and will return true if one | ||
| of these functions returns true | ||
| */ | ||
| const checkForWin= () => { | ||
|
|
||
| return verticalWin() || horizontalWin() || diagonalWin(); | ||
| } | ||
|
|
||
| function ticTacToe(row, column) { | ||
| // Your code here | ||
| /* check if input is valid. Input must be 1 of the possibleNumbers, if not return | ||
| falsey | ||
| */ | ||
| const isValid= (row1, column1) => { | ||
| const possibleNumbers = [0, 1, 2]; | ||
| return possibleNumbers.indexOf(parseInt(row1, 10)) !== -1 && possibleNumbers.indexOf(parseInt(column1, 10)) !== -1; | ||
|
|
||
| } | ||
|
|
||
| function getPrompt() { | ||
| /* function takes in user input from 0-2. Will check if either of the players wins the | ||
| game. Will console.log which player wins the game. */ | ||
| const ticTacToe= (row, column) => { | ||
| // check for valid input. If invalid input will cosole.log message to user to enter numbers | ||
| if (isValid(row, column)) { | ||
| board[row][column] = playerTurn; | ||
|
|
||
| /* If true console.log who won the game. If falsey, will execute else statement and | ||
| switch users */ | ||
| if (checkForWin()) { | ||
| console.log(playerTurn + ' Has Won The Game!!!!'); | ||
| return true; | ||
| } else { | ||
| playerTurn === 'X' ? playerTurn = 'O' : playerTurn = 'X'; | ||
| } | ||
|
|
||
| } else { | ||
| console.log("Enter numbers 0-2 only!!!!!!!!"); | ||
| } | ||
|
|
||
| } | ||
| /* function prompts user to enter coordinates on board. Function will call ticTacToe function and then | ||
| call the prompt function(itself) again. */ | ||
| const getPrompt= () => { | ||
| printBoard(); | ||
| console.log("It's Player " + playerTurn + "'s turn."); | ||
| rl.question('row: ', (row) => { | ||
|
|
@@ -55,31 +116,49 @@ function getPrompt() { | |
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| // Tests | ||
| // Test cases | ||
|
|
||
| if (typeof describe === 'function') { | ||
|
|
||
| describe('#ticTacToe()', () => { | ||
| it('should place mark on the board', () => { | ||
| ticTacToe(1, 1); | ||
| assert.deepEqual(board, [ [' ', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]); | ||
| assert.deepEqual(board, [ | ||
| [' ', ' ', ' '], | ||
| [' ', 'X', ' '], | ||
| [' ', ' ', ' '] | ||
| ]); | ||
| }); | ||
| it('should alternate between players', () => { | ||
| ticTacToe(0, 0); | ||
| assert.deepEqual(board, [ ['O', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]); | ||
| assert.deepEqual(board, [ | ||
| ['O', ' ', ' '], | ||
| [' ', 'X', ' '], | ||
| [' ', ' ', ' '] | ||
| ]); | ||
| }); | ||
| it('should check for vertical wins', () => { | ||
| board = [ [' ', 'X', ' '], [' ', 'X', ' '], [' ', 'X', ' '] ]; | ||
| board = [ | ||
| [' ', 'X', ' '], | ||
| [' ', 'X', ' '], | ||
| [' ', 'X', ' '] | ||
| ]; | ||
| assert.equal(verticalWin(), true); | ||
| }); | ||
| it('should check for horizontal wins', () => { | ||
| board = [ ['X', 'X', 'X'], [' ', ' ', ' '], [' ', ' ', ' '] ]; | ||
| board = [ | ||
| ['X', 'X', 'X'], | ||
| [' ', ' ', ' '], | ||
| [' ', ' ', ' '] | ||
| ]; | ||
| assert.equal(horizontalWin(), true); | ||
| }); | ||
| it('should check for diagonal wins', () => { | ||
| board = [ ['X', ' ', ' '], [' ', 'X', ' '], [' ', ' ', 'X'] ]; | ||
| board = [ | ||
| ['X', ' ', ' '], | ||
| [' ', 'X', ' '], | ||
| [' ', ' ', 'X'] | ||
| ]; | ||
| assert.equal(diagonalWin(), true); | ||
| }); | ||
| it('should detect a win', () => { | ||
|
|
||
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.
should be const, also just return the function, this isn't necessary