Compare commits
3 Commits
master
...
edward-shi
Author | SHA1 | Date | |
---|---|---|---|
6e479ea745 | |||
c86cd7b6e3 | |||
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,28 +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 resetGridState() {
|
||||
setGridState(Array(9).fill(0));
|
||||
setPlayerState(false);
|
||||
setWinState(0);
|
||||
}
|
||||
|
||||
function playTurn(index : number) {
|
||||
if (winState) {
|
||||
resetGridState()
|
||||
return;
|
||||
}
|
||||
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);
|
||||
@ -39,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
|
||||
@ -90,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;
|
||||
@ -100,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 state = gridState[index];
|
||||
const onClick = () => { playTurn(index) };
|
||||
return (<TicTacToeCell state={state} onClick={onClick}/>);
|
||||
})
|
||||
}
|
||||
{gridState.map((item, index) => {
|
||||
const getState = () => {
|
||||
return gridState[index];
|
||||
};
|
||||
const callback = () => {
|
||||
playTurn(index);
|
||||
};
|
||||
return <TicTacToeCell state={gridState[index]} onClick={callback} />;
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
@ -131,7 +117,6 @@ interface CellProps {
|
||||
}
|
||||
|
||||
function TicTacToeCell({ state, onClick }: CellProps) {
|
||||
|
||||
return (
|
||||
<div className={`TicTacToeCell ${getStateClass(state)}`} onClick={onClick}>
|
||||
<p>{getStateChar(state)}</p>
|
||||
@ -139,7 +124,7 @@ function TicTacToeCell({ state, onClick }: CellProps) {
|
||||
);
|
||||
}
|
||||
|
||||
function getStateChar(state : number) {
|
||||
function getStateChar(state: number) {
|
||||
switch (state) {
|
||||
case 1:
|
||||
return "O";
|
||||
@ -150,7 +135,7 @@ function getStateChar(state : number) {
|
||||
}
|
||||
}
|
||||
|
||||
function getStateClass(state : number) {
|
||||
function getStateClass(state: number) {
|
||||
switch (state) {
|
||||
case 1:
|
||||
return "CellRed";
|
||||
|
@ -1,13 +1,12 @@
|
||||
.TicTacToe {
|
||||
background-color: #3B3E49;
|
||||
color: #B9B8D6;
|
||||
border-radius: 20px;
|
||||
}
|
||||
|
||||
.TicTacToeGrid {
|
||||
display: grid;
|
||||
min-width: 50vw;
|
||||
min-height: 50vw;
|
||||
width: 50vh;
|
||||
height: 50vh;
|
||||
grid-template-columns: auto auto auto;
|
||||
}
|
||||
|
||||
@ -30,7 +29,6 @@
|
||||
left: 50%;
|
||||
top: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
font-size: 10vw;
|
||||
}
|
||||
|
||||
.CellRed {
|
||||
|
Loading…
Reference in New Issue
Block a user