Winning condition shown with offset outline instead of color

main
Selim Eser 2024-12-29 18:48:05 +01:00
parent 1b5bae32a7
commit fa36199c30
3 changed files with 68 additions and 43 deletions

View File

@ -15,6 +15,12 @@ export default function App() {
params.colorBoard = colorBoard; params.colorBoard = colorBoard;
params.setColorBoard = setColorBoard; params.setColorBoard = setColorBoard;
const [outlineBoard, setOutlineBoard] = useState(
Array.from(Array(6), () => new Array(7).fill("0px solid black"))
);
params.outlineBoard = outlineBoard;
params.setOutlineBoard = setOutlineBoard;
const [gameOver, setGameOver] = useState(false); const [gameOver, setGameOver] = useState(false);
params.gameOver = gameOver; params.gameOver = gameOver;
params.setGameOver = setGameOver; params.setGameOver = setGameOver;
@ -51,15 +57,18 @@ export default function App() {
function Field({ row, col, style, disabled }) { function Field({ row, col, style, disabled }) {
const [pointer, setPointer] = useState(false); const [pointer, setPointer] = useState(false);
const [color, setColor] = useState("white"); const [hoverColor, setHoverColor] = useState("white");
const styling = { const styling = {
backgroundColor: !pointer backgroundColor: !pointer
? style.backgroundColor ? style.backgroundColor
: params.currentPlayer === "red" && row === 0 : params.currentPlayer === "red" && row === 0
? color ? hoverColor
: params.currentPlayer === "yellow" && row === 0 && color, : params.currentPlayer === "yellow" && row === 0 && hoverColor,
cursor: pointer && "pointer", cursor: pointer && "pointer",
outlineWidth: style.outlineWidth,
outlineColor: style.outlineColor,
outlineStyle: style.outlineStyle,
}; };
return ( return (
@ -69,26 +78,24 @@ function Field({ row, col, style, disabled }) {
col={col} col={col}
style={styling} style={styling}
onClick={async () => { onClick={async () => {
await clickHandlerTurn(col, params); await clickHandlerTurn(params, col);
if (params.gamemode === 2) { if (params.gamemode === 2) {
params.currentPlayer === "red" params.currentPlayer === "red"
? setColor("lightcoral") ? setHoverColor("hsla(0, 100%, 80%, 1)")
: setColor("#f2f28d"); : setHoverColor("hsla(60, 100%, 80%, 1)");
} }
}} }}
disabled={disabled} disabled={disabled}
onMouseEnter={() => { onMouseEnter={() => {
params.gameOver && setPointer(false); if (!disabled) {
setPointer(!params.gameOver);
if (!disabled && !params.gameOver) {
setPointer(true);
} }
params.currentPlayer === "red" params.currentPlayer === "red"
? setColor("lightcoral") ? setHoverColor("hsla(0, 100%, 80%, 1)")
: setColor("#f2f28d"); : setHoverColor("hsla(60, 100%, 80%, 1)");
}} }}
onMouseLeave={() => { onMouseLeave={() => {
setColor(style.backgroundColor); setHoverColor(style.backgroundColor);
setPointer(false); setPointer(false);
}} }}
> >
@ -105,7 +112,12 @@ function Board() {
key={row.toString() + col.toString()} key={row.toString() + col.toString()}
row={row} row={row}
col={col} col={col}
style={{ backgroundColor: color }} style={{
backgroundColor: color,
outlineWidth: params.outlineBoard[row][col].split(" ")[0],
outlineColor: params.outlineBoard[row][col].split(" ")[2],
outlineStyle: params.outlineBoard[row][col].split(" ")[1],
}}
disabled={row === 0 ? params.gameOver : true} disabled={row === 0 ? params.gameOver : true}
/> />
); );

View File

@ -25,60 +25,65 @@ async function putChip(
return false; return false;
} }
async function checkWin(colorBoard, setColorBoard, currentPlayer) { async function checkWin(
const mem = colorBoard; colorBoard,
currentPlayer,
outlineBoard,
setOutlineBoard
) {
const memOutlineBoard = outlineBoard;
for (let row = 0; row <= 5; row++) { for (let row = 0; row <= 5; row++) {
for (let col = 0; col <= 6; col++) { for (let col = 0; col <= 6; col++) {
if ( if (
row <= 2 && row <= 2 &&
mem[row][col] === currentPlayer && colorBoard[row][col] === currentPlayer &&
mem[row + 1][col] === currentPlayer && colorBoard[row + 1][col] === currentPlayer &&
mem[row + 2][col] === currentPlayer && colorBoard[row + 2][col] === currentPlayer &&
mem[row + 3][col] === currentPlayer colorBoard[row + 3][col] === currentPlayer
) { ) {
for (let i = 0; i <= 3; i++) { for (let i = 0; i <= 3; i++) {
mem[row + i][col] = "orange"; memOutlineBoard[row + i][col] = "7px outset black";
} }
await setColorBoard(mem); await setOutlineBoard(memOutlineBoard);
return true; return true;
} else if ( } else if (
col <= 3 && col <= 3 &&
mem[row][col] === currentPlayer && colorBoard[row][col] === currentPlayer &&
mem[row][col + 1] === currentPlayer && colorBoard[row][col + 1] === currentPlayer &&
mem[row][col + 2] === currentPlayer && colorBoard[row][col + 2] === currentPlayer &&
mem[row][col + 3] === currentPlayer colorBoard[row][col + 3] === currentPlayer
) { ) {
for (let i = 0; i <= 3; i++) { for (let i = 0; i <= 3; i++) {
mem[row][col + i] = "orange"; memOutlineBoard[row][col + i] = "7px outset black";
} }
await setColorBoard(mem); await setOutlineBoard(memOutlineBoard);
return true; return true;
} else if ( } else if (
row <= 2 && row <= 2 &&
col <= 3 && col <= 3 &&
mem[row][col] === currentPlayer && colorBoard[row][col] === currentPlayer &&
mem[row + 1][col + 1] === currentPlayer && colorBoard[row + 1][col + 1] === currentPlayer &&
mem[row + 2][col + 2] === currentPlayer && colorBoard[row + 2][col + 2] === currentPlayer &&
mem[row + 3][col + 3] === currentPlayer colorBoard[row + 3][col + 3] === currentPlayer
) { ) {
for (let i = 0; i <= 3; i++) { for (let i = 0; i <= 3; i++) {
mem[row + i][col + i] = "orange"; memOutlineBoard[row + i][col + i] = "7px outset black";
} }
await setColorBoard(mem); await setOutlineBoard(memOutlineBoard);
return true; return true;
} else if ( } else if (
row <= 2 && row <= 2 &&
col <= 3 && col <= 3 &&
mem[row][col + 3] === currentPlayer && colorBoard[row][col + 3] === currentPlayer &&
mem[row + 1][col + 2] === currentPlayer && colorBoard[row + 1][col + 2] === currentPlayer &&
mem[row + 2][col + 1] === currentPlayer && colorBoard[row + 2][col + 1] === currentPlayer &&
mem[row + 3][col] === currentPlayer colorBoard[row + 3][col] === currentPlayer
) { ) {
for (let i = 0; i <= 3; i++) { for (let i = 0; i <= 3; i++) {
mem[row + i][col + 3 - i] = "orange"; memOutlineBoard[row + i][col + 3 - i] = "7px outset black";
} }
await setColorBoard(mem); await setOutlineBoard(memOutlineBoard);
return true; return true;
} }
} }
@ -106,7 +111,7 @@ export function colFull(col, colorBoard) {
} }
} }
export async function clickHandlerTurn(col, params) { export async function clickHandlerTurn(params, col) {
const iMax = params.gamemode === 1 ? 2 : 1; const iMax = params.gamemode === 1 ? 2 : 1;
for (let i = 0; i < iMax; i++) { for (let i = 0; i < iMax; i++) {
@ -134,8 +139,9 @@ export async function clickHandlerTurn(col, params) {
const win = await checkWin( const win = await checkWin(
params.colorBoard, params.colorBoard,
params.setColorBoard, memCurrentPlayer,
memCurrentPlayer params.outlineBoard,
params.setOutlineBoard
); );
await params.setTurns(params.turns + 1); await params.setTurns(params.turns + 1);
@ -163,6 +169,9 @@ export function clickHandlerRestart(params) {
params.setTurns(0); params.setTurns(0);
params.setGameOver(false); params.setGameOver(false);
params.setColorBoard(Array.from(Array(6), () => new Array(7).fill("white"))); params.setColorBoard(Array.from(Array(6), () => new Array(7).fill("white")));
params.setOutlineBoard(
Array.from(Array(6), () => new Array(7).fill("0px solid black"))
);
params.setMessage(""); params.setMessage("");
} }
@ -176,5 +185,8 @@ export function clickHandlerReturn(params) {
params.setTurns(0); params.setTurns(0);
params.setGameOver(false); params.setGameOver(false);
params.setColorBoard(Array.from(Array(6), () => new Array(7).fill("white"))); params.setColorBoard(Array.from(Array(6), () => new Array(7).fill("white")));
params.setOutlineBoard(
Array.from(Array(6), () => new Array(7).fill("0px solid black"))
);
params.setMessage(""); params.setMessage("");
} }

View File

@ -28,6 +28,7 @@ body {
border: 3px solid black; border: 3px solid black;
border-radius: 100%; border-radius: 100%;
margin: 10px; margin: 10px;
outline-offset: -7px;
} }
.inner-button { .inner-button {