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
8 changes: 7 additions & 1 deletion tic-tac-toe-app/App.jsx
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>
Copy link

@Keyairra-S-Wright Keyairra-S-Wright May 2, 2020

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

<Board />
</div>
)

};

ReactDOM.render(<App />, document.querySelector('.container'));
87 changes: 87 additions & 0 deletions tic-tac-toe-app/Board.jsx
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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

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

Creating a renderSquare() helper function is a helpful way of reducing duplicate code. 👍 . To kick this reduction of code up a notch, consider refactoring by doing something such as mapping through the boardSquares in order to render the instances of <Square/>. This would remove the need to render each square separately.

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

Choose a reason for hiding this comment

The 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;
}
7 changes: 7 additions & 0 deletions tic-tac-toe-app/Square.jsx
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
Copy link

@Keyairra-S-Wright Keyairra-S-Wright May 3, 2020

Choose a reason for hiding this comment

The 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>
);
}
8 changes: 5 additions & 3 deletions tic-tac-toe-app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
</head>
<body>
<main class="container"></main>

<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.js"></script>
<script type="text/babel" src="app.jsx"></script>
<script type="text/babel" src="Board.jsx"></script>
<script type="text/babel" src="Square.jsx"></script>
<script type="text/babel" src="App.jsx"></script>
</body>
</html>
</html>
26 changes: 26 additions & 0 deletions tic-tac-toe-app/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.container{

Choose a reason for hiding this comment

The 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;
};