-
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
Adah's #72
base: main
Are you sure you want to change the base?
Adah's #72
Changes from all commits
5cb7505
c487dca
5c65394
7e58837
2cb6cfd
ebb8598
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 player1 = 'X'; | ||
const player2 = 'O'; | ||
|
||
const generateSquares = () => { | ||
const squares = []; | ||
|
@@ -29,40 +29,115 @@ 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(player1); | ||
const [winner, setWinner] = useState(null); | ||
|
||
// let currentPlayer = player_1; | ||
// let winner = 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 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. | ||
let i = 0; | ||
|
||
// Check all the rows and columns for a winner | ||
while (i < 3) { | ||
if ( | ||
squares[i][0].value === squares[i][1].value && | ||
squares[i][2].value === squares[i][1].value && | ||
squares[i][0].value !== '' | ||
) { | ||
return squares[i][0].value; | ||
} else if ( | ||
squares[0][i].value === squares[1][i].value && | ||
squares[2][i].value === squares[1][i].value && | ||
squares[0][i].value !== '' | ||
) { | ||
return squares[0][i].value; | ||
} | ||
i += 1; | ||
} | ||
// Check Top-Left to bottom-right diagonal | ||
if ( | ||
squares[0][0].value === squares[1][1].value && | ||
squares[2][2].value === squares[1][1].value && | ||
squares[1][1].value !== '' | ||
) { | ||
return squares[0][0].value; | ||
} | ||
|
||
// Check Top-right to bottom-left diagonal | ||
if ( | ||
squares[0][2].value === squares[1][1].value && | ||
squares[2][0].value === squares[1][1].value && | ||
squares[1][1].value !== '' | ||
) { | ||
return squares[0][2].value; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const resetGame = () => { | ||
// Complete in Wave 4 | ||
let resetBoard = generateSquares(); | ||
setCurrentPlayer(player1); | ||
setWinner(null); | ||
setSquares(resetBoard); | ||
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. you can just say |
||
}; | ||
|
||
const onClickCallback = (id) => { | ||
console.log('onclick', id); | ||
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. remove console log after debugging |
||
setSquares((squares) => { | ||
let newBoard = squares.map((square) => { | ||
for (let property of square) { | ||
if (property.id ===id && property.value === '') { | ||
if (currentPlayer === player1) { | ||
property.value = player1; | ||
} else if (currentPlayer === player2) { | ||
property.value = player2; | ||
} | ||
} | ||
} | ||
|
||
return square; | ||
}); | ||
|
||
setWinner(checkForWinner()); | ||
return newBoard; | ||
}); | ||
|
||
if (currentPlayer === player1) { | ||
setCurrentPlayer(player2); | ||
} else { | ||
setCurrentPlayer(player1); | ||
} | ||
}; | ||
|
||
let header; | ||
let boardCallback; | ||
if (winner != null) { | ||
header = <h2>Winner is {winner}</h2>; | ||
} else { | ||
header = <h2>The Current Player is {currentPlayer}</h2>; | ||
boardCallback = onClickCallback; | ||
} | ||
|
||
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> | ||
{header} | ||
<button onClick={resetGame}>Reset Game</button> | ||
</header> | ||
<main> | ||
<Board squares={squares} /> | ||
<Board onClickCallback={boardCallback} squares={squares} /> | ||
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. here you can just reference the function in the curly braces instead of making a separate variable. So |
||
</main> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; | ||
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.
remove unused code