Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 54 additions & 10 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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);

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.

// 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 = () => {

Choose a reason for hiding this comment

The 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:

squares[i][0].value === squares[i][1].value and squares[i][0].value === squares[i][2].value

Then do something similar for the columns. The diagonals you have to do manually.

// Complete in Wave 3
Expand All @@ -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]
// }
// }

// })
// })


}

Expand All @@ -62,7 +106,7 @@ const App = () => {
<button>Reset Game</button>
</header>
<main>
<Board squares={squares} />
<Board squares={squares} onClickCallback = {squareId}/>
</main>
</div>
);
Expand Down
19 changes: 12 additions & 7 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>)

Choose a reason for hiding this comment

The 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 = {
Expand Down
13 changes: 5 additions & 8 deletions src/components/Square.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import PropTypes from 'prop-types';
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>
const handleClick = () => {
props.onClickCallback(props.id)
}

return <button className="square" onClick={handleClick}>{props.value}</button>
}

Square.propTypes = {
Expand Down