added restart button

main
selim 2024-12-28 03:21:11 +01:00
parent f6555251e8
commit 8ba4aca037
3 changed files with 36 additions and 10 deletions

View File

@ -1,5 +1,5 @@
import { useState } from "react"; import { useState } from "react";
import { clickHandler } from "./functions"; import { clickHandlerTurn, clickHandlerRestart } from "./functions";
let params = {}; let params = {};
@ -32,6 +32,7 @@ export default function App() {
<> <>
<h1>CONNECT4</h1> <h1>CONNECT4</h1>
<Board /> <Board />
<Restart />
<p>{params.message}</p> <p>{params.message}</p>
</> </>
); );
@ -44,7 +45,7 @@ function Field({ row, col, backgroundColor, disabled }) {
row={row} row={row}
col={col} col={col}
style={backgroundColor} style={backgroundColor}
onClick={() => clickHandler(col, params)} onClick={() => clickHandlerTurn(col, params)}
disabled={disabled} disabled={disabled}
> >
&nbsp; &nbsp;
@ -73,3 +74,9 @@ function Board() {
</div> </div>
); );
} }
function Restart() {
return (< button className="restart" disabled={!params.gameOver} hidden={!params.gameOver} onClick={() => clickHandlerRestart(params)}>
&#8634;
</button >);
}

View File

@ -78,7 +78,7 @@ export function checkWin(row, col, colorBoard, currentPlayer) {
} }
} }
export function clickHandler(col, params) { export function clickHandlerTurn(col, params) {
const [worked, currentChip] = putChip( const [worked, currentChip] = putChip(
col, col,
params.colorBoard, params.colorBoard,
@ -104,3 +104,10 @@ export function clickHandler(col, params) {
params.setMessage("column is full!"); params.setMessage("column is full!");
} }
} }
export function clickHandlerRestart(params) {
params.setTurns(1);
params.setGameOver(false);
params.setColorBoard(Array.from(Array(6), () => new Array(7).fill("white")));
params.setMessage("");
}

View File

@ -2,13 +2,14 @@ body{
text-align: center; text-align: center;
font-family: Arial, Helvetica, sans-serif; font-family: Arial, Helvetica, sans-serif;
} }
.board { .board {
width: 700px; width: 700px;
height: 600px; height: 600px;
display: grid; display: grid;
grid-template-columns: repeat(7, auto [col-start]); grid-template-columns: repeat(7, auto [col-start]);
margin: auto; margin: auto;
border: 3px solid black; border: 5px solid black;
background-color: blue; background-color: blue;
} }
@ -17,3 +18,14 @@ body{
border-radius: 100%; border-radius: 100%;
margin: 10px; margin: 10px;
} }
.restart {
width: 100px;
height: 60px;
font-size: xx-large;
border: 5px solid black;
color: white;
background-color: blue;
margin-top: 20px;
}