-
Notifications
You must be signed in to change notification settings - Fork 80
Alf - Spruce #71
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
base: main
Are you sure you want to change the base?
Alf - Spruce #71
Changes from all commits
5cb7505
c487dca
5c65394
7e58837
2cb6cfd
90b2bda
b801f04
55b5361
b760be7
7f21315
340c966
d1d6eaf
4ca200c
bf935fe
76d8f2d
58a2142
08f20c1
8c165b3
7bfb57d
b89c029
51857d7
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,8 +3,8 @@ import './App.css'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import Board from './components/Board'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const PLAYER_1 = 'X'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const PLAYER_2 = 'O'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const player1 = 'X'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const player2 = 'O'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const generateSquares = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const squares = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -30,36 +30,84 @@ const App = () => { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// empty value and unique ids. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const [squares, setSquares] = useState(generateSquares()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 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 [isX, setIsX] = useState(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const currentPlayer = !isX ? player1 : player2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 ternary! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const updateSquare = (id) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let newSquares = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (let row = 0; row < 3; row += 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
newSquares.push([]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for (let col = 0; col < 3; col += 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (id === squares[row][col].id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!squares[row][col].value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
squares[row][col].value = currentPlayer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setIsX(!isX); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
newSquares[row].push(squares[row][col]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setSquares(newSquares); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+38
to
+53
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. Great job in making a copy of the squares data! Here's a reminder on why we need to make the copy: To trigger the Instead, we should pass in a copy of our state data to the setState function 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. Rather than creating an empty list and pushing values into it with every iteration, we can make a shallow copy with the
Suggested change
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. I've also combined the conditional to reduce how nested this section of code looks. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const headerMessage = checkForWinner() ? `The winner is ${checkForWinner()}!!!` : `Current player: ${currentPlayer}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. Great ternary! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div className='App'> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<header className='App-header'> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<h1>TIC-TAC-TOE</h1> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<h2>{headerMessage}</h2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<button onClick={resetGame}>reset</button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</header> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<main> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Board squares={squares} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Board squares={squares} onClickCallback={checkForWinner() ? () => {} : updateSquare} /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. Clever use of a ternary operator to change the functionality of the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</main> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -3,29 +3,33 @@ import './Board.css'; | |||
import Square from './Square'; | ||||
import PropTypes from 'prop-types'; | ||||
|
||||
|
||||
// This turns the 2D array into a 1D array | ||||
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 singleArraySquares = [].concat(...squares); | ||||
return singleArraySquares.map((square) => { | ||||
return ( | ||||
<Square | ||||
value={square.value} | ||||
id={square.id} | ||||
onClickCallback={onClickCallback} | ||||
key={square.id} | ||||
/> | ||||
); | ||||
}); | ||||
}; | ||||
|
||||
const Board = ({ squares, onClickCallback }) => { | ||||
const squareList = generateSquareComponents(squares, onClickCallback); | ||||
console.log(squareList); | ||||
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. Generally, we should remove all console logs and print statements when submitting code for review. And our team should definitely take that advice as well, I believe this was meant to be removed before shipping to y'all.
Suggested change
|
||||
return <div className="grid" > | ||||
{squareList} | ||||
</div> | ||||
} | ||||
return <div className='grid'>{squareList}</div>; | ||||
}; | ||||
|
||||
Board.propTypes = { | ||||
squares: PropTypes.arrayOf( | ||||
PropTypes.arrayOf( | ||||
PropTypes.shape({ | ||||
id: PropTypes.number.isRequired, | ||||
value: PropTypes.string.isRequired | ||||
value: PropTypes.string.isRequired, | ||||
}) | ||||
) | ||||
), | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,33 @@ | ||
.square | ||
{ | ||
border: 4px solid #2c3e50; | ||
border: 3px solid black; | ||
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. LOVE the css hover effects! The overall color palette is super cute and portfolio ready ✨ |
||
border-radius: 2px; | ||
font-family: Helvetica; | ||
font-weight: bold; | ||
font-size: 8em; | ||
font-size: 5em; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
margin: 2px; | ||
} | ||
margin: 3px; | ||
color: black; | ||
} | ||
|
||
#blue:hover { | ||
background-color: rgb(186, 240, 253); | ||
} | ||
|
||
#pink:hover { | ||
background-color: rgb(255, 196, 250); | ||
} | ||
|
||
#yellow:hover { | ||
background-color: rgb(246, 255, 166); | ||
} | ||
|
||
#green:hover { | ||
background-color: rgb(150, 248, 188); | ||
} | ||
|
||
#orange:hover { | ||
background-color: rgb(255, 150, 112); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
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. | ||
let color = ''; | ||
|
||
if (props.id === 0 || props.id === 5) { | ||
color = 'blue'; | ||
} else if (props.id === 1 || props.id === 6) { | ||
color = 'yellow'; | ||
} else if (props.id === 2 || props.id === 7) { | ||
color = 'green'; | ||
} else if (props.id === 3 || props.id === 8) { | ||
color = 'pink'; | ||
} else if (props.id === 4) { | ||
color = 'orange'; | ||
} | ||
Comment on lines
+9
to
+19
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. Great use of the |
||
|
||
return <button | ||
className="square" | ||
> | ||
{props.value} | ||
</button> | ||
} | ||
const selectedSquare = () => { | ||
props.onClickCallback(props.id); | ||
}; | ||
|
||
return <button className='square' id={color} onClick={selectedSquare}>{props.value}</button>; | ||
}; | ||
|
||
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.
Annoyingly enough, our tests look for lowercase
'x'
and'o'
, which was our fault in not updating our tests.This was the reason why it appeared you failed tests, despite your project working as expected.