forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
☕ First commit TicTacToe #10
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
GormanGit
wants to merge
1
commit 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
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 |
|---|---|---|
|
|
@@ -3,91 +3,155 @@ | |
| const assert = require('assert'); | ||
| const readline = require('readline'); | ||
| const rl = readline.createInterface({ | ||
| input: process.stdin, | ||
| output: process.stdout | ||
| input: process.stdin, | ||
| output: process.stdout | ||
| }); | ||
| let board = [ | ||
| [' ', ' ', ' '], | ||
| [' ', ' ', ' '], | ||
| [' ', ' ', ' '] | ||
| [' ', ' ', ' '], | ||
| [' ', ' ', ' '], | ||
| [' ', ' ', ' '] | ||
| ]; | ||
|
|
||
| let playerTurn = 'X'; | ||
| let playerOne = 'X'; | ||
| let playerTwo = 'O'; | ||
|
|
||
| function printBoard() { | ||
| console.log(' 0 1 2'); | ||
| console.log('0 ' + board[0].join(' | ')); | ||
| console.log(' ---------'); | ||
| console.log('1 ' + board[1].join(' | ')); | ||
| console.log(' ---------'); | ||
| console.log('2 ' + board[2].join(' | ')); | ||
| console.log(' 0 1 2'); | ||
| console.log('0 ' + board[0].join(' | ')); | ||
| console.log(' ---------'); | ||
| console.log('1 ' + board[1].join(' | ')); | ||
| console.log(' ---------'); | ||
| console.log('2 ' + board[2].join(' | ')); | ||
| } | ||
|
|
||
| function horizontalWin() { | ||
| // Your code here | ||
| return ((board[0][0] === playerOne && board[0][1] === playerOne && board[0][2] === playerOne) || (board[1][0] | ||
| === playerOne && board[1][1] === playerOne && board[1][2] === playerOne) || (board[2][0] === playerOne | ||
| && board[2][1] === playerOne && board[2][2] === playerOne) || (board[0][0] === playerTwo && board[0][1] | ||
| === playerTwo && board[0][2] === playerTwo) || (board[1][0] === playerTwo && board[1][1] === playerTwo && | ||
| board[1][2] === playerTwo) || (board[2][0] === playerTwo && board[2][1] === playerTwo && board[2][2] === playerTwo)) | ||
| } | ||
|
|
||
| function verticalWin() { | ||
| // Your code here | ||
| return ((board[0][0] === playerOne && board[1][0] === playerOne && board[2][0] === playerOne) || (board[0][1] | ||
| === playerOne && board[1][1] === playerOne && board[2][1] === playerOne) || (board[0][2] === playerOne | ||
| && board[1][2] === playerOne && board[2][2] === playerOne) || (board[0][0] === playerTwo && board[1][0] | ||
| === playerTwo && board[2][0] === playerTwo) || (board[0][1] === playerTwo && board[1][1] === playerTwo && | ||
| board[2][1] === playerTwo) || (board[0][2] === playerTwo && board[1][2] === playerTwo && board[2][2] === playerTwo)) | ||
| } | ||
|
|
||
| function diagonalWin() { | ||
| // Your code here | ||
| return ((board[0][0] === playerOne && board[1][1] === playerOne && board[2][2] === playerOne) || (board[2][0] | ||
| === playerOne && board[1][1] === playerOne && board[0][2] === playerOne) || (board[0][0] === playerTwo && board[1][1] | ||
| === playerTwo && board[2][2] === playerTwo) || (board[2][0] === playerTwo && board[1][1] === playerTwo && | ||
| board[0][2] === playerTwo)) | ||
|
|
||
| } | ||
|
|
||
| function checkForWin() { | ||
| // Your code here | ||
| return (diagonalWin() || verticalWin() || horizontalWin()) | ||
|
|
||
| } | ||
| // Check for a valid spot on the board | ||
| // Only accept intigers, 0-2 | ||
| //Check for empty space | ||
| // Not already filled with 'X' or 'O' | ||
| //If both are true then place a marker. | ||
| // Player one is “X” will start by entering a position based on the board and then go to player'O' | ||
| // This position will no longer be a valid move. | ||
| // If same position is tried a message will be given of “..........” | ||
| // Check for win | ||
| // Check for win horizontal | ||
| // check for win vertical | ||
| // Check diagonal win | ||
| // If no win then change player turn. | ||
| // if win. return You Won.. ==> reset board. | ||
| // | ||
|
|
||
| const userInputIsValid = (row, column) => { | ||
| return (row < 3 && row >= 0) && (column < 3 && column >= 0) | ||
| } | ||
|
|
||
| const isSpaceEmpty = (row, column) => { | ||
| const space = board[row][column]; | ||
| return (space !== 'X' && space !== 'O') | ||
| } | ||
| const placeAMarker = (row, column) => { | ||
| if (isSpaceEmpty(row, column) === true){ | ||
| return board[row][column] = playerTurn | ||
| // console.log(board) | ||
| }else { | ||
| console.log("This is not a valid play."); | ||
| } | ||
| } | ||
|
|
||
| function ticTacToe(row, column) { | ||
| // Your code here | ||
| if (userInputIsValid(row, column) && isSpaceEmpty(row, column)) { | ||
|
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. This is so solid, it's very readable, and it's right in your main function. |
||
| placeAMarker(row, column) | ||
| } | ||
| // if (isSpaceEmpty(row, column) === board['X', 'O']['X', 'O']){ | ||
| // board[row][column] = playerTurn; | ||
| // } else { | ||
| // console.log("This is not a valid play Player " + playerTurn + " choose another location on the board."); | ||
| // } | ||
| // if (checkForWin()){ | ||
| // console.log("Player" + playerTurn + " has just won the game!"); | ||
| // } | ||
| if (playerTurn === 'X') { | ||
|
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. This would be a great place for a ternary! |
||
| playerTurn = 'O'; | ||
| } else { | ||
| playerTurn = 'X'; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| //ticTacToe(); | ||
|
|
||
| function getPrompt() { | ||
| printBoard(); | ||
| console.log("It's Player " + playerTurn + "'s turn."); | ||
| rl.question('row: ', (row) => { | ||
| rl.question('column: ', (column) => { | ||
| ticTacToe(row, column); | ||
| getPrompt(); | ||
| printBoard(); | ||
| console.log("It's Player " + playerTurn + "'s turn."); | ||
| rl.question('row: ', (row) => { | ||
| rl.question('column: ', (column) => { | ||
| ticTacToe(row, column); | ||
| getPrompt(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| // Tests | ||
|
|
||
| if (typeof describe === 'function') { | ||
|
|
||
| describe('#ticTacToe()', () => { | ||
| it('should place mark on the board', () => { | ||
| ticTacToe(1, 1); | ||
| assert.deepEqual(board, [ [' ', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]); | ||
| }); | ||
| it('should alternate between players', () => { | ||
| ticTacToe(0, 0); | ||
| assert.deepEqual(board, [ ['O', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]); | ||
| }); | ||
| it('should check for vertical wins', () => { | ||
| board = [ [' ', 'X', ' '], [' ', 'X', ' '], [' ', 'X', ' '] ]; | ||
| assert.equal(verticalWin(), true); | ||
| }); | ||
| it('should check for horizontal wins', () => { | ||
| board = [ ['X', 'X', 'X'], [' ', ' ', ' '], [' ', ' ', ' '] ]; | ||
| assert.equal(horizontalWin(), true); | ||
| }); | ||
| it('should check for diagonal wins', () => { | ||
| board = [ ['X', ' ', ' '], [' ', 'X', ' '], [' ', ' ', 'X'] ]; | ||
| assert.equal(diagonalWin(), true); | ||
| }); | ||
| it('should detect a win', () => { | ||
| assert.equal(checkForWin(), true); | ||
| describe('#ticTacToe()', () => { | ||
| it('should place mark on the board', () => { | ||
| ticTacToe(1, 1); | ||
| assert.deepEqual(board, [[' ', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' ']]); | ||
| }); | ||
| it('should alternate between players', () => { | ||
| ticTacToe(0, 0); | ||
| assert.deepEqual(board, [['O', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' ']]); | ||
| }); | ||
| it('should check for vertical wins', () => { | ||
| board = [[' ', 'X', ' '], [' ', 'X', ' '], [' ', 'X', ' ']]; | ||
| assert.equal(verticalWin(), true); | ||
| }); | ||
| it('should check for horizontal wins', () => { | ||
| board = [['X', 'X', 'X'], [' ', ' ', ' '], [' ', ' ', ' ']]; | ||
| assert.equal(horizontalWin(), true); | ||
| }); | ||
| it('should check for diagonal wins', () => { | ||
| board = [['X', ' ', ' '], [' ', 'X', ' '], [' ', ' ', 'X']]; | ||
| assert.equal(diagonalWin(), true); | ||
| }); | ||
| it('should detect a win', () => { | ||
| assert.equal(checkForWin(), true); | ||
| }); | ||
| }); | ||
| }); | ||
| } else { | ||
|
|
||
| getPrompt(); | ||
| getPrompt(); | ||
|
|
||
| } | ||
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.
There's a lot of repetition in these win functions. Try to simplify the logic in these.