refactor
This commit is contained in:
parent
82492e10d9
commit
455cf5ee63
@ -1,6 +1,6 @@
|
||||
import React from 'react';
|
||||
import { useState } from 'react';
|
||||
import './style.css';
|
||||
import React from "react";
|
||||
import { useState } from "react";
|
||||
import "./style.css";
|
||||
|
||||
function TicTacToe() {
|
||||
const [gridState, setGridState] = useState(Array(9).fill(0));
|
||||
@ -8,19 +8,19 @@ function TicTacToe() {
|
||||
const [winState, setWinState] = useState(0);
|
||||
let localWinState = winState;
|
||||
|
||||
function updateGridState(index : number, state : number) {
|
||||
function updateGridState(index: number, state: number) {
|
||||
gridState[index] = state;
|
||||
setGridState(gridState);
|
||||
}
|
||||
|
||||
function playTurn(index : number) {
|
||||
function playTurn(index: number) {
|
||||
if (gridState[index]) return;
|
||||
if (winState) return;
|
||||
|
||||
const newState = playerState ? 2 : 1;
|
||||
updateGridState(index, newState);
|
||||
|
||||
const winner = checkWinCondition()
|
||||
const winner = checkWinCondition();
|
||||
if (winner) {
|
||||
localWinState = winner;
|
||||
setWinState(winner);
|
||||
@ -30,17 +30,12 @@ function TicTacToe() {
|
||||
setPlayerState(!playerState);
|
||||
}
|
||||
|
||||
function checkWinCondition() : number {
|
||||
|
||||
function getWinningValue(
|
||||
a : number,
|
||||
b : number,
|
||||
c : number
|
||||
) {
|
||||
if ((a === b) && (b === c) && (a === c)) {
|
||||
function checkWinCondition(): number {
|
||||
function getWinningValue(a: number, b: number, c: number) {
|
||||
if (a === b && b === c && a === c) {
|
||||
return a;
|
||||
}
|
||||
return 0
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
* Diagonals
|
||||
@ -81,9 +76,9 @@ function TicTacToe() {
|
||||
const isDrawState = () => {
|
||||
for (let i in gridState) {
|
||||
if (!gridState[i]) return false;
|
||||
};
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
if (isDrawState()) return 3;
|
||||
|
||||
return 0;
|
||||
@ -91,26 +86,26 @@ function TicTacToe() {
|
||||
|
||||
let stateString = "";
|
||||
if (localWinState === 3) {
|
||||
stateString = `Game draw.`
|
||||
}
|
||||
else if (localWinState) {
|
||||
stateString = `Game draw.`;
|
||||
} else if (localWinState) {
|
||||
stateString = `Player ${getStateChar(localWinState)} wins.`;
|
||||
}
|
||||
else {
|
||||
stateString = `Player ${getStateChar(playerState ? 2 : 1)} turn.`
|
||||
} else {
|
||||
stateString = `Player ${getStateChar(playerState ? 2 : 1)} turn.`;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="TicTacToe">
|
||||
<h1>{stateString}</h1>
|
||||
<div className="TicTacToeGrid">
|
||||
{
|
||||
gridState.map((item, index) => {
|
||||
const getState = () => { return gridState[index] };
|
||||
const callback = () => { playTurn(index) };
|
||||
return (<TicTacToeCell getState={getState} callback={callback}/>);
|
||||
})
|
||||
}
|
||||
{gridState.map((item, index) => {
|
||||
const getState = () => {
|
||||
return gridState[index];
|
||||
};
|
||||
const callback = () => {
|
||||
playTurn(index);
|
||||
};
|
||||
return <TicTacToeCell getState={getState} callback={callback} />;
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
@ -122,19 +117,21 @@ interface CellProps {
|
||||
}
|
||||
|
||||
function TicTacToeCell({ getState, callback }: CellProps) {
|
||||
|
||||
function handleClick() {
|
||||
callback()
|
||||
callback();
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={`TicTacToeCell ${getStateClass(getState())}`} onClick={handleClick}>
|
||||
<div
|
||||
className={`TicTacToeCell ${getStateClass(getState())}`}
|
||||
onClick={handleClick}
|
||||
>
|
||||
<p>{getStateChar(getState())}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function getStateChar(state : number) {
|
||||
function getStateChar(state: number) {
|
||||
switch (state) {
|
||||
case 1:
|
||||
return "O";
|
||||
@ -145,7 +142,7 @@ function getStateChar(state : number) {
|
||||
}
|
||||
}
|
||||
|
||||
function getStateClass(state : number) {
|
||||
function getStateClass(state: number) {
|
||||
switch (state) {
|
||||
case 1:
|
||||
return "CellRed";
|
||||
|
Loading…
Reference in New Issue
Block a user