-
Notifications
You must be signed in to change notification settings - Fork 71
Rock - Priscille #51
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?
Rock - Priscille #51
Changes from all commits
e590139
1998767
a348a0d
1911f13
085ffb2
671e60f
a98c6fa
50f13e7
e1df316
71c0672
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import React, { useState } from 'react'; | |
| import './App.css'; | ||
|
|
||
| import Board from './components/Board'; | ||
| import Square from './components/Square'; | ||
|
|
||
| const PLAYER_1 = 'X'; | ||
| const PLAYER_2 = 'O'; | ||
|
|
@@ -27,42 +28,70 @@ 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()); | ||
|
|
||
| // 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 [currentPlayerX, setCurrentPlayerX] = useState(true); | ||
|
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 use of state to keep track of the current player. |
||
|
|
||
| const updateGameState = (id) => { | ||
|
|
||
| const game = squares.map((row) => { | ||
| return row.map((square) => { | ||
| if(id === square.id) { | ||
| if(square.value === '' && !checkForWinner(squares)){ | ||
| if(currentPlayerX){ | ||
| square.value = 'x'; | ||
| } else{ | ||
| square.value = 'o'; | ||
| } | ||
| setCurrentPlayerX(!currentPlayerX); | ||
| } | ||
| } | ||
| return square; | ||
| } ); | ||
|
|
||
| }); | ||
| setSquares(game); | ||
| }; | ||
|
|
||
| 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. | ||
|
|
||
| const checkForWinner = (squares) => { | ||
| // diagonals | ||
| if(squares[0][0].value && squares[0][0].value===squares[1][1].value && squares[0][0].value===squares[2][2].value){ | ||
| return squares[0][0].value; | ||
|
|
||
| } | ||
| if(squares[0][2].value && squares[0][2].value===squares[1][1].value && squares[0][2].value===squares[2][0].value){ | ||
| return squares[0][2].value; | ||
| } | ||
| // vertical | ||
| for(let i=0; i<3; i+=1){ | ||
| if(squares[0][i].value && squares[0][i].value===squares[1][i].value && squares[0][i].value===squares[2][i].value){ | ||
| return squares[0][i].value; | ||
| } | ||
| } | ||
| //horizontal | ||
| for(let i of squares) { | ||
| if(i[0].value && i[0].value===i[1].value && i[0].value===i[2].value){ | ||
| return i[0].value; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
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. There's something in the game logic here that is failing a test. Remind me to go over that with you during our next 1:1. 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. You're not checking for empty values in any of these combinations so I wonder if that's what is making one of the tests fail. |
||
|
|
||
| const resetGame = () => { | ||
| // Complete in Wave 4 | ||
| } | ||
|
|
||
| return ( | ||
| <div className="App"> | ||
| <header className="App-header"> | ||
| <h1>React Tic Tac Toe</h1> | ||
| <h2>The winner is ... -- Fill in for wave 3 </h2> | ||
| <h2>Winner is {checkForWinner(squares)}</h2> | ||
| <button>Reset Game</button> | ||
|
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. The reset button isn't quite working but here is a hint to get it to work:
|
||
| </header> | ||
| <main> | ||
| <Board squares={squares} /> | ||
| <Board | ||
| squares={squares} | ||
| onClickCallback ={updateGameState} | ||
| /> | ||
|
|
||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,4 +9,6 @@ | |
| justify-content: center; | ||
| align-items: center; | ||
| margin: 2px; | ||
| width: 100%; | ||
| height: 100%; | ||
| } | ||
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.
It seems like you aren't using these 2 variables anywhere in your project. You could probably leave them out?