Compare commits

..

3 Commits

2 changed files with 126 additions and 143 deletions

View File

@ -1,164 +1,149 @@
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));
const [playerState, setPlayerState] = useState(false); const [playerState, setPlayerState] = useState(false);
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 resetGridState() { function playTurn(index: number) {
setGridState(Array(9).fill(0)); if (gridState[index]) return;
setPlayerState(false); if (winState) return;
setWinState(0);
}
function playTurn(index : number) { const newState = playerState ? 2 : 1;
if (winState) { updateGridState(index, newState);
resetGridState()
return;
}
if (gridState[index]) return;
const newState = playerState ? 2 : 1; const winner = checkWinCondition();
updateGridState(index, newState); if (winner) {
localWinState = winner;
setWinState(winner);
return;
}
const winner = checkWinCondition() setPlayerState(!playerState);
if (winner) { }
localWinState = winner;
setWinState(winner);
return;
}
setPlayerState(!playerState); function checkWinCondition(): number {
} function getWinningValue(a: number, b: number, c: number) {
if (a === b && b === c && a === c) {
return a;
}
return 0;
}
/*
* Diagonals
*/
{
const a = gridState[3 * 0 + 0];
const b = gridState[3 * 1 + 1];
const c = gridState[3 * 2 + 2];
const winner = getWinningValue(a, b, c);
if (winner) return winner;
}
{
const a = gridState[3 * 0 + 2];
const b = gridState[3 * 1 + 1];
const c = gridState[3 * 2 + 0];
const winner = getWinningValue(a, b, c);
if (winner) return winner;
}
/*
* Rows and Columns
*/
for (let row = 0; row < 3; row++) {
const a = gridState[3 * row + 0];
const b = gridState[3 * row + 1];
const c = gridState[3 * row + 2];
const winner = getWinningValue(a, b, c);
if (winner) return winner;
}
function checkWinCondition() : number { for (let column = 0; column < 3; column++) {
const a = gridState[3 * 0 + column];
const b = gridState[3 * 1 + column];
const c = gridState[3 * 2 + column];
const winner = getWinningValue(a, b, c);
if (winner) return winner;
}
function getWinningValue( const isDrawState = () => {
a : number, for (let i in gridState) {
b : number, if (!gridState[i]) return false;
c : number }
) { return true;
if ((a === b) && (b === c) && (a === c)) { };
return a; if (isDrawState()) return 3;
}
return 0
}
/*
* Diagonals
*/
{
const a = gridState[3 * 0 + 0];
const b = gridState[3 * 1 + 1];
const c = gridState[3 * 2 + 2];
const winner = getWinningValue(a, b, c);
if (winner) return winner;
}
{
const a = gridState[3 * 0 + 2];
const b = gridState[3 * 1 + 1];
const c = gridState[3 * 2 + 0];
const winner = getWinningValue(a, b, c);
if (winner) return winner;
}
/*
* Rows and Columns
*/
for (let row = 0; row < 3; row++) {
const a = gridState[3 * row + 0];
const b = gridState[3 * row + 1];
const c = gridState[3 * row + 2];
const winner = getWinningValue(a, b, c);
if (winner) return winner;
}
for (let column = 0; column < 3; column++) { return 0;
const a = gridState[3 * 0 + column]; }
const b = gridState[3 * 1 + column];
const c = gridState[3 * 2 + column];
const winner = getWinningValue(a, b, c);
if (winner) return winner;
}
const isDrawState = () => { let stateString = "";
for (let i in gridState) { if (localWinState === 3) {
if (!gridState[i]) return false; stateString = `Game draw.`;
}; } else if (localWinState) {
return true; stateString = `Player ${getStateChar(localWinState)} wins.`;
} } else {
if (isDrawState()) return 3; stateString = `Player ${getStateChar(playerState ? 2 : 1)} turn.`;
}
return 0; return (
} <div className="TicTacToe">
<h1>{stateString}</h1>
let stateString = ""; <div className="TicTacToeGrid">
if (localWinState === 3) { {gridState.map((item, index) => {
stateString = `Game draw.` const getState = () => {
} return gridState[index];
else if (localWinState) { };
stateString = `Player ${getStateChar(localWinState)} wins.`; const callback = () => {
} playTurn(index);
else { };
stateString = `Player ${getStateChar(playerState ? 2 : 1)} turn.` return <TicTacToeCell state={gridState[index]} onClick={callback} />;
} })}
</div>
return ( </div>
<div className="TicTacToe"> );
<h1>{stateString}</h1>
<div className="TicTacToeGrid">
{
gridState.map((item, index) => {
const state = gridState[index];
const onClick = () => { playTurn(index) };
return (<TicTacToeCell state={state} onClick={onClick}/>);
})
}
</div>
</div>
);
} }
interface CellProps { interface CellProps {
state: number; state: number;
onClick: () => void; onClick: () => void;
} }
function TicTacToeCell({ state, onClick }: CellProps) { function TicTacToeCell({ state, onClick }: CellProps) {
return (
return ( <div className={`TicTacToeCell ${getStateClass(state)}`} onClick={onClick}>
<div className={`TicTacToeCell ${getStateClass(state)}`} onClick={onClick}> <p>{getStateChar(state)}</p>
<p>{getStateChar(state)}</p> </div>
</div> );
);
} }
function getStateChar(state : number) { function getStateChar(state: number) {
switch (state) { switch (state) {
case 1: case 1:
return "O"; return "O";
case 2: case 2:
return "X"; return "X";
default: default:
return ""; return "";
} }
} }
function getStateClass(state : number) { function getStateClass(state: number) {
switch (state) { switch (state) {
case 1: case 1:
return "CellRed"; return "CellRed";
case 2: case 2:
return "CellBlue"; return "CellBlue";
default: default:
return ""; return "";
} }
} }
export default TicTacToe; export default TicTacToe;

View File

@ -1,13 +1,12 @@
.TicTacToe { .TicTacToe {
background-color: #3B3E49; background-color: #3B3E49;
color: #B9B8D6; color: #B9B8D6;
border-radius: 20px;
} }
.TicTacToeGrid { .TicTacToeGrid {
display: grid; display: grid;
min-width: 50vw; width: 50vh;
min-height: 50vw; height: 50vh;
grid-template-columns: auto auto auto; grid-template-columns: auto auto auto;
} }
@ -30,7 +29,6 @@
left: 50%; left: 50%;
top: 50%; top: 50%;
transform: translate(-50%, -50%); transform: translate(-50%, -50%);
font-size: 10vw;
} }
.CellRed { .CellRed {