-
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
Paperr--AndreaPalacios #65
base: master
Are you sure you want to change the base?
Changes from all commits
c75ebd6
bd67409
66ad43d
ab96af6
5249b0d
8a69892
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 = []; | ||
|
@@ -21,51 +21,121 @@ const generateSquares = () => { | |
currentId += 1; | ||
} | ||
} | ||
|
||
return squares; | ||
} | ||
|
||
/*initial State 3 rows | ||
[[{id: 0, value: ""}, {id: 1, value: ""}, {id: 2, value: ""}], | ||
[{id: 3, value: ""}, {id: 4, value: ""}, {id: 5, value: ""}], | ||
[{id: 6, value: ""}, {id: 7, value: ""}, {id: 8, value: ""}]] */ | ||
|
||
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 [status, setStatus] = useState('Let\'s play'); | ||
const [winner, setWinner] = useState(null); | ||
const [turnCount, setTurnCount]=useState(0) | ||
|
||
// 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 updateSquares=(id)=>{ | ||
if (winner !== null) return; | ||
|
||
const newSquares = [...squares]; | ||
let selectedSquare = newSquares.flat().find( newSquare =>newSquare.id ===id); | ||
|
||
if (selectedSquare.value !== '') return; | ||
selectedSquare.value = player; | ||
|
||
setSquares(newSquares); | ||
updateTurn(); | ||
updateStatus(); | ||
}; | ||
|
||
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 = () => {{ | ||
// vertical cases | ||
for (let i = 0; i < 3; i++) { | ||
if (squares[0][i].value === squares[1][i].value && | ||
squares[1][i].value === squares[2][i].value && | ||
squares[0][i].value !== '') { | ||
return squares[0][i].value | ||
Comment on lines
+60
to
+63
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. |
||
} | ||
} | ||
|
||
// horizontal cases | ||
for (let i = 0; i < 3; i++) { | ||
if (squares[i][0].value === squares[i][1].value && | ||
squares[i][1].value === squares[i][2].value && | ||
squares[i][0].value !== '') { | ||
return squares[i][0].value | ||
} | ||
} | ||
|
||
// diagonal cases | ||
if (squares[0][0].value === squares[1][1].value && | ||
squares[1][1].value === squares[2][2].value && | ||
squares[0][0].value !== '') { | ||
return squares[0][0].value | ||
} | ||
if (squares[0][2].value === squares[1][1].value && | ||
squares[1][1].value === squares[2][0].value && | ||
squares[0][2].value !== '') { | ||
return squares[0][2].value | ||
} | ||
} | ||
return null; | ||
}; | ||
|
||
/*update turn is working*/ | ||
const updateTurn = ()=> { | ||
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. 👍 |
||
if (player === PLAYER_1) { | ||
setPlayer(PLAYER_2); | ||
setTurnCount(turnCount+1); | ||
} else if (player === PLAYER_2) { | ||
setPlayer(PLAYER_1); | ||
setTurnCount(turnCount + 1); | ||
} | ||
}; | ||
|
||
|
||
const updateStatus= () => { | ||
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 helper function! |
||
const winner = (checkForWinner()) | ||
if (winner === null && turnCount === 8) { | ||
setStatus('Oh no...It\'s a tie!') | ||
} | ||
else if (winner === null) { | ||
setStatus(`Current Player is ${player}`) | ||
} | ||
else if (winner === 'x' || winner === 'o') { | ||
setStatus(`Winner is ${winner}`) | ||
} | ||
return winner | ||
} | ||
|
||
/*reset Game is passing*/ | ||
const resetGame = () => { | ||
// Complete in Wave 4 | ||
setSquares(generateSquares()); | ||
setPlayer(PLAYER_1); | ||
setStatus('Let\'s Play!'); | ||
setWinner(null); | ||
setTurnCount(0) | ||
Comment on lines
+119
to
+123
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 code re-use! |
||
} | ||
|
||
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>{status}</h2> | ||
<button onClick={resetGame}>Reset Game</button> | ||
</header> | ||
<main> | ||
<Board squares={squares} /> | ||
<Board squares={squares} onClickCallback={updateSquares} /> | ||
</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.
This works, but if you were attempting to use it to build a copy of squares, it doesn't quite do that. Consider the following:
In the above example, arrays a & b are using the same inner arrays. This isn't a problem in this particular case, but I just wanted to point out that the [...array] operator is just creating a copy of the outer array, not of the inner arrays.