-
Notifications
You must be signed in to change notification settings - Fork 80
Nicole W. : Maple #79
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: main
Are you sure you want to change the base?
Changes from all commits
5cb7505
c487dca
5c65394
7e58837
2cb6cfd
90b2bda
da9efa3
5942b33
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 |
|---|---|---|
|
|
@@ -21,8 +21,7 @@ | |
| }, | ||
| "eslintConfig": { | ||
| "extends": [ | ||
| "react-app", | ||
| "react-app/jest" | ||
| "react-app" | ||
| ] | ||
| }, | ||
| "browserslist": { | ||
|
|
||
| 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 = []; | ||
|
|
@@ -29,22 +29,82 @@ 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 [currentPlayer, setCurrentPlayer] = useState(PlAYER_1); | ||
| // Wave 2 | ||
| // 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 squareClick = (id) => { | ||
| let madeMove = false; | ||
| const updateState = squares.map(row => row.map(pos => { | ||
| if (pos.id !== id) {return pos;} | ||
| madeMove =true; | ||
| return {...pos, value: currentPlayer}; | ||
| })); | ||
| if (madeMove){ | ||
| setSquares(newState); | ||
| let newPlayer; | ||
| if (currentPlayer == PLAYER_1) { | ||
| newPlayer = PlAYER_2; | ||
|
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 should be |
||
| } | ||
| setCurrentPlayer(); | ||
| } | ||
| }; | ||
|
|
||
| // to map every id to the row and column on the array | ||
| // the row for any number is going to be this formula | ||
| // and the column is also this formula | ||
| const row = parseInt(id / 3); | ||
| const col = id - 3 * parseInt(id / 3); | ||
|
|
||
| // 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 | ||
|
|
||
| // will pass whoevers turn it is here | ||
|
|
||
| squares[row][col] = 'x'; | ||
|
|
||
| // change the state | ||
| setSquares(squares); | ||
|
Comment on lines
+57
to
+66
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. When you push your updates we went over this should be fixed. We remove this code because you were just testing some things out. |
||
| }; | ||
|
|
||
| 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. | ||
| let i = 0; | ||
|
|
||
| // Check all the rows and columns for a winner | ||
| while (i < 3) { | ||
| if ( | ||
| squares[i][0].value === squares[i][1].value && | ||
| squares[i][2].value === squares[i][1].value && | ||
| squares[i][0].value !== '' | ||
| ) { | ||
| return squares[i][0].value; | ||
| } else if ( | ||
| squares[0][i].value === squares[1][i].value && | ||
| squares[2][i].value === squares[1][i].value && | ||
| squares[0][i].value !== '' | ||
| ) { | ||
| return squares[0][i].value; | ||
| } | ||
| i += 1; | ||
| } | ||
| // Check Top-Left to bottom-right diagonal | ||
| if ( | ||
| squares[0][0].value === squares[1][1].value && | ||
| squares[2][2].value === squares[1][1].value && | ||
| squares[1][1].value !== '' | ||
| ) { | ||
| return squares[0][0].value; | ||
| } | ||
|
|
||
| // Check Top-right to bottom-left diagonal | ||
| if ( | ||
| squares[0][2].value === squares[1][1].value && | ||
| squares[2][0].value === squares[1][1].value && | ||
| squares[1][1].value !== '' | ||
| ) { | ||
| return squares[0][2].value; | ||
| } | ||
| // need to update this for ties | ||
| return null; | ||
| }; | ||
|
|
||
| const resetGame = () => { | ||
|
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. for the resetGame function you want to "reset" the values back to their default values. |
||
|
|
@@ -59,7 +119,7 @@ const App = () => { | |
| <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. add an onClick that will trigger the |
||
| </header> | ||
| <main> | ||
| <Board squares={squares} /> | ||
| <Board squares={squares} onClickCallback={squareClick} /> | ||
| </main> | ||
| </div> | ||
| ); | ||
|
|
||
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.
this should be
PLAYER_1