Skip to content

Commit

Permalink
Testing the field grid logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zourdyzou committed Jul 3, 2022
1 parent f57952e commit e05c92f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "minesweeper",
"version": "1.0.0",
"description": "Minesweeper game",
"main": "webpack.config.js",
"main": "webpack.config.babel.js",
"scripts": {
"clean": "rm -rf node_modules coverage dist",
"clean:dist": "rm -rf dist",
"test": "jest --coverage",
"test": "jest",
"build": "cross-env NODE_ENV=production webpack --config webpack.config.babel.js",
"profile": "cross-env NODE_ENV=production webpack --profile --json --config webpack.config.babel.js > ./dist/profile.json && webpack-bundle-analyzer ./dist/profile.json",
"start": "cross-env WEBPACK_IS_DEV_SERVER=true NODE_ENV=development webpack serve --config webpack.config.babel.js",
Expand Down
70 changes: 66 additions & 4 deletions src/components/field/field.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,92 @@
import { CellState, emptyFieldGenerator } from "./field";
import { CellState, fieldGenerator, generateFieldWithDefaultState } from "./field";

const { empty, bomb, hidden } = CellState;

describe("Field Generator", () => {
describe("emptyFieldGenerator tests", () => {
it("should go with 2x2", function () {
expect(emptyFieldGenerator(2)).toStrictEqual([
expect(generateFieldWithDefaultState(2)).toStrictEqual([
[empty, empty],
[empty, empty],
]);
});

it("should go with 3x3", function () {
expect(emptyFieldGenerator(3)).toStrictEqual([
expect(generateFieldWithDefaultState(3)).toStrictEqual([
[empty, empty, empty],
[empty, empty, empty],
[empty, empty, empty],
]);
});

it("should go with 2x2", function () {
expect(emptyFieldGenerator(3, hidden)).toStrictEqual([
expect(generateFieldWithDefaultState(3, hidden)).toStrictEqual([
[hidden, hidden, hidden],
[hidden, hidden, hidden],
[hidden, hidden, hidden],
]);
});
});

describe("Simple Case", () => {
it("should throw wrong density", function () {
const errorText = "Probability must be between 0 and 1";
expect(() => fieldGenerator(1, -1)).toThrow(errorText);
expect(() => fieldGenerator(1, 2)).toThrow(errorText);
});

it("should have a smallest possible field without mine(bomb)", function () {
expect(fieldGenerator(1, 0)).toStrictEqual([[empty]]);
});

it("should have a BIG field without mine", () => {
expect(fieldGenerator(10, 0)).toStrictEqual([
[empty, empty, empty, empty, empty, empty, empty, empty, empty, empty],
[empty, empty, empty, empty, empty, empty, empty, empty, empty, empty],
[empty, empty, empty, empty, empty, empty, empty, empty, empty, empty],
[empty, empty, empty, empty, empty, empty, empty, empty, empty, empty],
[empty, empty, empty, empty, empty, empty, empty, empty, empty, empty],
[empty, empty, empty, empty, empty, empty, empty, empty, empty, empty],
[empty, empty, empty, empty, empty, empty, empty, empty, empty, empty],
[empty, empty, empty, empty, empty, empty, empty, empty, empty, empty],
[empty, empty, empty, empty, empty, empty, empty, empty, empty, empty],
[empty, empty, empty, empty, empty, empty, empty, empty, empty, empty],
]);
});

it("should have a smallest possible field with mine(bomb)", function () {
expect(fieldGenerator(1, 1)).toStrictEqual([[bomb]]);
});

it("should have 2x2 field with mines(bomb)", function () {
expect(fieldGenerator(2, 1)).toStrictEqual([
[bomb, bomb],
[bomb, bomb],
]);
});

it("should have 2x2 field with 50% probability mines(bomb)", function () {
expect(fieldGenerator(2, 0.5)).toStrictEqual([
[bomb, bomb],
[empty, empty],
]);
});

it("should have 4x4 field with 50% of probability mines(bomb)", function () {
expect(fieldGenerator(4, 0.5)).toStrictEqual([
[bomb, bomb, bomb, bomb],
[bomb, bomb, bomb, bomb],
[empty, empty, empty, empty],
[empty, empty, empty, empty],
]);
});

it("should have 3x3 field with 50% of probability mines(bomb)", function () {
const field = fieldGenerator(3, 0.5);
const flatField = field.flat();

console.table(field);
console.table(flatField);
});
});
});
32 changes: 31 additions & 1 deletion src/components/field/field.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { resolve } from "path";

export type Cell = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
export type Field = Cell[][];
export type Coordinates = [number, number];
Expand All @@ -10,5 +12,33 @@ export const CellState: Record<string, Cell> = {
weakMark: 12,
};

export const emptyFieldGenerator = (size: number, state: Cell = CellState.empty): Field =>
export const generateFieldWithDefaultState = (size: number, state: Cell = CellState.empty): Field =>
new Array(size).fill(null).map(() => new Array(size).fill(state));

export const fieldGenerator = (size: number, probability: number): Field => {
if (probability < 0 || probability > 1) {
throw new Error("Probability must be between 0 and 1");
}

const result: Field = generateFieldWithDefaultState(size);

// the size grid of the cells
let unprocessedCells = size * size;
// cells with bombs included
let restCellsWithBombs = unprocessedCells * probability;

for (let y = 0; y < size; y++) {
for (let x = 0; x < size; x++) {
if (restCellsWithBombs === 0) {
return result;
}
if (restCellsWithBombs / unprocessedCells > 0) {
result[y][x] = CellState.bomb;
restCellsWithBombs--;
}
unprocessedCells--;
}
}

return result;
};

0 comments on commit e05c92f

Please sign in to comment.