This commit is contained in:
edwardbrodski 2023-09-21 23:03:16 +01:00
parent 82492e10d9
commit 455cf5ee63

View File

@ -1,6 +1,6 @@
import React from 'react'; import React from "react";
import { useState } from 'react'; import { useState } from "react";
import './style.css'; import "./style.css";
function TicTacToe() { function TicTacToe() {
const [gridState, setGridState] = useState(Array(9).fill(0)); const [gridState, setGridState] = useState(Array(9).fill(0));
@ -20,7 +20,7 @@ function TicTacToe() {
const newState = playerState ? 2 : 1; const newState = playerState ? 2 : 1;
updateGridState(index, newState); updateGridState(index, newState);
const winner = checkWinCondition() const winner = checkWinCondition();
if (winner) { if (winner) {
localWinState = winner; localWinState = winner;
setWinState(winner); setWinState(winner);
@ -31,16 +31,11 @@ function TicTacToe() {
} }
function checkWinCondition(): number { function checkWinCondition(): number {
function getWinningValue(a: number, b: number, c: number) {
function getWinningValue( if (a === b && b === c && a === c) {
a : number,
b : number,
c : number
) {
if ((a === b) && (b === c) && (a === c)) {
return a; return a;
} }
return 0 return 0;
} }
/* /*
* Diagonals * Diagonals
@ -81,9 +76,9 @@ function TicTacToe() {
const isDrawState = () => { const isDrawState = () => {
for (let i in gridState) { for (let i in gridState) {
if (!gridState[i]) return false; if (!gridState[i]) return false;
};
return true;
} }
return true;
};
if (isDrawState()) return 3; if (isDrawState()) return 3;
return 0; return 0;
@ -91,26 +86,26 @@ function TicTacToe() {
let stateString = ""; let stateString = "";
if (localWinState === 3) { if (localWinState === 3) {
stateString = `Game draw.` stateString = `Game draw.`;
} } else if (localWinState) {
else if (localWinState) {
stateString = `Player ${getStateChar(localWinState)} wins.`; stateString = `Player ${getStateChar(localWinState)} wins.`;
} } else {
else { stateString = `Player ${getStateChar(playerState ? 2 : 1)} turn.`;
stateString = `Player ${getStateChar(playerState ? 2 : 1)} turn.`
} }
return ( return (
<div className="TicTacToe"> <div className="TicTacToe">
<h1>{stateString}</h1> <h1>{stateString}</h1>
<div className="TicTacToeGrid"> <div className="TicTacToeGrid">
{ {gridState.map((item, index) => {
gridState.map((item, index) => { const getState = () => {
const getState = () => { return gridState[index] }; return gridState[index];
const callback = () => { playTurn(index) }; };
return (<TicTacToeCell getState={getState} callback={callback}/>); const callback = () => {
}) playTurn(index);
} };
return <TicTacToeCell getState={getState} callback={callback} />;
})}
</div> </div>
</div> </div>
); );
@ -122,13 +117,15 @@ interface CellProps {
} }
function TicTacToeCell({ getState, callback }: CellProps) { function TicTacToeCell({ getState, callback }: CellProps) {
function handleClick() { function handleClick() {
callback() callback();
} }
return ( return (
<div className={`TicTacToeCell ${getStateClass(getState())}`} onClick={handleClick}> <div
className={`TicTacToeCell ${getStateClass(getState())}`}
onClick={handleClick}
>
<p>{getStateChar(getState())}</p> <p>{getStateChar(getState())}</p>
</div> </div>
); );