-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
38 lines (30 loc) · 1.16 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import * as React from 'react';
import {useState} from 'react';
import Board from "./frontend/Components/Board/Board";
import {BoardPiece, GameState} from "./frontend/Types/types";
import {gameIsOver} from "./frontend/Utils/utils";
import GameOver from "./frontend/Components/GameOver/GameOver";
const App: React.FC = () => {
const boardSize = 3 * 3;
const [boardState, setBoardState] = useState<BoardPiece[]>(Array(boardSize).fill(BoardPiece.BLANK));
const updateSquareState = (index: number, newPiece: BoardPiece) => {
setBoardState((prevState) => {
return prevState.map((piece, idx) => {
return index === idx ? newPiece : piece
})
})
}
const updateBoardState = (boardState: GameState) => {
setBoardState(boardState.gameState);
}
const gameOver = gameIsOver(boardState)
return (
<>
<Board boardState={boardState} updateSquareState={updateSquareState} updateBoardState={updateBoardState}/> :
{gameOver.gameIsOver &&
<GameOver winner={gameOver.winner} boardState={boardState}/>
}
</>
)
}
export default App