-
Notifications
You must be signed in to change notification settings - Fork 10
carmen #4
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: master
Are you sure you want to change the base?
carmen #4
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 |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| const App = () => { | ||
| return <div>A tic-tac-toe game</div>; | ||
| return ( | ||
| <div> | ||
| <h1>A tic-tac-toe game</h1> | ||
| <Board /> | ||
| </div> | ||
| ) | ||
|
|
||
| }; | ||
|
|
||
| ReactDOM.render(<App />, document.querySelector('.container')); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // main board component | ||
| function Board() { | ||
| // creating state for the board squares | ||
| const [boardSquares, setBoardSquares] = React.useState( | ||
| new Array(9).fill(null) | ||
| ); | ||
|
|
||
| // state for the next turn | ||
| const [isXPlayer, setXPlayer] = React.useState(true); | ||
|
|
||
| // fucntion for when a sqaure is clicked | ||
| const whenClicked = index => { | ||
| // spread the items in boardSquare to new array to make a copy | ||
| const squares = [...boardSquares]; | ||
| // check if square has a value | ||
| if (squares[index]) return; | ||
| // add next turn | ||
| squares[index] = isXPlayer ? 'X' : 'O'; | ||
|
|
||
| // set the new state of the board squares | ||
|
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. The inline comments can help other developer better understand the intention of this code base. Groovy. 👍 |
||
| setBoardSquares(squares); | ||
|
|
||
| // change the state for the next turn | ||
| setXPlayer(!isXPlayer); | ||
| }; | ||
|
|
||
| // rendering the squares using square component | ||
| const renderSquare = index => { | ||
| return ( | ||
| <Square value={boardSquares[index]} onClick={() => whenClicked(index)} /> | ||
| ); | ||
| }; | ||
|
|
||
| // create the gameStatus to determine next player and winner | ||
| let gameStatus; | ||
| const winner = findWinner(boardSquares); | ||
| gameStatus = winner | ||
| ? `Winner is ${winner}!` | ||
| : `It is${isXPlayer ? 'X' : 'O'}'s Turn...'`; | ||
|
|
||
| return ( | ||
| <div className="board"> | ||
| <h2 className={gameStatus}>{gameStatus}</h2> | ||
| <div className="boardRow"> | ||
| {renderSquare(0)} | ||
| {renderSquare(1)} | ||
| {renderSquare(2)} | ||
| </div> | ||
| <div className="boardRow"> | ||
| {renderSquare(3)} | ||
| {renderSquare(4)} | ||
| {renderSquare(5)} | ||
| </div> | ||
| <div className="boardRow"> | ||
| {renderSquare(6)} | ||
| {renderSquare(7)} | ||
| {renderSquare(8)} | ||
|
Comment on lines
+44
to
+57
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. Creating a |
||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| // function to determine winner | ||
| const findWinner = (squares) => { | ||
| // winning combinations | ||
|
|
||
| const winningCombos = [ | ||
| [0, 1, 2], | ||
| [3, 4, 5], | ||
| [6, 7, 8], | ||
| [0, 3, 6], | ||
| [1, 4, 7], | ||
| [2, 5, 8], | ||
| [0, 4, 8], | ||
| [2, 4, 6], | ||
| ]; | ||
|
Comment on lines
+67
to
+76
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. Setting up the winning combinations makes the remainder of your code clearer. 💯 |
||
| for (let i = 0; i < winningCombos.length; i++) { | ||
| // destructure winningCombos[i] array | ||
| const [a, b, c] = winningCombos[i]; | ||
| // returning x or o if squares match | ||
| if (squares[a] && squares[a] === squares[b] && squares[a] === squares[b]) { | ||
| return squares[a]; | ||
| } | ||
| } | ||
| // else return null | ||
| return null; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| function Square(props) { | ||
| return ( | ||
| <div className="boardSquare" onClick={props.onClick}> | ||
| {props.value} | ||
|
Comment on lines
+3
to
+4
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. This is just a matter of preference: when using multiple props, the destructuring assignment can be used to help reduce code repetition. This is especially helpful in components with an increased number of props. |
||
| </div> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| .container{ | ||
|
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. The styles all reference the classnames you've assigned to elements. 💯 |
||
| display: flex; | ||
| justify-content: center; | ||
| text-align: center; | ||
|
|
||
| } | ||
| .boardSquare{ | ||
| background-color: #d5bef3; | ||
| border-style:solid; | ||
| height:200px; | ||
| width:200px; | ||
| }; | ||
|
|
||
| .board{ | ||
| display: flex; | ||
| flex-direction: row; | ||
| flex-wrap: wrap; | ||
| align-items: center; | ||
| } | ||
|
|
||
| .boardRow{ | ||
| display: flex; | ||
| flex-direction: row; | ||
| flex-wrap: wrap; | ||
| align-items: center; | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
The components in this code base are set up in a modular way. This separation of concerns keeps the code maintainable and accessible for other developers. 👍