forked from AdaGold/react-tic-tac-toe
-
Notifications
You must be signed in to change notification settings - Fork 37
Kareha & Ana #16
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
anakp07
wants to merge
5
commits into
Ada-C14:master
Choose a base branch
from
anakp07:master
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
Kareha & Ana #16
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
247a5c7
generateSquareComponents wave1 tests passing
agesak 3cf099b
implemented updateSquares, SetPlayer, -- wave 2
285e6d8
Wave 2 tests passing
agesak f9550bd
row and cols working for winner
agesak 51163ae
finalized the checkforWinner function in wave 3 and implemented the r…
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,8 +3,8 @@ import './App.css'; | |
|
|
||
| import Board from './components/Board'; | ||
|
|
||
| const PLAYER_1 = 'X'; | ||
| const PLAYER_2 = 'O'; | ||
| const PLAYER_1 = 'x'; | ||
| const PLAYER_2 = 'o'; | ||
|
|
||
| const generateSquares = () => { | ||
| const squares = []; | ||
|
|
@@ -27,42 +27,86 @@ const generateSquares = () => { | |
|
|
||
| const App = () => { | ||
|
|
||
| // This starts state off as a 2D array of JS objects with | ||
| // empty value and unique ids. | ||
| const [squares, setSquares] = useState(generateSquares()); | ||
| const [player, setPlayer] = useState(PLAYER_1); | ||
| const [winner, setWinner] = useState(null); | ||
|
|
||
| // Wave 2 | ||
| // You will need to create a method to change the square | ||
| // When it is clicked on. | ||
| // Then pass it into the squares as a callback | ||
| const updateSquare = (squareId) => { | ||
| if (winner !== null) return; | ||
| const squaresNew = generateSquares(); | ||
| for (let row = 0; row < 3; row += 1) { | ||
| for (let col = 0; col < 3; col += 1) { | ||
| const square = squares[row][col]; | ||
| if (square.id === squareId && square.value === ''){ | ||
| squaresNew[row][col] = {id: squareId, value: player} | ||
| } else { | ||
| squaresNew[row][col] = {...square}; | ||
| } | ||
| } | ||
| } | ||
| setPlayer(player === PLAYER_1 ? PLAYER_2 : PLAYER_1); | ||
| setSquares(squaresNew); | ||
| setWinner(checkForWinner(squaresNew)); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| const checkForWinner = (squares) => { | ||
| for (let row = 0; row < 3; row += 1){ | ||
| const rowVals = []; | ||
| const colVals = []; | ||
| for (let col = 0; col < 3; col += 1){ | ||
| rowVals.push(squares[row][col].value); | ||
| colVals.push(squares[col][row].value); | ||
| if (winnerHelper(rowVals)) { | ||
| return winnerHelper(rowVals); | ||
|
Comment on lines
+61
to
+62
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. Do you need to call |
||
| } | ||
| if (winnerHelper(colVals)) { | ||
| return winnerHelper(colVals); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (squares[0][0].value === squares[1][1].value && squares[1][1].value === squares[2][2].value && squares[0][0].value !== | ||
| '') { | ||
| return squares[0][0].value; | ||
| } | ||
|
|
||
| const checkForWinner = () => { | ||
| // Complete in Wave 3 | ||
| // You will need to: | ||
| // 1. Go accross each row to see if | ||
| // 3 squares in the same row match | ||
| // i.e. same value | ||
| // 2. Go down each column to see if | ||
| // 3 squares in each column match | ||
| // 3. Go across each diagonal to see if | ||
| // all three squares have the same value. | ||
| if (squares[0][2].value === squares[1][1].value && squares[1][1].value === squares[2][0].value && squares[0][2].value !== | ||
| '') { | ||
| return squares[0][2].value; | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| const winnerHelper = (array) => { | ||
|
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 helper function! |
||
| if (array.length === 3) { | ||
| if (array.every(v => v !== '')) { | ||
| if (array.every(v => v === array[0])){ | ||
| return array[0]; | ||
| } | ||
| } | ||
| } | ||
| return null | ||
| } | ||
|
|
||
|
|
||
|
|
||
| const resetGame = () => { | ||
| // Complete in Wave 4 | ||
| setSquares(generateSquares()); | ||
| setPlayer(PLAYER_1); | ||
| setWinner(null); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="App"> | ||
| <header className="App-header"> | ||
| <h1>React Tic Tac Toe</h1> | ||
| <h2>The winner is ... -- Fill in for wave 3 </h2> | ||
| <button>Reset Game</button> | ||
| <h2>{winner === null ? `Current Player ${ player }` : `Winner is ${ winner }`}</h2> | ||
| <button onClick={resetGame}>Reset Game</button> | ||
| </header> | ||
| <main> | ||
| <Board squares={squares} /> | ||
| <Board squares={squares} onClickCallback={updateSquare} /> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
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
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
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
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
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
Oops, something went wrong.
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.
👍