2 Commits

Author SHA1 Message Date
sheldonmlee 6fe78927c6 Css tweaks 2023-09-25 05:29:40 +08:00
sheldonmlee 69a13f7a7f Refactor TicTacToeCell interface and callback, and implement game reset. 2023-09-22 21:34:23 +08:00
2 changed files with 22 additions and 15 deletions
+18 -13
View File
@@ -13,9 +13,18 @@ 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);
@@ -106,9 +115,9 @@ function TicTacToe() {
<div className="TicTacToeGrid"> <div className="TicTacToeGrid">
{ {
gridState.map((item, index) => { gridState.map((item, index) => {
const getState = () => { return gridState[index] }; const state = gridState[index];
const callback = () => { playTurn(index) }; const onClick = () => { playTurn(index) };
return (<TicTacToeCell getState={getState} callback={callback}/>); return (<TicTacToeCell state={state} onClick={onClick}/>);
}) })
} }
</div> </div>
@@ -117,19 +126,15 @@ function TicTacToe() {
} }
interface CellProps { interface CellProps {
getState: () => number; state: number;
callback: () => void; onClick: () => void;
} }
function TicTacToeCell({ getState, callback }: CellProps) { function TicTacToeCell({ state, onClick }: CellProps) {
function handleClick() {
callback()
}
return ( return (
<div className={`TicTacToeCell ${getStateClass(getState())}`} onClick={handleClick}> <div className={`TicTacToeCell ${getStateClass(state)}`} onClick={onClick}>
<p>{getStateChar(getState())}</p> <p>{getStateChar(state)}</p>
</div> </div>
); );
} }
+4 -2
View File
@@ -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 {