I’m building a minesweeper game with socket.io everything seems to be working fine but I have one problem I need to add difficulties before the game loads on easy mode, for example, I need to have 10 cells on medium 100 and on hard 500 I don’t understand how can I add the difficulties to the game I tried to do it with a switch statement in a function but I couldn’t do it I’ve been working on this for a long time also when I tried to do cells with 100 cells instead of 10 in Array. fill function the board seems to fail and the cells are not opening the right away and they don’t stand under each other the order fails
front code:
import React, { useState, useEffect, useRef } from "react";
import "./App.css";
import { faceSmile } from "react-icons/fa";
import socketIOClient from "socket.io-client";
const ENDPOINT = "http://127.0.0.1:4001";
function App() {
const [bombs, setBombs] = useState([]);
const [visited, setVisited] = useState([]);
const [state,setState ] = useState({bombs:[], visited:[]});
const socketRef = useRef();
const buttonStyle = {
width: 40,
height: 40,
backgroundColor: "#8888",
color: "black",
verticalAlign: "top",
fontSize: "32px",
fontFamily: "Arial, Helvetica, sans-serif;",
borderLeft: "5px solid rgb(220,220,220)",
borderTop: "5px solid rgb(220,220,220)",
borderBottom: "5px solid #333",
borderRight: "5px solid #333",
display: "inline-block",
};
const visitStyle = {
width: 48,
height: 48,
backgroundColor: "#555",
color: "white",
fontWeight: "bold",
border: "1px solid black",
verticalAlign: "top",
fontSize: "32px",
fontFamily: "Montserrat, sans-serif",
display: "inline-block",
};
useEffect(() => {
socketRef.current = socketIOClient(ENDPOINT);
socketRef.current.on("boardUpdate", (data) => {
console.log("boardUpdate front", data)
setState(data);
});
socketRef.current.on("onLost", () => {
alert("You lost!");
window.location.reload();
});
return () => socketRef.current.disconnect();
}, []);
const handleFlag = (e) => {
e.preventDefault();
e.target.style.backgroundColor = "red";
};
const visitCell = (i, j) => {
socketRef.current.emit("onclick", { i, j });
};
const generateBombs = () => {
console.log("generateBombs", socketRef.current);
socketRef.current.emit("generateBoard");
};
return (
<div className="App">
<header className="App-header">
<div className={"title"}> REACT-SWEEPER </div>
<i class="fa-regular fa-face-smile"></i>
<div className={"subtext"}> </div>
{state.bombs.map((arr, index) => (
<div>
{arr.map((elem, i) => (
<div
onContextMenu={handleFlag}
onClick={() => visitCell(index, i)}
style={state.visited[index][i] == 0 ? buttonStyle : visitStyle}
>
{state.visited[index][i] == 0
? null
: state.bombs[index][i] == 0
? ""
: state.bombs[index][i]}
</div>
))}
</div>
))}
<button className={"generate"} onClick={() => generateBombs()}>
New Game
<i class="fa-regular fa-face-smile"></i>
</button>
</header>
</div>
);
}
export default App;
back code with socket.io
const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server, {
cors: {
origins: ["*"],
},
});
var bombs = [];
var visited = [];
io.on("connection", (socket) => {
console.log("a user connected");
socket.on("disconnect", () => {
console.log("user disconnected");
});
socket.on("generateBoard", () => {
generateBombs();
console.log("generateBoard", this.bombs, this.visited);
io.emit("boardUpdate", { bombs: this.bombs, visited: this.visited });
});
socket.on("onclick", (data) => {
console.log("onclick", data);
visitCell(data.i, data.j);
io.emit("boardUpdate", { bombs: this.bombs, visited: this.visited });
});
});
server.listen(4001, () => {
console.log("listening on *:4001");
});
const generateBombs = () => {
let bombArr = Array(10)
.fill(0)
.map((elem) => Array(10).fill(0));
for (let i = 0; i < bombArr.length; i++) {
let bombPos = Math.floor(Math.random() * 10);
bombArr[i][bombPos] = "X";
}
for (let i = 0; i < bombArr.length; i++) {
for (let j = 0; j < bombArr[i].length; j++) {
if (bombArr[i][j] !== "X") {
let sum = 0;
if (i > 0 && bombArr[i - 1][j] == "X") sum++;
if (i < bombArr.length - 1 && bombArr[i + 1][j] == "X") sum++;
if (j < bombArr.length - 1 && bombArr[i][j + 1] == "X") sum++;
if (j > 0 && bombArr[i][j - 1] == "X") sum++;
if (i < bombArr.length - 1 && j > 0 && bombArr[i + 1][j - 1] == "X")
sum++;
if (
i < bombArr.length - 1 &&
j < bombArr.length - 1 &&
bombArr[i + 1][j + 1] == "X"
)
sum++;
if (i > 0 && j > 0 && bombArr[i - 1][j - 1] == "X") sum++;
if (i > 0 && j < bombArr.length - 1 && bombArr[i - 1][j + 1] == "X")
sum++;
bombArr[i][j] = sum;
}
}
}
setBombs(bombArr);
let cover = Array(10)
.fill(0)
.map((elem) => Array(10).fill(0));
setVisited(cover);
};
const setBombs = (bombArr) => {
this.bombs = bombArr;
};
const setVisited = (cover) => {
this.visited = cover;
};
const Cells = (i, j) => {
if (
i < 0 ||
i > this.visited.length - 1 ||
j < 0 ||
j > this.visited[0].length - 1 ||
this.visited[i][j] == 1 ||
this.bombs[i][j] == "X"
)
return;
this.visited[i][j] = 1;
setVisited([...this.visited]);
if (this.bombs[i][j] < 1) {
Cells(i + 1, j);
Cells(i - 1, j);
Cells(i, j + 1);
Cells(i, j - 1);
}
};
const visitCell = (i, j) => {
if (this.bombs[i][j] == "X") {
io.emit("onLost");
}
Cells(i, j);
this.visited[i][j] = 1;
setVisited([...this.visited]);
};