-
Notifications
You must be signed in to change notification settings - Fork 72
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
finished waves 1-3: rendered Board and Squares, added Click Funtional… #69
base: master
Are you sure you want to change the base?
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 = []; | ||||||||||
|
@@ -25,44 +25,101 @@ const generateSquares = () => { | |||||||||
return squares; | ||||||||||
} | ||||||||||
|
||||||||||
const cellMap = [ | ||||||||||
{x: 0, y: 0}, | ||||||||||
{x: 1, y: 0}, | ||||||||||
{x: 2, y: 0}, | ||||||||||
{x: 0, y: 1}, | ||||||||||
{x: 1, y: 1}, | ||||||||||
{x: 2, y: 1}, | ||||||||||
{x: 0, y: 2}, | ||||||||||
{x: 1, y: 2}, | ||||||||||
{x: 2, y: 2} | ||||||||||
] | ||||||||||
|
||||||||||
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); | ||||||||||
const [winner, setWinner] = useState('') | ||||||||||
// 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 = (event) => { | ||||||||||
const coordinates = cellMap[event.target.id] | ||||||||||
if(squares[coordinates.y][coordinates.x].value!==''||winner!==''){ | ||||||||||
return | ||||||||||
} | ||||||||||
let updatedSquares=[...squares] | ||||||||||
updatedSquares[coordinates.y][coordinates.x].value=currentPlayer | ||||||||||
setSquares(updatedSquares) | ||||||||||
setCurrentPlayer(currentPlayer===PLAYER_1? PLAYER_2: PLAYER_1) | ||||||||||
setWinner(checkForWinner()) | ||||||||||
|
||||||||||
} | ||||||||||
|
||||||||||
|
||||||||||
|
||||||||||
const 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. Just a note that this function doesn't determine ties. |
||||||||||
// 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 | ||||||||||
for(let y = 0; y< 3; y++){ | ||||||||||
let player=squares[y][0].value | ||||||||||
for(let x= 1; x<3; x++){ | ||||||||||
if(squares[y][x].value!==player){ | ||||||||||
Comment on lines
+74
to
+75
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. Indentation
Suggested change
|
||||||||||
player='' | ||||||||||
} | ||||||||||
} | ||||||||||
if(player!==''){ | ||||||||||
return player | ||||||||||
} | ||||||||||
} | ||||||||||
// 2. Go down each column to see if | ||||||||||
// 3 squares in each column match | ||||||||||
for(let x = 0; x<3; x++){ | ||||||||||
let player=squares[0][x].value | ||||||||||
for(let y = 0; y<3; y++){ | ||||||||||
if(squares[y][x].value!==player){ | ||||||||||
player='' | ||||||||||
} | ||||||||||
} | ||||||||||
if(player!==''){ | ||||||||||
return player | ||||||||||
} | ||||||||||
} | ||||||||||
// 3. Go across each diagonal to see if | ||||||||||
// all three squares have the same value. | ||||||||||
|
||||||||||
if (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[1][1].value && squares[0][2].value === squares[2][0].value){ | ||||||||||
return squares[0][2].value | ||||||||||
} | ||||||||||
return '' | ||||||||||
} | ||||||||||
|
||||||||||
const resetGame = () => { | ||||||||||
// Complete in Wave 4 | ||||||||||
setSquares(generateSquares()) | ||||||||||
setCurrentPlayer(PLAYER_1) | ||||||||||
setWinner('') | ||||||||||
} | ||||||||||
|
||||||||||
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> | ||||||||||
{winner!==''&&<h2> Winner is {winner} </h2>} | ||||||||||
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 tricky JS syntax |
||||||||||
<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.
Interesting solution!