🐛 Fix generation with less bombs than expected & wrong placed numbers

This commit is contained in:
2024-02-18 19:05:58 +01:00
parent dce5862d36
commit 21d5f64c06
2 changed files with 20 additions and 32 deletions
+2 -2
View File
@@ -16,7 +16,7 @@ import { useActions } from './hooks/useActions';
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------ Utils -------------------------------------------------------- // ------------------------------------------------------ Utils --------------------------------------------------------
import { genGrid, getCellContent } from '../../utils/game'; import { genGrid, getCellDisplayContent } from '../../utils/game';
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------- Assets -------------------------------------------------------- // ----------------------------------------------------- Assets --------------------------------------------------------
@@ -53,7 +53,7 @@ const Grid = () => {
onClick={(ev) => handleLeftClick(ev, rowIndex, cellIndex)} onClick={(ev) => handleLeftClick(ev, rowIndex, cellIndex)}
onContextMenu={(ev) => handleRightClick(ev, rowIndex, cellIndex)} onContextMenu={(ev) => handleRightClick(ev, rowIndex, cellIndex)}
> >
{getCellContent(cell)} {getCellDisplayContent(cell)}
</button> </button>
); );
})} })}
+16 -28
View File
@@ -30,43 +30,29 @@ export const genBombs = (grid: GridType, difficulty: Difficulty) => {
} }
bombs = Math.floor(bombs); bombs = Math.floor(bombs);
for (let i = 1; i <= bombs; i++) { for (let i = 1; i <= bombs; i++) {
let needRegenerate = false; let needRegenerate = false;
do { do {
needRegenerate = false;
const row = getRandomArbitrary(0, grid.length); const row = getRandomArbitrary(0, grid.length);
const col = getRandomArbitrary(0, grid.length); const col = getRandomArbitrary(0, grid.length);
let bombsArounds = 0; let bombsArounds = 0;
for (let testRowIndex = -1; testRowIndex <= 1; testRowIndex++) { if (grid[row][col].value === 'bomb') {
const testedRow = row + testRowIndex; continue;
}
if (testedRow < 0) continue; checkAroundCell(grid, row, col, (cell: { hidden: boolean; flag: boolean; value: CellValue }) => {
if (testedRow >= grid.length) continue; if (cell.value === 'bomb') {
for (let testColIndex = -1; testColIndex <= 1; testColIndex++) {
const testedCol = col + testColIndex;
if (testedCol < 0) continue;
if (testedCol >= grid.length) continue;
if (testedRow === row && testedCol === col) continue;
if (grid[testedRow][testedCol].value === 'bomb') {
bombsArounds++; bombsArounds++;
if (bombsArounds > 3) {
needRegenerate = true;
break;
}
}
}
if (needRegenerate) break;
} }
});
if (bombsArounds <= 3) { if (bombsArounds <= 3) {
grid[row][col].value = 'bomb'; grid[row][col].value = 'bomb';
needRegenerate = false;
} }
} while (needRegenerate); } while (needRegenerate);
} }
@@ -77,9 +63,13 @@ export const genBombs = (grid: GridType, difficulty: Difficulty) => {
export const genNumbers = (grid: GridType): GridType => { export const genNumbers = (grid: GridType): GridType => {
for (let row = 0; row < grid.length; row++) { for (let row = 0; row < grid.length; row++) {
for (let col = 0; col < grid[row].length; col++) { for (let col = 0; col < grid[row].length; col++) {
if (grid[row][col].value === 'bomb') {
continue;
}
let bombsArounds = 0; let bombsArounds = 0;
checkAroundCell(grid, row, col, (testedCell: { hidden: boolean; value: CellValue }, testedRow: number, testedCol: number) => { checkAroundCell(grid, row, col, (testedCell: { hidden: boolean; value: CellValue }) => {
if (testedCell.value === 'bomb') { if (testedCell.value === 'bomb') {
bombsArounds++; bombsArounds++;
} }
@@ -168,14 +158,12 @@ export const startGame = (size: number, difficulty: Difficulty, needEmpty: boole
} }
const { grid: bombsGrid, bombsCount } = genBombs(grid, difficulty); const { grid: bombsGrid, bombsCount } = genBombs(grid, difficulty);
grid = bombsGrid; grid = genNumbers(bombsGrid);
grid = genNumbers(grid);
return { grid, bombsCount }; return { grid, bombsCount };
}; };
export const getCellContent = (cell: { hidden: boolean; flag: boolean; value: CellValue }) => { export const getCellDisplayContent = (cell: { hidden: boolean; flag: boolean; value: CellValue }) => {
if (cell.hidden && cell.flag) { if (cell.hidden && cell.flag) {
return ( return (
<img <img