Compare commits
No commits in common. "6e479ea7450c2abb05f4cceb06b15e09ee6d3617" and "82492e10d91e7ee4a5ae5c4365fb43b6a01e9c3e" have entirely different histories.
6e479ea745
...
82492e10d9
@ -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));
|
||||||
@ -8,19 +8,19 @@ function TicTacToe() {
|
|||||||
const [winState, setWinState] = useState(0);
|
const [winState, setWinState] = useState(0);
|
||||||
let localWinState = winState;
|
let localWinState = winState;
|
||||||
|
|
||||||
function updateGridState(index: number, state: number) {
|
function updateGridState(index : number, state : number) {
|
||||||
gridState[index] = state;
|
gridState[index] = state;
|
||||||
setGridState(gridState);
|
setGridState(gridState);
|
||||||
}
|
}
|
||||||
|
|
||||||
function playTurn(index: number) {
|
function playTurn(index : number) {
|
||||||
if (gridState[index]) return;
|
if (gridState[index]) return;
|
||||||
if (winState) return;
|
if (winState) return;
|
||||||
|
|
||||||
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);
|
||||||
@ -30,12 +30,17 @@ function TicTacToe() {
|
|||||||
setPlayerState(!playerState);
|
setPlayerState(!playerState);
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkWinCondition(): number {
|
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 a;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* Diagonals
|
* Diagonals
|
||||||
@ -76,9 +81,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;
|
||||||
@ -86,45 +91,50 @@ 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 {
|
}
|
||||||
stateString = `Player ${getStateChar(playerState ? 2 : 1)} turn.`;
|
else {
|
||||||
|
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) => {
|
{
|
||||||
const getState = () => {
|
gridState.map((item, index) => {
|
||||||
return gridState[index];
|
const getState = () => { return gridState[index] };
|
||||||
};
|
const callback = () => { playTurn(index) };
|
||||||
const callback = () => {
|
return (<TicTacToeCell getState={getState} callback={callback}/>);
|
||||||
playTurn(index);
|
})
|
||||||
};
|
}
|
||||||
return <TicTacToeCell state={gridState[index]} onClick={callback} />;
|
|
||||||
})}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CellProps {
|
interface CellProps {
|
||||||
state: number;
|
getState: () => number;
|
||||||
onClick: () => void;
|
callback: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
function TicTacToeCell({ state, onClick }: CellProps) {
|
function TicTacToeCell({ getState, callback }: CellProps) {
|
||||||
|
|
||||||
|
function handleClick() {
|
||||||
|
callback()
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`TicTacToeCell ${getStateClass(state)}`} onClick={onClick}>
|
<div className={`TicTacToeCell ${getStateClass(getState())}`} onClick={handleClick}>
|
||||||
<p>{getStateChar(state)}</p>
|
<p>{getStateChar(getState())}</p>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getStateChar(state: number) {
|
function getStateChar(state : number) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case 1:
|
case 1:
|
||||||
return "O";
|
return "O";
|
||||||
@ -135,7 +145,7 @@ function getStateChar(state: number) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getStateClass(state: number) {
|
function getStateClass(state : number) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case 1:
|
case 1:
|
||||||
return "CellRed";
|
return "CellRed";
|
||||||
|
Loading…
Reference in New Issue
Block a user