-
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
11 changed files
with
217 additions
and
4 deletions.
There are no files selected for viewing
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
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
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/eslintReactTypescriptConfig.json"] | ||
"extends": ["../../lib/eslintReactTypescriptConfig.json", "../../lib/eslintBrowserConfig.json"], | ||
"overrides": [ | ||
{ | ||
"files": "./components/*.tsx", | ||
"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,72 @@ | ||
// External Imports | ||
import React from 'react'; | ||
|
||
// Internal Imports | ||
import { Square } from './Square.tsx'; | ||
|
||
// Local Functions | ||
function calculateWinner(squares: string[]) { | ||
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; | ||
} | ||
|
||
// Prop Types | ||
interface BoardProps { | ||
onPlay: (s: string[]) => void; | ||
squares: string[]; | ||
xIsNext: boolean; | ||
} | ||
|
||
// Component Definition | ||
function Board({ onPlay, squares, xIsNext }: BoardProps) { | ||
function handleClick(index: number) { | ||
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> | ||
</> | ||
); | ||
} | ||
|
||
// 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.tsx'; | ||
|
||
// 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) as string[]]); | ||
const [currentMove, setCurrentMove] = useState(0); | ||
const handlePlay = useCallback( | ||
(nextSquares: string[]) => { | ||
const nextHistory = [...history.slice(0, currentMove + 1), nextSquares]; | ||
setHistory(nextHistory); | ||
setCurrentMove(nextHistory.length - 1); | ||
}, | ||
[currentMove, history], | ||
); | ||
|
||
function jumpTo(nextMove: number) { | ||
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,20 @@ | ||
// External Imports | ||
import React from 'react'; | ||
|
||
// Prop Types | ||
interface SquareProps { | ||
onSquareClick: () => void; | ||
value: string; | ||
} | ||
|
||
// Component Definition | ||
function Square({ onSquareClick, value }: SquareProps) { | ||
return ( | ||
<button type="button" className="square" onClick={onSquareClick}> | ||
{value} | ||
</button> | ||
); | ||
} | ||
|
||
// Module Export | ||
export { Square }; |
16 changes: 16 additions & 0 deletions
16
test/eslintReactTypescriptConfig/exampleReactTypescriptConfigEntrypoint.tsx
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,16 @@ | ||
// Code based on this example: 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.tsx'; | ||
|
||
// Render the Application | ||
const rootElement = document.querySelector('#react-root'); | ||
if (!rootElement) { | ||
throw new TypeError('React root element not found'); | ||
} | ||
const reactRoot = createRoot(rootElement); | ||
reactRoot.render(<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