-
Notifications
You must be signed in to change notification settings - Fork 80
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
Spruce: Kelemen - Tic Tac Toe Digital starter #68
base: main
Are you sure you want to change the base?
Changes from all commits
5cb7505
c487dca
5c65394
7e58837
2cb6cfd
90b2bda
f2d977d
59ed805
c81c38b
ea23b2c
98dc308
4b4d853
957df4d
4ede68d
b3eb0bc
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 |
---|---|---|
|
@@ -14,3 +14,10 @@ | |
margin-bottom: 2em; | ||
} | ||
|
||
.green { | ||
color: rgb(199, 238, 199); | ||
} | ||
|
||
.red { | ||
color: rgb(187, 133, 133); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import React, { useState } from 'react'; | ||
import './App.css'; | ||
|
||
import Login from './components/Login'; | ||
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 = []; | ||
|
@@ -26,40 +26,120 @@ const generateSquares = () => { | |
}; | ||
|
||
const App = () => { | ||
// This starts state off as a 2D array of JS objects with | ||
// empty value and unique ids. | ||
// useState | ||
const [squares, setSquares] = useState(generateSquares()); | ||
const [player, setPlayer] = useState(PLAYER_1); | ||
|
||
const onClickCallback = (id) => { | ||
if (winner) { | ||
return; | ||
} | ||
|
||
// 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 | ||
let madeMove = false; | ||
const newState = squares.map((row) => | ||
row.map((pos) => { | ||
if (pos.id !== id) { | ||
return pos; | ||
} | ||
if (pos.value !== '') { | ||
return pos; | ||
} | ||
madeMove = true; | ||
return { ...pos, value: player }; | ||
}) | ||
); | ||
|
||
if (madeMove) { | ||
setSquares(newState); | ||
setPlayer(player === PLAYER_1 ? PLAYER_2 : PLAYER_1); | ||
} | ||
}; | ||
|
||
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; | ||
} | ||
|
||
return null; | ||
}; | ||
const nameColor = player === PLAYER_1 ? 'green' : 'red'; | ||
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 calculated value to get the class to apply to the status line. No state required! We can simply do the calculation in the body of our function before we reach the return. Components are functions, and we we can run whatever code we need to generate our tag hierarchy before we get to the return statement! |
||
|
||
// const playersList = () => ( | ||
// <div> | ||
// <h5>Players: {player1Name} vs. {player2Name}</h5> | ||
// </ | ||
// div> | ||
// ); | ||
|
||
{ | ||
/* const addPlayerNames = (newPlayers) => { | ||
newPlayersList(() => ( | ||
userName1: userName1, | ||
userName2: userName2, | ||
)); | ||
setFormFields(newPlayers); | ||
}; */ | ||
} | ||
|
||
const winner = checkForWinner(); | ||
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. Great. We can't make use of the checkForWinner (at least as written) during the click handler, since the state variable that it checks won't be updated until the subsequent render. But we can call it in the function body (which happens on each render) to see whether there is a winner, and then make adjustments to the tag structure we return based on the calculated winner (if any). |
||
const getStatus = () => { | ||
if (winner) { | ||
return `Winner is ${winner}`; | ||
} | ||
return `It is now ${player}'s turn`; | ||
}; | ||
|
||
const resetGame = () => { | ||
// Complete in Wave 4 | ||
setSquares(generateSquares()); | ||
setPlayer(PLAYER_1); | ||
}; | ||
Comment on lines
125
to
128
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. Yes. Resetting the game simply involves setting each of our state values back to their initial values. |
||
|
||
// ========= App rendered =========== | ||
|
||
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> | ||
<Login /> | ||
<h2 className={nameColor}>{getStatus()}</h2> | ||
|
||
<button onClick={() => resetGame()}>Reset Game</button> | ||
</header> | ||
<main> | ||
<Board squares={squares} /> | ||
<Board squares={squares} onClickCallback={onClickCallback} /> | ||
</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.
👍
We only want to switch players if there was a valid move made.