Add parseInt
All checks were successful
gitea/minesweeper-backend/pipeline/head This commit looks good
All checks were successful
gitea/minesweeper-backend/pipeline/head This commit looks good
This commit is contained in:
parent
316a4ebe8d
commit
08cf12ad99
45
index.js
45
index.js
@ -65,7 +65,7 @@ const handleDisconnect = (socket, playerRoomMap) => {
|
||||
|
||||
const handleCreateRoom = (socket, data) => {
|
||||
const { username, rows, cols, bombs, autoflag, gameMode } = data;
|
||||
const room = buildRoom(socket.id, username, rows, cols, bombs, autoflag, gameMode);
|
||||
const room = buildRoom(socket.id, username, parseInt(rows), parseInt(cols), parseInt(bombs), autoflag, gameMode);
|
||||
rooms.set(room.id, room);
|
||||
playerRoomMap.set(socket.id, room);
|
||||
console.log('created room', room.id);
|
||||
@ -384,17 +384,38 @@ const flagBombs = (board) => {
|
||||
const getSurroundingTiles = (board, idx) => {
|
||||
const { rows, cols } = board;
|
||||
|
||||
// Thanks copilot for writing this garbage so that I don't have to <3
|
||||
return [
|
||||
idx % cols !== 0 && board.tiles[idx - cols - 1], // top-left
|
||||
board.tiles[idx - cols], // top
|
||||
(idx + 1) % cols !== 0 && board.tiles[idx - cols + 1], // top-right
|
||||
idx % cols !== 0 && board.tiles[idx - 1], // left
|
||||
(idx + 1) % cols !== 0 && board.tiles[idx + 1], // right
|
||||
idx % cols !== 0 && board.tiles[idx + cols - 1], // bottom-left
|
||||
board.tiles[idx + cols], // bottom
|
||||
(idx + 1) % cols !== 0 && board.tiles[idx + cols + 1] // bottom-right
|
||||
].filter(tile => tile);
|
||||
const neighbors = [];
|
||||
|
||||
const isTopBorder = idx < cols;
|
||||
const isBottomBorder = idx >= (rows - 1) * cols;
|
||||
const isLeftBorder = idx % cols === 0;
|
||||
const isRightBorder = (idx + 1) % cols === 0;
|
||||
|
||||
// Top left
|
||||
neighbors.push(!isTopBorder && !isLeftBorder ? board.tiles[idx - cols - 1] : undefined);
|
||||
|
||||
// Top
|
||||
neighbors.push(!isTopBorder ? board.tiles[idx - cols] : undefined);
|
||||
|
||||
// Top right
|
||||
neighbors.push(!isTopBorder && !isRightBorder ? board.tiles[idx - cols + 1] : undefined);
|
||||
|
||||
// Left
|
||||
neighbors.push(!isLeftBorder ? board.tiles[idx - 1] : undefined);
|
||||
|
||||
// Right
|
||||
neighbors.push(!isRightBorder ? board.tiles[idx + 1] : undefined);
|
||||
|
||||
// Bottom left
|
||||
neighbors.push(!isBottomBorder && !isLeftBorder ? board.tiles[idx + cols - 1] : undefined);
|
||||
|
||||
// Bottom
|
||||
neighbors.push(!isBottomBorder ? board.tiles[idx + cols] : undefined);
|
||||
|
||||
// Bottom right
|
||||
neighbors.push(!isBottomBorder && !isRightBorder ? board.tiles[idx + cols + 1] : undefined);
|
||||
|
||||
return neighbors.filter(tile => tile != undefined);
|
||||
};
|
||||
|
||||
const checkWinCondition = (players, board) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user