-
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: Kayla H. & Mac P. #58
base: main
Are you sure you want to change the base?
Changes from all commits
bd986ba
4112cb2
62df9d3
540c0ab
b38de5f
2ce7b31
d4d5d8f
173dc2f
66ad9d5
66fa54f
5c20889
119362b
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 |
---|---|---|
|
@@ -25,3 +25,5 @@ yarn-error.log* | |
|
||
.vscode | ||
|
||
# so I can git pull | ||
yarn.lock |
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,45 +21,113 @@ const generateSquares = () => { | |
currentId += 1; | ||
} | ||
} | ||
|
||
return squares; | ||
}; | ||
|
||
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 [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 onSquareClickCallback = (id) => { | ||
const boardCopy = squares.map((row) => { | ||
row.forEach((column) => { | ||
if (column.id === id) { | ||
if (column.value === '') { | ||
if (player === PLAYER_1) { | ||
column.value = PLAYER_1; | ||
} else { | ||
column.value = PLAYER_2; | ||
} | ||
} | ||
} | ||
}); | ||
return row; | ||
}); | ||
setSquares(boardCopy); | ||
if (player === PLAYER_1) { | ||
setPlayer(PLAYER_2); | ||
} else { | ||
setPlayer(PLAYER_1); | ||
} | ||
Comment on lines
+47
to
+52
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. Good job updating state. |
||
|
||
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. Consider additional logic that prevents the game from continuing once someone has won. |
||
// 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 r = 0; | ||
let c = 0; | ||
let winner = '...'; | ||
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. Consider having this be a top-level const like |
||
|
||
// checking for descending diagnol winner | ||
if ( | ||
squares[r][c].value === squares[r + 1][c + 1].value && | ||
squares[r + 1][c + 1].value === squares[r + 2][c + 2].value && | ||
squares[r][c].value != '' | ||
) { | ||
winner = squares[r][c].value; | ||
setWinner(winner); | ||
} | ||
|
||
// checking for ascending diagnol winner | ||
if ( | ||
squares[r][c + 2].value === squares[r + 1][c + 1].value && | ||
squares[r + 1][c + 1].value === squares[r + 2][c].value && | ||
squares[r][c].value != '' | ||
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. Make sure to use strict inequality |
||
) { | ||
winner = squares[r][c + 2].value; | ||
setWinner(winner); | ||
} | ||
|
||
// checking for horizontal winner | ||
while (r < 3) { | ||
if ( | ||
squares[r][c].value === squares[r][c + 1].value && | ||
squares[r][c + 1].value === squares[r][c + 2].value && | ||
squares[r][c].value != '' | ||
) { | ||
winner = squares[r][c].value; | ||
setWinner(winner); | ||
return winner; | ||
Comment on lines
+90
to
+91
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. Unclear why some winning lines return the winner and others do not. |
||
} | ||
r += 1; | ||
} | ||
// checking for vertical winner | ||
while (c < 3) { | ||
r = 0; | ||
if ( | ||
squares[r][c].value === squares[r + 1][c].value && | ||
squares[r + 1][c].value === squares[r + 2][c].value && | ||
squares[r][c].value != '' | ||
) { | ||
winner = squares[r][c].value; | ||
setWinner(winner); | ||
return winner; | ||
} | ||
c += 1; | ||
} | ||
}; | ||
|
||
const resetGame = () => { | ||
// Complete in Wave 4 | ||
const boardCopy = squares.map((row) => { | ||
row.forEach((column) => { | ||
column.value = ''; | ||
}); | ||
return row; | ||
}); | ||
Comment on lines
+112
to
+117
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! |
||
setSquares(boardCopy); | ||
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> | ||
<h2>Winner is {winner} </h2> | ||
<button onClick={resetGame}>Reset Game</button> | ||
</header> | ||
<main> | ||
<Board squares={squares} /> | ||
<Board squares={squares} onClickCallback={onSquareClickCallback} /> | ||
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. Good job passing callback! |
||
</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.
Nice use of map!