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

Scissors - Araceli #63

Open
wants to merge 1 commit 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
108 changes: 85 additions & 23 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import './App.css';

import Board from './components/Board';
import Square from './components/Square';

const PLAYER_1 = 'X';
const PLAYER_2 = 'O';


const generateSquares = () => {
const squares = [];

Expand All @@ -27,45 +29,105 @@ 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 [player, setPlayer] = useState(PLAYER_1);
// const [result, setResult] = useState({winner: 'none', state: 'none'})


// useEffect(() => {
// checkForWinner();
// }, [squares]);

// useEffect(() => {
// if (result.state != 'none') {
// alert(`Game Finished! Winning Player: ${result.winner}`);
// }
// }, [result]);

const changePlayers = () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great helper function!

if (player === PLAYER_1) {
setPlayer(PLAYER_2)
} else {
setPlayer(PLAYER_1)
}
};

const markSquares = (squareId) => {

// 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
for (let row = 0; row < 3; row +=1) {
for (let col = 0; col < 3; col += 1) {
if (squares[row][col].id===squareId && squares[row][col].value=='') {
squares[row][col].value=player;
};
};
};
setSquares(squares);
changePlayers();
checkForWinner();

};


const checkForWinner = () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the logic in here works! With some minor changes it can be updated to update the UI (and pass the tests):

  1. set a variable (ex: let winner = null) at the top of the function.
  2. If a winner is ever found, set winner equal to the player that has won (ex, on line 78 winner = squares[0][index].value).
  3. At the end of the function save the winner in state.
  4. Create a function that checks the winner state variable and returns a string that contains either "Winner is [winner]" or "The winner is..." and call the function on line 123 inside the

    tags.

The last piece is to add a check at the top of markSquares that returns if a winner has been found so that squares can not continue to be marked.

// 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.

}
// columns
for (let index = 0; index < 3; index++) {
if (squares[0][index].value===squares[1][index].value &&
squares[1][index].value===squares[2][index].value &&
squares[0][index].value!=='') {
alert(`Game Finished! Winning Player: ${player}`);
resetGame();
Comment on lines +75 to +79

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This chunk of code is repeated for rows and diagonals, the only changes are which points are being checked. I recommend thinking about creating a helper function that does this comparison function to improve readability.

};
};

// rows
for (let index = 0; index < 3; index++) {
if (squares[index][0].value===squares[index][1].value &&
squares[index][1].value===squares[index][2].value &&
squares[index][0].value!=='') {
alert(`Game Finished! Winning Player: ${player}`);
resetGame();
};
};

// diagonals
if (squares[0][0].value===squares[1][1].value &&
squares[1][1].value===squares[2][2].value &&
squares[0][0].value!=='') {
alert(`Game Finished! Winning Player: ${player}`);
resetGame();
};

if (squares[0][2].value===squares[1][1].value &&
squares[1][1].value===squares[2][0].value &&
squares[0][2].value!=='') {
alert(`Game Finished! Winning Player: ${player}`);
resetGame();
};

// check if tie
// possibly try to use a counter?

};


const resetGame = () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// Complete in Wave 4
}
setSquares(generateSquares());
setPlayer(PLAYER_1);
};

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>The winner is... {checkForWinner} </h2>
<button onClick={resetGame}>Reset Game</button>
</header>
<main>
<Board squares={squares} />
<Board squares={squares} onClickCallback={markSquares} />
</main>
</div>
);
}
};

export default App;
4 changes: 2 additions & 2 deletions src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('App', () => {
expect(buttons[buttonIndex].innerHTML).toEqual(expectedResult);
}

describe.skip('Wave 2: clicking on squares and rendering App', () => {
describe('Wave 2: clicking on squares and rendering App', () => {

test('App renders with a board of 9 empty buttons', () => {
// Arrange-Act - Render the app
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('App', () => {
});


describe.skip('Wave 3: Winner tests', () => {
describe('Wave 3: Winner tests', () => {
describe('Prints "Winner is x" when x wins', () => {
test('that a winner will be identified when 3 Xs get in a row across the top', () => {
// Arrange
Expand Down
2 changes: 1 addition & 1 deletion src/components/Board.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ div.grid {
width: 600px;
height: 600px;
margin: 0 auto;
background-color: #34495e;
background-color: #634783;
color: #fff;
border: 6px solid #2c3e50;
border-radius: 10px;
Expand Down
19 changes: 17 additions & 2 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,30 @@ const generateSquareComponents = (squares, onClickCallback) => {
// you need to return a 1D array
// of square components

}

// displays 9 square objects on the board
let squaresOnBoard = []
for (let i = 0; i < squares.length; i++) {
squaresOnBoard = squaresOnBoard.concat(squares[i])
}

return squaresOnBoard.map((square) => {
return <Square
key={square.id}
id={square.id}
value={square.value}
onClickCallback={onClickCallback}
/>
})
};

const Board = ({ squares, onClickCallback }) => {
const squareList = generateSquareComponents(squares, onClickCallback);
console.log(squareList);
return <div className="grid" >
{squareList}
</div>
}
};

Board.propTypes = {
squares: PropTypes.arrayOf(
Expand Down
3 changes: 2 additions & 1 deletion src/components/Square.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.square
{
border: 4px solid #2c3e50;
border: 4px solid #000000;
border-radius: 2px;
font-family: Helvetica;
font-weight: bold;
Expand All @@ -9,4 +9,5 @@
justify-content: center;
align-items: center;
margin: 2px;
cursor: pointer;
}
16 changes: 12 additions & 4 deletions src/components/Square.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ import PropTypes from 'prop-types';

import './Square.css'

// responsible for rendering squares on tic tac toe board - buttons

const Square = (props) => {
// For Wave 1 enable this
// Component to alert a parent
// component when it's clicked on.

return <button
const handleClick = () => {
props.onClickCallback(props.id);
}

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



Square.propTypes = {
value: PropTypes.string.isRequired,
Expand Down