Compare commits
2 Commits
edward-shi
...
master
Author | SHA1 | Date | |
---|---|---|---|
6fe78927c6 | |||
69a13f7a7f |
@ -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));
|
||||||
@ -13,14 +13,23 @@ function TicTacToe() {
|
|||||||
setGridState(gridState);
|
setGridState(gridState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resetGridState() {
|
||||||
|
setGridState(Array(9).fill(0));
|
||||||
|
setPlayerState(false);
|
||||||
|
setWinState(0);
|
||||||
|
}
|
||||||
|
|
||||||
function playTurn(index : number) {
|
function playTurn(index : number) {
|
||||||
|
if (winState) {
|
||||||
|
resetGridState()
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (gridState[index]) return;
|
if (gridState[index]) 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);
|
||||||
@ -31,11 +40,16 @@ function TicTacToe() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 +90,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,26 +100,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 {
|
}
|
||||||
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 state = gridState[index];
|
||||||
};
|
const onClick = () => { playTurn(index) };
|
||||||
const callback = () => {
|
return (<TicTacToeCell state={state} onClick={onClick}/>);
|
||||||
playTurn(index);
|
})
|
||||||
};
|
}
|
||||||
return <TicTacToeCell state={gridState[index]} onClick={callback} />;
|
|
||||||
})}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@ -117,6 +131,7 @@ interface CellProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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>
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
.TicTacToe {
|
.TicTacToe {
|
||||||
background-color: #3B3E49;
|
background-color: #3B3E49;
|
||||||
color: #B9B8D6;
|
color: #B9B8D6;
|
||||||
|
border-radius: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.TicTacToeGrid {
|
.TicTacToeGrid {
|
||||||
display: grid;
|
display: grid;
|
||||||
width: 50vh;
|
min-width: 50vw;
|
||||||
height: 50vh;
|
min-height: 50vw;
|
||||||
grid-template-columns: auto auto auto;
|
grid-template-columns: auto auto auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,6 +30,7 @@
|
|||||||
left: 50%;
|
left: 50%;
|
||||||
top: 50%;
|
top: 50%;
|
||||||
transform: translate(-50%, -50%);
|
transform: translate(-50%, -50%);
|
||||||
|
font-size: 10vw;
|
||||||
}
|
}
|
||||||
|
|
||||||
.CellRed {
|
.CellRed {
|
||||||
|
Loading…
Reference in New Issue
Block a user