Compare commits
2 Commits
edward-shi
...
master
Author | SHA1 | Date | |
---|---|---|---|
6fe78927c6 | |||
69a13f7a7f |
@ -1,149 +1,164 @@
|
|||||||
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) {
|
||||||
|
gridState[index] = state;
|
||||||
|
setGridState(gridState);
|
||||||
|
}
|
||||||
|
|
||||||
function updateGridState(index: number, state: number) {
|
function resetGridState() {
|
||||||
gridState[index] = state;
|
setGridState(Array(9).fill(0));
|
||||||
setGridState(gridState);
|
setPlayerState(false);
|
||||||
}
|
setWinState(0);
|
||||||
|
}
|
||||||
|
|
||||||
function playTurn(index: number) {
|
function playTurn(index : number) {
|
||||||
if (gridState[index]) return;
|
if (winState) {
|
||||||
if (winState) return;
|
resetGridState()
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (gridState[index]) 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);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setPlayerState(!playerState);
|
setPlayerState(!playerState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkWinCondition() : number {
|
||||||
|
|
||||||
function checkWinCondition(): number {
|
function getWinningValue(
|
||||||
function getWinningValue(a: number, b: number, c: number) {
|
a : number,
|
||||||
if (a === b && b === c && a === c) {
|
b : number,
|
||||||
return a;
|
c : number
|
||||||
}
|
) {
|
||||||
return 0;
|
if ((a === b) && (b === c) && (a === c)) {
|
||||||
}
|
return a;
|
||||||
/*
|
}
|
||||||
* Diagonals
|
return 0
|
||||||
*/
|
}
|
||||||
{
|
/*
|
||||||
const a = gridState[3 * 0 + 0];
|
* Diagonals
|
||||||
const b = gridState[3 * 1 + 1];
|
*/
|
||||||
const c = gridState[3 * 2 + 2];
|
{
|
||||||
const winner = getWinningValue(a, b, c);
|
const a = gridState[3 * 0 + 0];
|
||||||
if (winner) return winner;
|
const b = gridState[3 * 1 + 1];
|
||||||
}
|
const c = gridState[3 * 2 + 2];
|
||||||
{
|
const winner = getWinningValue(a, b, c);
|
||||||
const a = gridState[3 * 0 + 2];
|
if (winner) return winner;
|
||||||
const b = gridState[3 * 1 + 1];
|
}
|
||||||
const c = gridState[3 * 2 + 0];
|
{
|
||||||
const winner = getWinningValue(a, b, c);
|
const a = gridState[3 * 0 + 2];
|
||||||
if (winner) return winner;
|
const b = gridState[3 * 1 + 1];
|
||||||
}
|
const c = gridState[3 * 2 + 0];
|
||||||
/*
|
const winner = getWinningValue(a, b, c);
|
||||||
* Rows and Columns
|
if (winner) return winner;
|
||||||
*/
|
}
|
||||||
for (let row = 0; row < 3; row++) {
|
/*
|
||||||
const a = gridState[3 * row + 0];
|
* Rows and Columns
|
||||||
const b = gridState[3 * row + 1];
|
*/
|
||||||
const c = gridState[3 * row + 2];
|
for (let row = 0; row < 3; row++) {
|
||||||
const winner = getWinningValue(a, b, c);
|
const a = gridState[3 * row + 0];
|
||||||
if (winner) return winner;
|
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++) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
for (let column = 0; column < 3; column++) {
|
const isDrawState = () => {
|
||||||
const a = gridState[3 * 0 + column];
|
for (let i in gridState) {
|
||||||
const b = gridState[3 * 1 + column];
|
if (!gridState[i]) return false;
|
||||||
const c = gridState[3 * 2 + column];
|
};
|
||||||
const winner = getWinningValue(a, b, c);
|
return true;
|
||||||
if (winner) return winner;
|
}
|
||||||
}
|
if (isDrawState()) return 3;
|
||||||
|
|
||||||
const isDrawState = () => {
|
return 0;
|
||||||
for (let i in gridState) {
|
}
|
||||||
if (!gridState[i]) return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
if (isDrawState()) return 3;
|
|
||||||
|
|
||||||
return 0;
|
let stateString = "";
|
||||||
}
|
if (localWinState === 3) {
|
||||||
|
stateString = `Game draw.`
|
||||||
|
}
|
||||||
|
else if (localWinState) {
|
||||||
|
stateString = `Player ${getStateChar(localWinState)} wins.`;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
stateString = `Player ${getStateChar(playerState ? 2 : 1)} turn.`
|
||||||
|
}
|
||||||
|
|
||||||
let stateString = "";
|
return (
|
||||||
if (localWinState === 3) {
|
<div className="TicTacToe">
|
||||||
stateString = `Game draw.`;
|
<h1>{stateString}</h1>
|
||||||
} else if (localWinState) {
|
<div className="TicTacToeGrid">
|
||||||
stateString = `Player ${getStateChar(localWinState)} wins.`;
|
{
|
||||||
} else {
|
gridState.map((item, index) => {
|
||||||
stateString = `Player ${getStateChar(playerState ? 2 : 1)} turn.`;
|
const state = gridState[index];
|
||||||
}
|
const onClick = () => { playTurn(index) };
|
||||||
|
return (<TicTacToeCell state={state} onClick={onClick}/>);
|
||||||
return (
|
})
|
||||||
<div className="TicTacToe">
|
}
|
||||||
<h1>{stateString}</h1>
|
</div>
|
||||||
<div className="TicTacToeGrid">
|
</div>
|
||||||
{gridState.map((item, index) => {
|
);
|
||||||
const getState = () => {
|
|
||||||
return gridState[index];
|
|
||||||
};
|
|
||||||
const callback = () => {
|
|
||||||
playTurn(index);
|
|
||||||
};
|
|
||||||
return <TicTacToeCell state={gridState[index]} onClick={callback} />;
|
|
||||||
})}
|
|
||||||
</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 (
|
|
||||||
<div className={`TicTacToeCell ${getStateClass(state)}`} onClick={onClick}>
|
return (
|
||||||
<p>{getStateChar(state)}</p>
|
<div className={`TicTacToeCell ${getStateClass(state)}`} onClick={onClick}>
|
||||||
</div>
|
<p>{getStateChar(state)}</p>
|
||||||
);
|
</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;
|
||||||
|
@ -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