diff --git a/src/components/Grid/Grid.tsx b/src/components/Grid/Grid.tsx index be82865..6195b38 100644 --- a/src/components/Grid/Grid.tsx +++ b/src/components/Grid/Grid.tsx @@ -16,7 +16,8 @@ import { useActions } from './hooks/useActions'; // --------------------------------------------------------------------------------------------------------------------- // ------------------------------------------------------ Utils -------------------------------------------------------- -import { genGrid, getCellDisplayContent } from '../../utils/game'; +import { genGrid } from '../../utils/generation'; +import { getCellDisplayContent } from '../../utils/gameInteractions'; // --------------------------------------------------------------------------------------------------------------------- // ----------------------------------------------------- Assets -------------------------------------------------------- diff --git a/src/components/Grid/hooks/useActions.ts b/src/components/Grid/hooks/useActions.ts index 7255803..b2ebda2 100644 --- a/src/components/Grid/hooks/useActions.ts +++ b/src/components/Grid/hooks/useActions.ts @@ -13,7 +13,8 @@ import { GameState } from '../../../types/gameState'; // --------------------------------------------------------------------------------------------------------------------- // ------------------------------------------------------ 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>, gameState: GameState, setGameState: SetterOrUpdater) => { diff --git a/src/utils/gameInteractions.tsx b/src/utils/gameInteractions.tsx new file mode 100644 index 0000000..eaae51d --- /dev/null +++ b/src/utils/gameInteractions.tsx @@ -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 ( + flag + ); + } + + if (!cell.hidden) { + if (cell.value === 'bomb') { + return ( + bomb + ); + } + if (cell.value !== 'empty') { + return cell.value; + } + } +}; diff --git a/src/utils/game.tsx b/src/utils/generation.ts similarity index 56% rename from src/utils/game.tsx rename to src/utils/generation.ts index 27afcff..63d4c71 100644 --- a/src/utils/game.tsx +++ b/src/utils/generation.ts @@ -1,7 +1,6 @@ -import { Difficulty, CellValue, GridType } from '../types/game'; import { getRandomArbitrary } from './generics'; -import Bomb from '../assets/bomb.png'; -import Flag from '../assets/flag.png'; +import { Difficulty, CellValue, GridType } from '../types/game'; +import { checkAroundCell } from './gameInteractions'; export const genGrid = (size: number): GridType => { let grid: GridType = Array.apply(null, Array(size)).map(() => { @@ -17,7 +16,7 @@ export const genGrid = (size: number): GridType => { return grid; }; -export const genBombs = (grid: GridType, difficulty: Difficulty) => { +const genBombs = (grid: GridType, difficulty: Difficulty) => { const surface = grid.length * grid.length; let bombs = 0; @@ -60,7 +59,7 @@ export const genBombs = (grid: GridType, difficulty: Difficulty) => { 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 col = 0; col < grid[row].length; col++) { if (grid[row][col].value === 'bomb') { @@ -84,56 +83,6 @@ export const genNumbers = (grid: GridType): GridType => { 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 } => { let grid = genGrid(size); @@ -162,28 +111,3 @@ export const startGame = (size: number, difficulty: Difficulty, needEmpty: boole return { grid, bombsCount }; }; - -export const getCellDisplayContent = (cell: { hidden: boolean; flag: boolean; value: CellValue }) => { - if (cell.hidden && cell.flag) { - return ( - flag - ); - } - - if (!cell.hidden) { - if (cell.value === 'bomb') { - return ( - bomb - ); - } - if (cell.value !== 'empty') { - return cell.value; - } - } -};