-
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
Sandra Caballero- Spruce #73
base: main
Are you sure you want to change the base?
Changes from all commits
5cb7505
c487dca
5c65394
7e58837
2cb6cfd
90b2bda
179750a
4a1462e
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 |
---|---|---|
|
@@ -21,8 +21,7 @@ | |
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"react-app", | ||
"react-app/jest" | ||
"react-app" | ||
] | ||
}, | ||
"browserslist": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { check } from 'prettier'; | ||
import React, { useState } from 'react'; | ||
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 = []; | ||
|
@@ -29,37 +30,103 @@ 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(PLAYER_1); | ||
// const [winner, setWinner] = useState(null); | ||
// const [gameEnd, setGameEnd] = useState(false); | ||
|
||
// Wave 2 | ||
const squareClicked =(id)=>{ | ||
let didPlay = false | ||
if (winner !== null) {return} | ||
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. 👍 |
||
const newState = squares.map(row =>row.map(pos =>{ | ||
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 (pos.id !== id) {return pos;} | ||
if (pos.value !== '') {return pos;} | ||
//we know the id matches | ||
didPlay = true | ||
const newPos = {...pos} | ||
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. 👍 |
||
newPos.value = currentPlayer | ||
return newPos | ||
})) | ||
if (didPlay) { | ||
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. 👍 |
||
setSquares(newState) | ||
if (currentPlayer === PLAYER_1) { | ||
setCurrentPlayer(PLAYER_2) | ||
} else {setCurrentPlayer(PLAYER_1)} | ||
} | ||
}; | ||
|
||
// let winner = checkForWinner(); | ||
// console.log(winner); | ||
// if (winner !== null){ | ||
// setWinner(winner); | ||
// setGameEnd(true); | ||
|
||
// break; | ||
// } | ||
// // 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 | ||
setSquares(generateSquares()); | ||
setCurrentPlayer(PLAYER_1); | ||
// setWinner(null); | ||
// setGameEnd(false); | ||
}; | ||
Comment on lines
111
to
116
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. 👍 |
||
|
||
const winner = checkForWinner() | ||
// console.log(winner) | ||
|
||
return ( | ||
<div className="App"> | ||
<header className="App-header"> | ||
<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> | ||
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. It would be nice to use conditional rendering here to avoid displaying the winner message until after the game is over. Alternatively, we could display the current player's turn here until the game has ended. We could also think about how to detect whether the game ended in a tie, and display a message to that effect. |
||
<button onClick={resetGame}>Reset Game</button> | ||
</header> | ||
<main> | ||
<Board squares={squares} /> | ||
<Board squares={squares} onClickCallback={squareClicked} /> | ||
</main> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,15 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import './Square.css' | ||
import './Square.css'; | ||
|
||
const Square = (props) => { | ||
// For Wave 1 enable this | ||
// Component to alert a parent | ||
// component when it's clicked on. | ||
|
||
return <button | ||
className="square" | ||
> | ||
{props.value} | ||
</button> | ||
} | ||
return <button onClick={()=> props.onClickCallback(props.id)} className='square'>{props.value}</button>; | ||
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 inline handler to call our click callback when the button was clicked. Notice that we can't simply use props.onClickCallback directly as the onClick handler, since we need to pass the id of the clicked square to the callback. If we had used it as the onClick directly, then the event details provided by the browser would have been passed in as the first parameter rather than the id. |
||
}; | ||
|
||
Square.propTypes = { | ||
value: PropTypes.string.isRequired, | ||
|
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.
Exactly. We don't need these pieces of state, since the winner can be calculated from the board, and whether the game is over can be determined from the winner. No additional state needed!