-
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
Pine: Kris & Lia #60
base: main
Are you sure you want to change the base?
Pine: Kris & Lia #60
Changes from 10 commits
94077b6
a216ec3
2330ea4
7f53e1e
f734a1f
09eb1f7
2e0398b
69a4eb4
982992d
4e4fce1
95376ce
3a923c3
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 = []; | ||
|
@@ -17,6 +17,7 @@ const generateSquares = () => { | |
squares[row].push({ | ||
id: currentId, | ||
value: '', | ||
disabled: false, | ||
}); | ||
currentId += 1; | ||
} | ||
|
@@ -26,40 +27,109 @@ 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 [ticDict, setTicDict] = useState({}); | ||
const [turn, setTurn] = useState(PLAYER_1); | ||
const [winner, setWinner] = useState(null); | ||
// 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 toggleTurn = () => { | ||
let newTurn = ''; | ||
if (turn === PLAYER_1) { | ||
newTurn = PLAYER_2; | ||
} else { | ||
newTurn = PLAYER_1; | ||
} | ||
setTurn(newTurn); | ||
}; | ||
|
||
const onClickCallback = (squareID) => { | ||
const newSquares = [...squares]; | ||
newSquares.forEach((row) => { | ||
row.forEach((square) => { | ||
if (square.id === squareID && square.value === '') { | ||
square.value = turn; | ||
ticDict[squareID] = turn; | ||
} | ||
}); | ||
}); | ||
setSquares(newSquares); | ||
toggleTurn(); | ||
|
||
let someoneWon = checkForWinner(); | ||
if (someoneWon) { | ||
setWinner(someoneWon); | ||
squares.forEach((row) => { | ||
row.map((square) => { | ||
if (square.value == '') { | ||
square.disabled = true; | ||
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. Neat addition! |
||
} | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
const checkForWinner = () => { | ||
// Complete in Wave 3 | ||
// You will need to: | ||
// 1. Go accross each row to see if | ||
// 1. Go across 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. | ||
/* we need to update dict each click and then check | ||
|
||
012, 345, 678 horizontal winners | ||
036, 147, 258 vertical winners | ||
048, 246 diagonal winners */ | ||
|
||
let winner = null; | ||
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. There is an interesting bug in this function! The code fails the test that checks the bottom row of x's. The test setup goes as follows: 6 - x, 1 - o, 8 - x, 2 - o, 7 - x, which results in 2 o's in the top row and 3 x's in the bottom row. The if-else sequence to find the winner fails at this point, because line 94 is true! For values 3, 4 and 5, ticDict is undefined, so they are equal! As a result, winner is set to undefined. 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. Oh my goodness. Thank you for figuring this one out--we were stumped! 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. Haha, cool bug!!! Thanks for pointing it out :-) |
||
if (ticDict[0] == ticDict[1] && ticDict[1] == ticDict[2]) { | ||
winner = ticDict[0]; | ||
} else if (ticDict[3] == ticDict[4] && ticDict[4] == ticDict[5]) { | ||
winner = ticDict[3]; | ||
} else if (ticDict[6] == ticDict[7] && ticDict[6] == ticDict[8]) { | ||
winner = ticDict[6]; | ||
} else if (ticDict[0] == ticDict[3] && ticDict[3] == ticDict[6]) { | ||
winner = ticDict[0]; | ||
} else if (ticDict[1] == ticDict[4] && ticDict[4] == ticDict[7]) { | ||
winner = ticDict[1]; | ||
} else if (ticDict[2] == ticDict[5] && ticDict[5] == ticDict[8]) { | ||
winner = ticDict[2]; | ||
} else if (ticDict[0] == ticDict[4] && ticDict[4] == ticDict[8]) { | ||
winner = ticDict[0]; | ||
} else if (ticDict[2] == ticDict[4] && ticDict[4] == ticDict[6]) { | ||
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 is a long sequence of hardcoded checks - I recommend thinking about how you could shorten this with loops. |
||
winner = ticDict[2]; | ||
} else if (Object.keys(ticDict).length === 9) { | ||
winner = 'tie'; | ||
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! |
||
} | ||
return winner; | ||
}; | ||
|
||
const resetGame = () => { | ||
// Complete in Wave 4 | ||
setTicDict({}); | ||
setSquares(generateSquares()); | ||
setTurn(PLAYER_1); | ||
setWinner(null); | ||
}; | ||
|
||
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>Winner is {winner}</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.
This variable isn't being used correctly as a state variable. A state variable should be treated as if it is const at all times, and changes should be made through the corresponding set state function. To change a state var, make a copy, make the changes to the copy, and then call the set state function and pass in the changed copy. This can cause some issues if you want to use the new version right away, but you can bypass this by using the copied version (ie, pass the copied version to checkForWinner).
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.
Hi Jasmine, thank you for the feedback. Can you clarify which variable isn't being used correctly as a state variable here? Do you mean that we shouldn't be writing directly to ticDict? We should be writing to copy of ticDict and then using a set function to update it?