-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
211 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
{ | ||
"root": true, | ||
"extends": ["../../lib/eslintReactConfig.json"] | ||
"extends": ["../../lib/eslintReactConfig.json", "../../lib/eslintBrowserConfig.json"], | ||
"overrides": [ | ||
{ | ||
"files": "./components/*.jsx", | ||
"rules": { "unicorn/filename-case": ["error", { "case": "pascalCase" }] } | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// External Imports | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
|
||
// Internal Imports | ||
import { Square } from './Square.jsx'; | ||
|
||
// Local Functions | ||
function calculateWinner(squares) { | ||
const lines = [ | ||
[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 (const [a, b, c] of lines) { | ||
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) { | ||
return squares[a]; | ||
} | ||
} | ||
// eslint-disable-next-line unicorn/no-null -- return an explicit null value | ||
return null; | ||
} | ||
|
||
// Component Definition | ||
function Board({ onPlay, squares, xIsNext }) { | ||
function handleClick(index) { | ||
if (calculateWinner(squares) || squares[index]) { | ||
return; | ||
} | ||
const nextSquares = [...squares]; | ||
nextSquares[index] = xIsNext ? 'X' : 'O'; | ||
onPlay(nextSquares); | ||
} | ||
|
||
const winner = calculateWinner(squares); | ||
const status = winner ? `Winner: ${winner}` : `Next player: ${xIsNext ? 'X' : 'O'}`; | ||
|
||
return ( | ||
<> | ||
<div className="status">{status}</div> | ||
<div className="board-row"> | ||
<Square value={squares[0]} onSquareClick={() => handleClick(0)} /> | ||
<Square value={squares[1]} onSquareClick={() => handleClick(1)} /> | ||
<Square value={squares[2]} onSquareClick={() => handleClick(2)} /> | ||
</div> | ||
<div className="board-row"> | ||
<Square value={squares[3]} onSquareClick={() => handleClick(3)} /> | ||
<Square value={squares[4]} onSquareClick={() => handleClick(4)} /> | ||
<Square value={squares[5]} onSquareClick={() => handleClick(5)} /> | ||
</div> | ||
<div className="board-row"> | ||
<Square value={squares[6]} onSquareClick={() => handleClick(6)} /> | ||
<Square value={squares[7]} onSquareClick={() => handleClick(7)} /> | ||
<Square value={squares[8]} onSquareClick={() => handleClick(8)} /> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
// Prop Types | ||
Board.propTypes = { | ||
onPlay: PropTypes.func.isRequired, | ||
squares: PropTypes.arrayOf(PropTypes.number).isRequired, | ||
xIsNext: PropTypes.bool.isRequired, | ||
}; | ||
|
||
// Module Exports | ||
export { Board }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// External Imports | ||
import React, { useCallback, useState } from 'react'; | ||
|
||
// Internal Imports | ||
import { Board } from './Board.jsx'; | ||
|
||
// Component Definition | ||
function Game() { | ||
// eslint-disable-next-line unicorn/no-null -- the game expects explicit null values | ||
const [history, setHistory] = useState([Array.from({ length: 9 }).fill(null)]); | ||
const [currentMove, setCurrentMove] = useState(0); | ||
const handlePlay = useCallback( | ||
(nextSquares) => { | ||
const nextHistory = [...history.slice(0, currentMove + 1), nextSquares]; | ||
setHistory(nextHistory); | ||
setCurrentMove(nextHistory.length - 1); | ||
}, | ||
[currentMove, history], | ||
); | ||
|
||
function jumpTo(nextMove) { | ||
setCurrentMove(nextMove); | ||
} | ||
|
||
const xIsNext = currentMove % 2 === 0; | ||
const currentSquares = history[currentMove]; | ||
|
||
const moves = history.map((squares, move) => { | ||
const description = move > 0 ? `Go to move #${move}` : 'Go to game start'; | ||
return ( | ||
// eslint-disable-next-line react/no-array-index-key -- this is the best unique value | ||
<li key={move}> | ||
<button type="button" onClick={() => jumpTo(move)}> | ||
{description} | ||
</button> | ||
</li> | ||
); | ||
}); | ||
|
||
return ( | ||
<div className="game"> | ||
<div className="game-board"> | ||
<Board xIsNext={xIsNext} squares={currentSquares} onPlay={handlePlay} /> | ||
</div> | ||
<div className="game-info"> | ||
<ol>{moves}</ol> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
// Module Exports | ||
export { Game }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// External Imports | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
|
||
// Component Definition | ||
function Square({ onSquareClick, value }) { | ||
return ( | ||
<button type="button" className="square" onClick={onSquareClick}> | ||
{value} | ||
</button> | ||
); | ||
} | ||
|
||
// Prop Types | ||
Square.propTypes = { | ||
onSquareClick: PropTypes.func.isRequired, | ||
value: PropTypes.string.isRequired, | ||
}; | ||
|
||
// Module Export | ||
export { Square }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Code adapted from here: https://react.dev/learn/tutorial-tic-tac-toe#what-are-you-building | ||
|
||
// External Imports | ||
import { createRoot } from 'react-dom/client'; | ||
import React from 'react'; | ||
|
||
// Internal Imports | ||
import { Game } from './components/Game.jsx'; | ||
|
||
// Render the Application | ||
const reactRoot = createRoot(document.querySelector('#react-root')); | ||
reactRoot.render(<Game />); |
This file was deleted.
Oops, something went wrong.