-
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
Paper, Ruthie Newman #45
base: master
Are you sure you want to change the base?
Changes from all commits
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,9 @@ 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 = []; | ||
|
@@ -27,15 +28,30 @@ 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 [currentPlayer, setPlayerState] = useState(PLAYER_1); | ||
|
||
// 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 squareId = (id) => { | ||
setPlayerState(!currentPlayer); | ||
const player = currentPlayer ? PLAYER_1 : PLAYER_2; | ||
|
||
squares.forEach(row=>{ | ||
row.forEach(square => { | ||
if (square.id === id) { | ||
if(square.value === '') | ||
if (player === PLAYER_1) { | ||
console.log(PLAYER_1) | ||
square.value = 'x' | ||
}else{ | ||
console.log(PLAYER_2) | ||
square.value = 'o' | ||
} | ||
}else if(square.value != ''){ | ||
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. Just noting this function is still essentially blank. I suggest making a loop that goes through the rows checking to see if:
Then do something similar for the columns. The diagonals you have to do manually. |
||
// Complete in Wave 3 | ||
|
@@ -47,6 +63,34 @@ const App = () => { | |
// 3 squares in each column match | ||
// 3. Go across each diagonal to see if | ||
// all three squares have the same value. | ||
|
||
//possible approach to calculating squares | ||
|
||
// const options = [ | ||
// [0,1,2], | ||
// [3,4,5], | ||
// [6,7,8], | ||
// [0,3,6], | ||
// [1,4,7], | ||
// [2,5,8], | ||
// [6,4,2], | ||
// [0,4,8] | ||
// ] | ||
|
||
// squares.forEach(row=>{ | ||
// row.forEach(square=>{ | ||
// for(let i = 0; i < options.length; i++) { | ||
// const [a, b, c] = options[i]; | ||
// console.log(a) | ||
// if (square[a].value && square[a].value === square[b].value && square[a] === square[c]) { | ||
// console.log(square[a]) | ||
// return square[a] | ||
// } | ||
// } | ||
|
||
// }) | ||
// }) | ||
|
||
|
||
} | ||
|
||
|
@@ -62,7 +106,7 @@ const App = () => { | |
<button>Reset Game</button> | ||
</header> | ||
<main> | ||
<Board squares={squares} /> | ||
<Board squares={squares} onClickCallback = {squareId}/> | ||
</main> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,24 @@ import PropTypes from 'prop-types'; | |
|
||
|
||
const generateSquareComponents = (squares, onClickCallback) => { | ||
// Complete this for Wave 1 | ||
// squares is a 2D Array, but | ||
// you need to return a 1D array | ||
// of square components | ||
|
||
|
||
const flatList = [] | ||
console.log(squares) | ||
squares.forEach(row=>{ | ||
row.forEach(square =>{ | ||
flatList.push(square) | ||
}) | ||
}); | ||
|
||
const squareComponents = flatList.map(data => <Square id={data.id} value={data.value} onClickCallback={onClickCallback} ></Square>) | ||
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. 👍 |
||
return squareComponents | ||
} | ||
|
||
const Board = ({ squares, onClickCallback }) => { | ||
const squareList = generateSquareComponents(squares, onClickCallback); | ||
console.log(squareList); | ||
return <div className="grid" > | ||
{squareList} | ||
</div> | ||
return <div className="grid">{squareList}</div> | ||
} | ||
|
||
Board.propTypes = { | ||
|
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.
I also suggest you might want a state variable to keep track of the winner.