-
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
Scissors - Araceli #63
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import React, { useState } from 'react'; | ||
import React, { useState, useEffect } from 'react'; | ||
import './App.css'; | ||
|
||
import Board from './components/Board'; | ||
import Square from './components/Square'; | ||
|
||
const PLAYER_1 = 'X'; | ||
const PLAYER_2 = 'O'; | ||
|
||
|
||
const generateSquares = () => { | ||
const squares = []; | ||
|
||
|
@@ -27,45 +29,105 @@ 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()); | ||
const [player, setPlayer] = useState(PLAYER_1); | ||
// const [result, setResult] = useState({winner: 'none', state: 'none'}) | ||
|
||
|
||
// useEffect(() => { | ||
// checkForWinner(); | ||
// }, [squares]); | ||
|
||
// useEffect(() => { | ||
// if (result.state != 'none') { | ||
// alert(`Game Finished! Winning Player: ${result.winner}`); | ||
// } | ||
// }, [result]); | ||
|
||
const changePlayers = () => { | ||
if (player === PLAYER_1) { | ||
setPlayer(PLAYER_2) | ||
} else { | ||
setPlayer(PLAYER_1) | ||
} | ||
}; | ||
|
||
const markSquares = (squareId) => { | ||
|
||
// 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 | ||
for (let row = 0; row < 3; row +=1) { | ||
for (let col = 0; col < 3; col += 1) { | ||
if (squares[row][col].id===squareId && squares[row][col].value=='') { | ||
squares[row][col].value=player; | ||
}; | ||
}; | ||
}; | ||
setSquares(squares); | ||
changePlayers(); | ||
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. All of the logic in here works! With some minor changes it can be updated to update the UI (and pass the tests):
The last piece is to add a check at the top of markSquares that returns if a winner has been found so that squares can not continue to be marked. |
||
// 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. | ||
|
||
} | ||
// columns | ||
for (let index = 0; index < 3; index++) { | ||
if (squares[0][index].value===squares[1][index].value && | ||
squares[1][index].value===squares[2][index].value && | ||
squares[0][index].value!=='') { | ||
alert(`Game Finished! Winning Player: ${player}`); | ||
resetGame(); | ||
Comment on lines
+75
to
+79
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 chunk of code is repeated for rows and diagonals, the only changes are which points are being checked. I recommend thinking about creating a helper function that does this comparison function to improve readability. |
||
}; | ||
}; | ||
|
||
// rows | ||
for (let index = 0; index < 3; index++) { | ||
if (squares[index][0].value===squares[index][1].value && | ||
squares[index][1].value===squares[index][2].value && | ||
squares[index][0].value!=='') { | ||
alert(`Game Finished! Winning Player: ${player}`); | ||
resetGame(); | ||
}; | ||
}; | ||
|
||
// diagonals | ||
if (squares[0][0].value===squares[1][1].value && | ||
squares[1][1].value===squares[2][2].value && | ||
squares[0][0].value!=='') { | ||
alert(`Game Finished! Winning Player: ${player}`); | ||
resetGame(); | ||
}; | ||
|
||
if (squares[0][2].value===squares[1][1].value && | ||
squares[1][1].value===squares[2][0].value && | ||
squares[0][2].value!=='') { | ||
alert(`Game Finished! Winning Player: ${player}`); | ||
resetGame(); | ||
}; | ||
|
||
// check if tie | ||
// possibly try to use a counter? | ||
|
||
}; | ||
|
||
|
||
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. 👍 |
||
// Complete in Wave 4 | ||
} | ||
setSquares(generateSquares()); | ||
setPlayer(PLAYER_1); | ||
}; | ||
|
||
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... {checkForWinner} </h2> | ||
<button onClick={resetGame}>Reset Game</button> | ||
</header> | ||
<main> | ||
<Board squares={squares} /> | ||
<Board squares={squares} onClickCallback={markSquares} /> | ||
</main> | ||
</div> | ||
); | ||
} | ||
}; | ||
|
||
export default App; |
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.
Great helper function!