Skip to content

Commit

Permalink
Refactoring with stryker
Browse files Browse the repository at this point in the history
  • Loading branch information
nickovchinnikov committed Sep 28, 2021
1 parent f90aea3 commit 3f0cdf7
Show file tree
Hide file tree
Showing 9 changed files with 1,838 additions and 35 deletions.
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module.exports = {
'^@/(.*)$': '<rootDir>/src/$1',
},

modulePathIgnorePatterns: ['<rootDir>/.stryker-tmp'],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ['<rootDir>/settings/JestSetup.js'],

Expand Down
13 changes: 8 additions & 5 deletions src/helpers/detectSolvedPullze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,36 @@ export const detectSolvedPuzzle = (
let bombsCounter = 0;
let flagCounter = 0;
let detectedBombsCounter = 0;
let hiddenCounter = 0;
let isFieldHaveHiddenCells = false;

for (const y of gameField.keys()) {
for (const x of gameField[y].keys()) {
const gameCell = gameField[y][x];
const playerCell = playerField[y][x];
const isPlayerCellFlag = [flag, weakFlag].includes(playerCell);

if (playerCell === hidden) {
hiddenCounter++;
isFieldHaveHiddenCells = true;
}

if ([flag, weakFlag].includes(playerCell)) {
if (isPlayerCellFlag) {
flagCounter++;
}

if (gameCell === bomb) {
bombsCounter++;

if (playerCell === flag) {
if (isPlayerCellFlag) {
detectedBombsCounter++;
}
}
}
}

const isPuzzleSolved =
bombsCounter === detectedBombsCounter && hiddenCounter === 0;
bombsCounter === detectedBombsCounter &&
flagCounter === bombsCounter &&
!isFieldHaveHiddenCells;

return [isPuzzleSolved, flagCounter];
};
44 changes: 40 additions & 4 deletions src/helpers/detectSolvedPuzzle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,57 @@ describe('Detect solved puzzle function test cases', () => {
expect(flagCounter).toBe(0);
expect(isSolved).toBe(false);
});
it('Wrong 3*3 hidden cell case', () => {
it('Wrong 3*3 without hidden cells case', () => {
const gameField: Field = [
[1, 1, e],
[b, 1, e],
[1, 1, e],
];

const playerField: Field = [
[1, h, e],
[1, f, e],
[f, 1, e],
[1, 1, e],
];

const [isSolved, flagCounter] = detectSolvedPuzzle(playerField, gameField);

expect(flagCounter).toBe(2);
expect(isSolved).toBe(false);
});
it('Loose 3*3 case', () => {
const gameField: Field = [
[1, 1, e],
[b, 1, e],
[1, 1, e],
];

const playerField: Field = [
[1, 1, e],
[b, 1, e],
[1, 1, e],
];

const [isSolved, flagCounter] = detectSolvedPuzzle(playerField, gameField);

expect(flagCounter).toBe(0);
expect(isSolved).toBe(false);
});
it('Wrong flag on 3*3 case', () => {
const gameField: Field = [
[1, 1, e],
[b, 1, e],
[1, 1, e],
];

const playerField: Field = [
[1, f, e],
[b, 1, e],
[1, 1, e],
];

const [isSolved, flagCounter] = detectSolvedPuzzle(playerField, gameField);

expect(flagCounter).toBe(1);
expect(isSolved).toBe(false);
});
Expand All @@ -71,13 +107,13 @@ describe('Detect solved puzzle function test cases', () => {
[f, f, 1, h, h],
[f, 3, 1, h, h],
[1, 1, h, h, h],
[1, h, h, h, h],
[1, h, h, h, f],
[2, h, h, h, h],
];

const [isSolved, flagCounter] = detectSolvedPuzzle(playerField, gameField);

expect(flagCounter).toBe(3);
expect(flagCounter).toBe(4);
expect(isSolved).toStrictEqual(false);
});
it('5*5 solved case', () => {
Expand Down
47 changes: 44 additions & 3 deletions src/helpers/openCell.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CellState } from './Field';
import { openCell } from './openCell';

const { empty: e, hidden: h, bomb: b, flag: f } = CellState;
const { empty: e, hidden: h, bomb: b, weakFlag: w, flag: f } = CellState;

describe('Open cell action', () => {
describe('Simple cases with loose', () => {
Expand All @@ -20,6 +20,48 @@ describe('Open cell action', () => {
)
).toThrow('Game Over');
});
it("Open cell with the flag it shouldn't open", () => {
const [playerField, isSolved] = openCell(
[1, 1],
[
[h, h, h],
[h, f, h],
[h, h, h],
],
[
[1, 1, 0],
[9, 1, 0],
[1, 1, 0],
]
);

expect(isSolved).toBe(false);
expect(playerField).toStrictEqual([
[h, h, h],
[h, f, h],
[h, h, h],
]);
});
it('Open cell with the weak flag should open', () => {
const [playerField] = openCell(
[1, 1],
[
[h, h, h],
[h, w, h],
[h, h, h],
],
[
[1, 1, 0],
[9, 1, 0],
[1, 1, 0],
]
);
expect(playerField).toStrictEqual([
[h, h, h],
[h, 1, h],
[h, h, h],
]);
});
});
describe('Open cell with number', () => {
it('Open cell with state == 1', () => {
Expand Down Expand Up @@ -113,7 +155,7 @@ describe('Open cell action', () => {
});
describe('Detect win state', () => {
it('5*5 solved case', () => {
const [playerField, isSolved, flagCounter] = openCell(
const [playerField, isSolved] = openCell(
[4, 0],
[
[f, f, 1, 1, 2],
Expand All @@ -131,7 +173,6 @@ describe('Open cell action', () => {
]
);

expect(flagCounter).toBe(4);
expect(isSolved).toStrictEqual(true);
expect(playerField).toStrictEqual([
[f, f, 1, 1, 2],
Expand Down
26 changes: 12 additions & 14 deletions src/helpers/openCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,34 @@ export const openCell = (
coords: Coords,
playerField: Field,
gameField: Field
): [Field, boolean, number] => {
const { empty, hidden, bomb } = CellState;
): [Field, boolean] => {
const { empty, hidden, bomb, weakFlag, flag } = CellState;

const [y, x] = coords;
const gameCell = gameField[y][x];
const playerCell = playerField[y][x];

if (gameCell === bomb) {
throw new Error('Game Over');
}

if (gameCell === empty) {
playerField[y][x] = gameCell;
if (flag === playerCell) {
return [playerField, false];
}

playerField[y][x] = gameCell;

if (gameCell === empty && [hidden, weakFlag].includes(playerCell)) {
const items = getNeigboursItems(coords);

for (const [y, x] of Object.values(items)) {
if (checkItemInField([y, x], gameField)) {
const playerCell = playerField[y][x];
const gameCell = gameField[y][x];

if (playerCell === hidden && gameCell !== bomb) {
[playerField] = openCell([y, x], playerField, gameField);
}
[playerField] = openCell([y, x], playerField, gameField);
}
}
}

playerField[y][x] = gameCell;

const [isSolved, flagCounter] = detectSolvedPuzzle(playerField, gameField);
const [isSolved] = detectSolvedPuzzle(playerField, gameField);

return [playerField, isSolved, flagCounter];
return [playerField, isSolved];
};
13 changes: 13 additions & 0 deletions src/modules/GameWithHooks/GameWithHooks.snap.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';

import { GameWithHooks } from './GameWithHooks';

it('Render game field by default', () => {
const { asFragment } = render(<GameWithHooks />);
userEvent.click(screen.getByTestId('0,0'), { button: 2 });
expect(asFragment()).toMatchSnapshot();
userEvent.click(screen.getByTestId('8,8'), { button: 2 });
expect(asFragment()).toMatchSnapshot();
});
Loading

0 comments on commit 3f0cdf7

Please sign in to comment.