Skip to content

Commit

Permalink
refactor: use enum
Browse files Browse the repository at this point in the history
  • Loading branch information
lautarodragan committed Jul 30, 2020
1 parent cf88db9 commit 5f45b37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
14 changes: 8 additions & 6 deletions lib/src/cell.ts
Original file line number Diff line number Diff line change
@@ -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,
}
40 changes: 18 additions & 22 deletions web/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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 })
}
}
Expand All @@ -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) => {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'
}
Expand Down

0 comments on commit 5f45b37

Please sign in to comment.