Skip to content
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
15,299 changes: 15,299 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
.App {
text-align: center;
background-color: pink;
}

.App-header {
background-color: #282c34;
background-color: #faf6fa;
min-height: 10vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
color: rgb(77, 4, 58);
margin-bottom: 2em;
}

61 changes: 51 additions & 10 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,77 @@ const generateSquares = () => {
}

const App = () => {

const [squares, setSquares] = useState(generateSquares());
const [xTurn, setxTurn] = useState(PLAYER_1);

const onClickCallback = (id) => {
if (checkForWinner() !== null) return;
const updatedSquares = [...squares];

// 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 i = 0; i < 3; i++){
for (let j = 0; j < 3; j++) {
let thisSquare = updatedSquares[i][j];
if (thisSquare.id === id) {
if (thisSquare.value !== '') return;
thisSquare.value = xTurn? PLAYER_1 : PLAYER_2;
setxTurn(!xTurn);
}
}
}

console.log(checkForWinner());
setSquares(updatedSquares);

}

const checkForWinner = () => {
// Complete in Wave 3
let square = squares.flat().map(square => ({id:square.id, value:square.value }))
const winningBoards = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];

for (let i = 0; i < winningBoards.length; i++) {
const [a, b, c] = winningBoards[i];
if (square[a].value && square[a].value === square[b].value && square[a].value === square[c].value) {
let player = "";
square[a].value === 'X' ? player = 'X\'s' : player = 'O"s';
return player;
}
}

if ( square.every((square) => square.value !== "")) {
return "CAT"

Choose a reason for hiding this comment

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

most people would just use true and false but this is fun! Haha

}
return null;
}

const resetGame = () => {
// Complete in Wave 4
setSquares(generateSquares());
setxTurn(true);
}

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 className="reset" onClick={resetGame} >Reset Game </button>
</header>
<main>
<Board squares={squares} />
<Board squares={squares}
onClickCallback = {onClickCallback}
/>
</main>
</div>
);

}

export default App;
6 changes: 3 additions & 3 deletions src/components/Board.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ div.grid {
width: 600px;
height: 600px;
margin: 0 auto;
background-color: #34495e;
color: #fff;
border: 6px solid #2c3e50;
background-color: #e722dd;
color: rgb(19, 2, 2);
border: 6px solid #440244;
border-radius: 10px;
display: grid;
grid-template: repeat(3, 1fr) / repeat(3, 1fr);
Expand Down
26 changes: 21 additions & 5 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@ import PropTypes from 'prop-types';


const generateSquareComponents = (squares, onClickCallback) => {
// Complete this for Wave 1
const squaresArray = [];

squares.forEach((row) => {
row.forEach((square)=> {

squaresArray.push(
<Square
key={square.id}
id={square.id}
value={square.value}
onClickCallback={onClickCallback}
/>
);
})
});

return squaresArray;
}

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
2 changes: 1 addition & 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 #440244;
border-radius: 2px;
font-family: Helvetica;
font-weight: bold;
Expand Down
20 changes: 10 additions & 10 deletions src/components/Square.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ 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 Square = ({value, id, onClickCallback}) => {
return (
<button
className="square"
onClick={() => onClickCallback(id)}
>
{value}
</button>
);
}


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