-
Notifications
You must be signed in to change notification settings - Fork 48
Space - Faezeh #28
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: master
Are you sure you want to change the base?
Space - Faezeh #28
Changes from all commits
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 |
|---|---|---|
|
|
@@ -25,34 +25,94 @@ const generateSquares = () => { | |
| return squares; | ||
| } | ||
|
|
||
|
|
||
| const App = () => { | ||
|
|
||
|
|
||
| const [squares, setSquares] = useState(generateSquares()); | ||
|
|
||
| const [player, setPlayer] = useState(PLAYER_1); | ||
| const [winner, setWinner] = useState("No one! it's a tie!!"); | ||
|
|
||
| // 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 onClickCallback = (id) => { | ||
| const newSquares = []; | ||
|
|
||
| for (let row of squares) { | ||
| const newRow = []; | ||
| newSquares.push(newRow); | ||
| for (let col of row) { | ||
| const newCol = {...col} | ||
| if (newCol.id === id) { | ||
| if (newCol.value !== "" || | ||
| winner === "Player 1" || | ||
| winner === "Player 2") { | ||
| return; | ||
| } | ||
| newCol.value = player | ||
| }; | ||
| newRow.push(newCol); | ||
| }; | ||
| }; | ||
|
|
||
| setSquares(newSquares); | ||
| checkForWinner(newSquares); | ||
|
|
||
| const newPlayer = (player === PLAYER_1) ? PLAYER_2 : PLAYER_1 | ||
| setPlayer(newPlayer); | ||
| } | ||
|
|
||
| const checkForWinner = () => { | ||
| const checkForWinner = (squares) => { | ||
| // Complete in Wave 3 | ||
| const values = squares.flat().map((square) => square.value); | ||
|
|
||
| const threeX = ['X','X','X']; | ||
| const threeO = ['O','O','O']; | ||
|
|
||
| if (arrayEquals([values[0],values[1],values[2]], threeX) || | ||
| arrayEquals([values[3],values[4],values[5]], threeX) || | ||
| arrayEquals([values[6],values[7],values[8]], threeX) || | ||
| arrayEquals([values[0],values[3],values[6]], threeX) || | ||
| arrayEquals([values[1],values[4],values[7]], threeX) || | ||
| arrayEquals([values[2],values[5],values[8]], threeX) || | ||
| arrayEquals([values[0],values[4],values[8]], threeX) || | ||
| arrayEquals([values[2],values[4],values[6]], threeX)) { | ||
| return setWinner("Player 1") | ||
| } else if (arrayEquals([values[0],values[1],values[2]], threeO) || | ||
| arrayEquals([values[3],values[4],values[5]], threeO) || | ||
| arrayEquals([values[6],values[7],values[8]], threeO) || | ||
| arrayEquals([values[0],values[3],values[6]], threeO) || | ||
| arrayEquals([values[1],values[4],values[7]], threeO) || | ||
| arrayEquals([values[2],values[5],values[8]], threeO) || | ||
| arrayEquals([values[0],values[4],values[8]], threeO) || | ||
| arrayEquals([values[2],values[4],values[6]], threeO)) { | ||
| return setWinner("Player 2") | ||
| } else { | ||
| return setWinner("No one! it's a tie!!") | ||
| }; | ||
| } | ||
|
|
||
| const arrayEquals = (one, two) => { | ||
| return JSON.stringify(one) === JSON.stringify(two); | ||
|
Comment on lines
+97
to
+98
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. I see you saw the post in Slack. nice! |
||
| } | ||
|
|
||
| const resetGame = () => { | ||
| // Complete in Wave 4 | ||
| window.location.reload(); | ||
| // found this on upmostly.com | ||
|
Comment on lines
+103
to
+104
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. That works, but you could also just do |
||
| } | ||
|
|
||
| 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>The winner is {winner} </h2> | ||
| <button onClick={resetGame}>Reset Game</button> | ||
| </header> | ||
| <main> | ||
| <Board squares={squares} /> | ||
| <Board squares={squares} onClickCallback={onClickCallback} /> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,27 @@ import Square from './Square'; | |
| import PropTypes from 'prop-types'; | ||
|
|
||
|
|
||
|
|
||
| const generateSquareComponents = (squares, onClickCallback) => { | ||
| // Complete this for Wave 1 | ||
|
|
||
| const squareBoxes = []; | ||
|
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 works, but you could also use the |
||
|
|
||
| for (let row of squares) { | ||
| for (let col of row) { | ||
| squareBoxes.push( | ||
| <Square | ||
| id={col.id} | ||
| value={col.value} | ||
| onClickCallback={onClickCallback} | ||
| key={col.id} | ||
| /> | ||
| ); | ||
| }; | ||
| }; | ||
|
|
||
| return squareBoxes; | ||
|
|
||
| } | ||
|
|
||
| const Board = ({ squares, onClickCallback }) => { | ||
|
|
||
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!