Compare commits
No commits in common. "6e479ea7450c2abb05f4cceb06b15e09ee6d3617" and "82492e10d91e7ee4a5ae5c4365fb43b6a01e9c3e" have entirely different histories.
6e479ea745
...
82492e10d9
@ -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));
|
||||
@ -20,7 +20,7 @@ function TicTacToe() {
|
||||
const newState = playerState ? 2 : 1;
|
||||
updateGridState(index, newState);
|
||||
|
||||
const winner = checkWinCondition();
|
||||
const winner = checkWinCondition()
|
||||
if (winner) {
|
||||
localWinState = winner;
|
||||
setWinState(winner);
|
||||
@ -31,11 +31,16 @@ function TicTacToe() {
|
||||
}
|
||||
|
||||
function checkWinCondition() : number {
|
||||
function getWinningValue(a: number, b: number, c: number) {
|
||||
if (a === b && b === c && a === c) {
|
||||
|
||||
function getWinningValue(
|
||||
a : number,
|
||||
b : number,
|
||||
c : number
|
||||
) {
|
||||
if ((a === b) && (b === c) && (a === c)) {
|
||||
return a;
|
||||
}
|
||||
return 0;
|
||||
return 0
|
||||
}
|
||||
/*
|
||||
* Diagonals
|
||||
@ -76,9 +81,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;
|
||||
@ -86,40 +91,45 @@ 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 state={gridState[index]} onClick={callback} />;
|
||||
})}
|
||||
{
|
||||
gridState.map((item, index) => {
|
||||
const getState = () => { return gridState[index] };
|
||||
const callback = () => { playTurn(index) };
|
||||
return (<TicTacToeCell getState={getState} callback={callback}/>);
|
||||
})
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
interface CellProps {
|
||||
state: number;
|
||||
onClick: () => void;
|
||||
getState: () => number;
|
||||
callback: () => void;
|
||||
}
|
||||
|
||||
function TicTacToeCell({ getState, callback }: CellProps) {
|
||||
|
||||
function handleClick() {
|
||||
callback()
|
||||
}
|
||||
|
||||
function TicTacToeCell({ state, onClick }: CellProps) {
|
||||
return (
|
||||
<div className={`TicTacToeCell ${getStateClass(state)}`} onClick={onClick}>
|
||||
<p>{getStateChar(state)}</p>
|
||||
<div className={`TicTacToeCell ${getStateClass(getState())}`} onClick={handleClick}>
|
||||
<p>{getStateChar(getState())}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user