Compare commits

...

2 Commits

2 changed files with 22 additions and 15 deletions

View File

@ -13,9 +13,18 @@ function TicTacToe() {
setGridState(gridState);
}
function resetGridState() {
setGridState(Array(9).fill(0));
setPlayerState(false);
setWinState(0);
}
function playTurn(index : number) {
if (winState) {
resetGridState()
return;
}
if (gridState[index]) return;
if (winState) return;
const newState = playerState ? 2 : 1;
updateGridState(index, newState);
@ -106,9 +115,9 @@ function TicTacToe() {
<div className="TicTacToeGrid">
{
gridState.map((item, index) => {
const getState = () => { return gridState[index] };
const callback = () => { playTurn(index) };
return (<TicTacToeCell getState={getState} callback={callback}/>);
const state = gridState[index];
const onClick = () => { playTurn(index) };
return (<TicTacToeCell state={state} onClick={onClick}/>);
})
}
</div>
@ -117,19 +126,15 @@ function TicTacToe() {
}
interface CellProps {
getState: () => number;
callback: () => void;
state: number;
onClick: () => void;
}
function TicTacToeCell({ getState, callback }: CellProps) {
function handleClick() {
callback()
}
function TicTacToeCell({ state, onClick }: CellProps) {
return (
<div className={`TicTacToeCell ${getStateClass(getState())}`} onClick={handleClick}>
<p>{getStateChar(getState())}</p>
<div className={`TicTacToeCell ${getStateClass(state)}`} onClick={onClick}>
<p>{getStateChar(state)}</p>
</div>
);
}

View File

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