🎨 Splitted utils into generation and gameInteractions

This commit is contained in:
2024-02-18 21:57:05 +01:00
parent 4b4473e2b2
commit b19c665ab0
4 changed files with 86 additions and 82 deletions
+2 -1
View File
@@ -16,7 +16,8 @@ import { useActions } from './hooks/useActions';
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------ Utils -------------------------------------------------------- // ------------------------------------------------------ Utils --------------------------------------------------------
import { genGrid, getCellDisplayContent } from '../../utils/game'; import { genGrid } from '../../utils/generation';
import { getCellDisplayContent } from '../../utils/gameInteractions';
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------- Assets -------------------------------------------------------- // ----------------------------------------------------- Assets --------------------------------------------------------
+2 -1
View File
@@ -13,7 +13,8 @@ import { GameState } from '../../../types/gameState';
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------ Utils -------------------------------------------------------- // ------------------------------------------------------ Utils --------------------------------------------------------
import { discoverAroundCell, revealAllGrid, startGame } from '../../../utils/game'; import { discoverAroundCell, revealAllGrid } from '../../../utils/gameInteractions';
import { startGame } from '../../../utils/generation';
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
export const useActions = (grid: GridType, setGrid: React.Dispatch<React.SetStateAction<GridType>>, gameState: GameState, setGameState: SetterOrUpdater<GameState>) => { export const useActions = (grid: GridType, setGrid: React.Dispatch<React.SetStateAction<GridType>>, gameState: GameState, setGameState: SetterOrUpdater<GameState>) => {
+78
View File
@@ -0,0 +1,78 @@
import { CellValue, GridType } from '../types/game';
import Bomb from '../assets/bomb.png';
import Flag from '../assets/flag.png';
export const checkAroundCell = (grid: GridType, row: number, col: number, callback: Function): void => {
for (let testRowIndex = -1; testRowIndex <= 1; testRowIndex++) {
const testedRow = row + testRowIndex;
if (testedRow < 0) continue;
if (testedRow >= grid.length) continue;
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;
callback(grid[testedRow][testedCol], testedRow, testedCol);
}
}
};
export const discoverAroundCell = (grid: GridType, rowIndex: number, colIndex: number): GridType => {
grid[rowIndex][colIndex].hidden = false;
checkAroundCell(grid, rowIndex, colIndex, (cell: { hidden: boolean; value: CellValue; flag: boolean }, testedRowIndex: number, testedColIndex: number) => {
if (!cell.flag && cell.hidden) {
if (cell.value === 'empty') {
grid = discoverAroundCell(grid, testedRowIndex, testedColIndex);
} else if (typeof cell.value === 'number') {
grid[testedRowIndex][testedColIndex].hidden = false;
}
}
});
return grid;
};
export const revealAllGrid = (grid: GridType, excludeGoodFlags: boolean = true) => {
return grid.map((row) => {
return row.map((col) => {
if (excludeGoodFlags && col.value === 'bomb' && col.flag) {
return col;
}
return {
...col,
hidden: false,
flag: false,
};
});
});
};
export const getCellDisplayContent = (cell: { hidden: boolean; flag: boolean; value: CellValue }) => {
if (cell.hidden && cell.flag) {
return (
<img
src={Flag}
alt='flag'
/>
);
}
if (!cell.hidden) {
if (cell.value === 'bomb') {
return (
<img
src={Bomb}
alt='bomb'
/>
);
}
if (cell.value !== 'empty') {
return cell.value;
}
}
};
+4 -80
View File
@@ -1,7 +1,6 @@
import { Difficulty, CellValue, GridType } from '../types/game';
import { getRandomArbitrary } from './generics'; import { getRandomArbitrary } from './generics';
import Bomb from '../assets/bomb.png'; import { Difficulty, CellValue, GridType } from '../types/game';
import Flag from '../assets/flag.png'; import { checkAroundCell } from './gameInteractions';
export const genGrid = (size: number): GridType => { export const genGrid = (size: number): GridType => {
let grid: GridType = Array.apply(null, Array(size)).map(() => { let grid: GridType = Array.apply(null, Array(size)).map(() => {
@@ -17,7 +16,7 @@ export const genGrid = (size: number): GridType => {
return grid; return grid;
}; };
export const genBombs = (grid: GridType, difficulty: Difficulty) => { const genBombs = (grid: GridType, difficulty: Difficulty) => {
const surface = grid.length * grid.length; const surface = grid.length * grid.length;
let bombs = 0; let bombs = 0;
@@ -60,7 +59,7 @@ export const genBombs = (grid: GridType, difficulty: Difficulty) => {
return { grid: grid, bombsCount: bombs }; return { grid: grid, bombsCount: bombs };
}; };
export const genNumbers = (grid: GridType): GridType => { 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') { if (grid[row][col].value === 'bomb') {
@@ -84,56 +83,6 @@ export const genNumbers = (grid: GridType): GridType => {
return grid; return grid;
}; };
export const checkAroundCell = (grid: GridType, row: number, col: number, callback: Function): void => {
for (let testRowIndex = -1; testRowIndex <= 1; testRowIndex++) {
const testedRow = row + testRowIndex;
if (testedRow < 0) continue;
if (testedRow >= grid.length) continue;
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;
callback(grid[testedRow][testedCol], testedRow, testedCol);
}
}
};
export const discoverAroundCell = (grid: GridType, rowIndex: number, colIndex: number): GridType => {
grid[rowIndex][colIndex].hidden = false;
checkAroundCell(grid, rowIndex, colIndex, (cell: { hidden: boolean; value: CellValue; flag: boolean }, testedRowIndex: number, testedColIndex: number) => {
if (!cell.flag && cell.hidden) {
if (cell.value === 'empty') {
grid = discoverAroundCell(grid, testedRowIndex, testedColIndex);
} else if (typeof cell.value === 'number') {
grid[testedRowIndex][testedColIndex].hidden = false;
}
}
});
return grid;
};
export const revealAllGrid = (grid: GridType, excludeGoodFlags: boolean = true) => {
return grid.map((row) => {
return row.map((col) => {
if (excludeGoodFlags && col.value === 'bomb' && col.flag) {
return col;
}
return {
...col,
hidden: false,
flag: false,
};
});
});
};
export const startGame = (size: number, difficulty: Difficulty, needEmpty: boolean = true, rowIndex?: number, colIndex?: number): { grid: GridType; bombsCount: number } => { export const startGame = (size: number, difficulty: Difficulty, needEmpty: boolean = true, rowIndex?: number, colIndex?: number): { grid: GridType; bombsCount: number } => {
let grid = genGrid(size); let grid = genGrid(size);
@@ -162,28 +111,3 @@ export const startGame = (size: number, difficulty: Difficulty, needEmpty: boole
return { grid, bombsCount }; return { grid, bombsCount };
}; };
export const getCellDisplayContent = (cell: { hidden: boolean; flag: boolean; value: CellValue }) => {
if (cell.hidden && cell.flag) {
return (
<img
src={Flag}
alt='flag'
/>
);
}
if (!cell.hidden) {
if (cell.value === 'bomb') {
return (
<img
src={Bomb}
alt='bomb'
/>
);
}
if (cell.value !== 'empty') {
return cell.value;
}
}
};