From 5f45b37a424d8d8adaec94665d291696b8bbf5ff Mon Sep 17 00:00:00 2001 From: Lautaro Dragan Date: Thu, 30 Jul 2020 19:56:34 -0300 Subject: [PATCH] refactor: use enum --- lib/src/cell.ts | 14 ++++++++------ web/pages/index.js | 40 ++++++++++++++++++---------------------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/lib/src/cell.ts b/lib/src/cell.ts index 2800a48..0ec7915 100644 --- a/lib/src/cell.ts +++ b/lib/src/cell.ts @@ -1,6 +1,8 @@ -export const CELL_UNKNOWN_CLEAR = 0 -export const CELL_UNKNOWN_MINE = 1 -export const CELL_KNOWN_CLEAR = 2 -export const CELL_KNOWN_MINE = 3 -export const CELL_UNKNOWN_CLEAR_FLAG = 4 -export const CELL_UNKNOWN_MINE_FLAG = 5 +export enum CellValue { + UnknownClear, + UnknownMine, + KnownClear, + KnownMine, + UnknownClearFlag, + UnknownMineFlag, +} diff --git a/web/pages/index.js b/web/pages/index.js index b07a669..f6b8922 100644 --- a/web/pages/index.js +++ b/web/pages/index.js @@ -4,11 +4,7 @@ import dynamic from 'next/dynamic' import React, { useState, useEffect, useRef } from 'react' import { - CELL_KNOWN_CLEAR, - CELL_UNKNOWN_CLEAR, - CELL_UNKNOWN_CLEAR_FLAG, - CELL_UNKNOWN_MINE, - CELL_UNKNOWN_MINE_FLAG, + CellValue, getFlagCount, getSurroundingFlagCount, getSurroundingMineCount, @@ -24,7 +20,7 @@ const NoSsr = dynamic(() => Promise.resolve(({ children }) => <>{children}), }) const getCellText = (board, x, y) => { - if (board[y][x] !== CELL_KNOWN_CLEAR) + if (board[y][x] !== CellValue.KnownClear) return '' const surroundingMineCount = getSurroundingMineCount(board, x, y) if (surroundingMineCount < 1) @@ -85,11 +81,11 @@ export default function Home() { if (won || lostPosition) return - if (value === CELL_UNKNOWN_CLEAR) { + if (value === CellValue.UnknownClear) { if (startTime === null) startTimeTracker() setBoard(recursiveSolve(board, x, y)) - } else if (value === CELL_UNKNOWN_MINE) { + } else if (value === CellValue.UnknownMine) { lose({ x, y }) } } @@ -100,14 +96,14 @@ export default function Home() { if (won || lostPosition) return - if (board[y][x] === CELL_UNKNOWN_CLEAR) - setCell(x, y, CELL_UNKNOWN_CLEAR_FLAG) - else if (board[y][x] === CELL_UNKNOWN_MINE) - setCell(x, y, CELL_UNKNOWN_MINE_FLAG) - else if (board[y][x] === CELL_UNKNOWN_CLEAR_FLAG) - setCell(x, y, CELL_UNKNOWN_CLEAR) - else if (board[y][x] === CELL_UNKNOWN_MINE_FLAG) - setCell(x, y, CELL_UNKNOWN_MINE) + if (board[y][x] === CellValue.UnknownClear) + setCell(x, y, CellValue.UnknownClearFlag) + else if (board[y][x] === CellValue.UnknownMine) + setCell(x, y, CellValue.UnknownMineFlag) + else if (board[y][x] === CellValue.UnknownClearFlag) + setCell(x, y, CellValue.UnknownClear) + else if (board[y][x] === CellValue.UnknownMineFlag) + setCell(x, y, CellValue.UnknownMine) } const onMouseDown = (x, y, value) => (event) => { @@ -138,7 +134,7 @@ export default function Home() { setSweeperPosition(null) - if (event.button !== 0 || board[y][x] !== CELL_KNOWN_CLEAR) + if (event.button !== 0 || board[y][x] !== CellValue.KnownClear) return const surroundingMineCount = getSurroundingMineCount(board, x, y) @@ -172,20 +168,20 @@ export default function Home() { const getClassNameForCell = (x, y, value) => { if (lostPosition && lostPosition.x === x && lostPosition.y === y) return 'lost' - if ((value === CELL_UNKNOWN_MINE || value === CELL_UNKNOWN_CLEAR) + if ((value === CellValue.UnknownMine || value === CellValue.UnknownClear) && sweeperPosition && sweeperPosition.x >= x - 1 && sweeperPosition.x <= x + 1 && sweeperPosition.y >= y - 1 && sweeperPosition.y <= y + 1) return 'clear' - if (lostPosition && value === CELL_UNKNOWN_MINE ) + if (lostPosition && value === CellValue.UnknownMine ) return 'mine' - if (!lostPosition && value === CELL_UNKNOWN_MINE) + if (!lostPosition && value === CellValue.UnknownMine) return cheatSeeMines ? 'unknown mine' : 'unknown' - if (value === CELL_KNOWN_CLEAR) + if (value === CellValue.KnownClear) return 'clear' - if (value === CELL_UNKNOWN_CLEAR_FLAG || value === CELL_UNKNOWN_MINE_FLAG) + if (value === CellValue.UnknownClearFlag || value === CellValue.UnknownMineFlag) return 'unknown flag' return 'unknown' }