-
Notifications
You must be signed in to change notification settings - Fork 0
Initial commit of Towers of Hanoi code #4
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
base: gh-pages
Are you sure you want to change the base?
Changes from 1 commit
44e675a
eb446d2
648d94f
4c6d0a1
7a97a30
2402344
27105cd
64467df
9506a6e
b66d0f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,44 +22,81 @@ function printBoard() { | |
| console.log(' ---------'); | ||
| console.log('2 ' + board[2].join(' | ')); | ||
| } | ||
|
|
||
| //code here | ||
| function horizontalWin() { | ||
| // if horizontal grid has equal items, there is a win | ||
|
|
||
| return board[0].every(square => square === playerTurn) || board[1].every(square => square === playerTurn) || board[2].every(square => square === playerTurn); | ||
| } | ||
|
|
||
| function verticalWin() { | ||
| // if vertical grid has equal items, there is a win | ||
|
|
||
| return [board[0][0], board[1][0], board[2][0]].every(square => square === playerTurn) || [board[0][1], board[1][1], board[2][1]].every(square => square === playerTurn) || [board[0][2], board[1][2], board[2][2]].every(square => square === playerTurn); | ||
| } | ||
|
|
||
| function diagonalWin() { | ||
| // if both diagonal grids have equal items, there is a win | ||
| return [board[0][0], board[1][1], board[2][2]].every(square => square === playerTurn) || [board[0][2], board[1][1], board[2][0]].every(square => square === playerTurn); | ||
| } | ||
|
|
||
| function checkForWin() { | ||
| // | ||
| if (horizontalWin()) { | ||
|
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. Consolidate your |
||
| printBoard(); | ||
| console.log(`Congratulations player ${playerTurn}. You won on the horizontal!`); | ||
| return true; | ||
| } else if (verticalWin()) { | ||
| printBoard(); | ||
| console.log(`Congratulations player ${playerTurn}. You won on the vertical!`); | ||
| return true; | ||
| } else if (diagonalWin()) { | ||
| printBoard(); | ||
| console.log(`Congratulations player ${playerTurn}. You won on the diagonal!`); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function ticTacToe(row, column) { | ||
| // Your code here | ||
| board[row][column] = playerTurn; | ||
|
|
||
| const validValue = (myIndex) => { | ||
| const valuesArr = [0,1,2]; | ||
| return valuesArr.some(validIndex => myIndex == validIndex); | ||
|
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. Good data scrub. |
||
| } | ||
|
|
||
| if (validValue(row) && validValue(column)) { | ||
| if (!board[row][column].trim() ) { | ||
| board[row][column] = playerTurn; | ||
|
|
||
| if (!checkForWin()) { | ||
| 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. Use a ternary operator to toggle the |
||
| playerTurn = 'O'; | ||
| } else { | ||
| playerTurn = 'X'; | ||
| } | ||
| return false; | ||
| } else { | ||
| console.log(`The winner is player ${playerTurn}. Start a new game`); | ||
| return true; | ||
| } | ||
| } else { | ||
| console.log('Please choose another square! That one is taken!'); | ||
| } | ||
| } else { | ||
| console.log('Please enter a valid index. Valid values are 0, 1, 2'); | ||
| } | ||
| } | ||
|
|
||
| function getPrompt() { | ||
| printBoard(); | ||
| console.log("It's Player " + playerTurn + "'s turn."); | ||
| rl.question('row: ', (row) => { | ||
| rl.question('column: ', (column) => { | ||
| ticTacToe(row, column); | ||
| getPrompt(); | ||
| if (!ticTacToe(row, column)) { | ||
| getPrompt(); | ||
| } else { | ||
| process.exit(0); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| // Tests | ||
|
|
||
| if (typeof describe === 'function') { | ||
|
|
||
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.
Nice use of the
every()js method here.