Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/core/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { times } from "ramda";
import times from '../lib/times.mjs';

/*
mines opened
Expand Down Expand Up @@ -32,7 +32,6 @@ export const getCellNeighborMines = (cell = 0) => cell >> 3;
// export const unflagCell = (cell = 0) => cell & (~CELL_FLAGGED);
export const toggleCellFlag = (cell = 0) => cell ^ CELL_FLAGGED;


export function getFreeRandomCell([width, height], flatBusyCells) {
const sortedBusyCells = flatBusyCells
.slice(0)
Expand Down Expand Up @@ -88,7 +87,7 @@ export const getNeighborCells = ([width, height], i) => {
export const calcHeatMap = ([width, height], mines = []) => {
const map = [];

mines.forEach(cell => {
mines.forEach((cell) => {
const col = cell % width;
const row = Math.floor(cell / width);

Expand All @@ -114,7 +113,7 @@ export function genCells(arena, minesCount, clicked) {
const heatMap = calcHeatMap(arena, mines);

const cells = times(
i => (heatMap[i] << 3) + (+mines.includes(i) << 1),
(i) => (heatMap[i] << 3) + (+mines.includes(i) << 1),
arena[0] * arena[1]
);

Expand Down
9 changes: 9 additions & 0 deletions src/lib/times.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function times(fn, n) {
const result = new Array(n);

for (let i = 0; i < n; i++) {
result[i] = fn(i);
}

return result;
}
27 changes: 27 additions & 0 deletions src/lib/times.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';
import times from './times.mjs';
// import { times } from 'ramda';

describe('Runs function', () => {
it('0 times', ({ mock }) => {
const fn = mock.fn((i) => i * 2);
const result = times(fn, 0);
assert.strictEqual(fn.mock.callCount(), 0);
assert.deepStrictEqual(result, []);
});

it('1 time', ({ mock }) => {
const fn = mock.fn((i) => i * 2);
const result = times(fn, 1);
assert.strictEqual(fn.mock.callCount(), 1);
assert.deepStrictEqual(result, [0]);
});

it('8 times', ({ mock }) => {
const fn = mock.fn((i) => i * 2);
const result = times(fn, 8);
assert.strictEqual(fn.mock.callCount(), 8);
assert.deepStrictEqual(result, [0, 2, 4, 6, 8, 10, 12, 14]);
});
});