Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nick/detect solved puzzle #33

Merged
merged 5 commits into from
Aug 20, 2021
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,9 @@
### Set flag action

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/32/files)

### Solved puzzle detector
### Create win game state handler
### Add test cases for the useGame hook win state

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/33/files)
47 changes: 47 additions & 0 deletions src/helpers/detectSolvedPullze.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { CellState, Field } from './Field';

/**
* Detect solved puzzle based on the player and game fields coorelation
* @param {Field} playerField
* @param {Field} gameField
* @returns {[boolean, number]}
*/
export const detectSolvedPuzzle = (
playerField: Field,
gameField: Field
): [boolean, number] => {
const { hidden, bomb, flag, weakFlag } = CellState;

let bombsCounter = 0;
let flagCounter = 0;
let detectedBombsCounter = 0;
let hiddenCounter = 0;

for (const y of gameField.keys()) {
for (const x of gameField[y].keys()) {
const gameCell = gameField[y][x];
const playerCell = playerField[y][x];

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

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

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

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

const isPuzzleSolved =
bombsCounter === detectedBombsCounter && hiddenCounter === 0;

return [isPuzzleSolved, flagCounter];
};
105 changes: 105 additions & 0 deletions src/helpers/detectSolvedPuzzle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { CellState, Field } from './Field';
import { detectSolvedPuzzle } from './detectSolvedPullze';

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

describe('Detect solved puzzle function test cases', () => {
it('Simplest 3*3 case', () => {
const gameField: Field = [
[1, 1, e],
[b, 1, e],
[1, 1, e],
];

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

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

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

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

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

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

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

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

expect(flagCounter).toBe(1);
expect(isSolved).toBe(false);
});
it('5*5 with hidden cells', () => {
const gameField: Field = [
[9, 9, 1, 1, 2],
[9, 3, 1, 0, 0],
[1, 1, 0, 1, 1],
[1, 0, 0, 1, 9],
[2, 1, 0, 1, 0],
];

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

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

expect(flagCounter).toBe(3);
expect(isSolved).toStrictEqual(false);
});
it('5*5 solved case', () => {
const gameField: Field = [
[9, 9, 1, 1, 2],
[9, 3, 1, 0, 0],
[1, 1, 0, 1, 1],
[1, 0, 0, 1, 9],
[2, 1, 0, 1, 0],
];

const playerField: Field = [
[f, f, 1, 1, 2],
[f, 3, 1, 0, 0],
[1, 1, 0, 1, 1],
[1, 0, 0, 1, f],
[2, 1, 0, 1, 0],
];

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

expect(flagCounter).toBe(4);
expect(isSolved).toStrictEqual(true);
});
});
41 changes: 36 additions & 5 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 } = CellState;
const { empty: e, hidden: h, bomb: b, flag: f } = CellState;

describe('Open cell action', () => {
describe('Simple cases with loose', () => {
Expand All @@ -23,7 +23,7 @@ describe('Open cell action', () => {
});
describe('Open cell with number', () => {
it('Open cell with state == 1', () => {
const playerField = openCell(
const [playerField] = openCell(
[1, 1],
[
[h, h, h],
Expand All @@ -43,7 +43,7 @@ describe('Open cell action', () => {
]);
});
it('Open cell with state == 3', () => {
const playerField = openCell(
const [playerField] = openCell(
[1, 1],
[
[h, h, h],
Expand All @@ -65,7 +65,7 @@ describe('Open cell action', () => {
});
describe('Open empty cell', () => {
it('Open empty cell, simple 3*3 case', () => {
const playerField = openCell(
const [playerField] = openCell(
[1, 2],
[
[h, h, h],
Expand All @@ -85,7 +85,7 @@ describe('Open cell action', () => {
]);
});
it('Open empty cell 5*5 case', () => {
const playerField = openCell(
const [playerField] = openCell(
[2, 2],
[
[h, h, h, h, h],
Expand All @@ -111,4 +111,35 @@ describe('Open cell action', () => {
]);
});
});
describe('Detect win state', () => {
it('5*5 solved case', () => {
const [playerField, isSolved, flagCounter] = openCell(
[4, 0],
[
[f, f, 1, 1, 2],
[f, 3, 1, 0, 0],
[1, 1, 0, 1, 1],
[1, 0, 0, 1, f],
[h, 1, 0, 1, 0],
],
[
[9, 9, 1, 1, 2],
[9, 3, 1, 0, 0],
[1, 1, 0, 1, 1],
[1, 0, 0, 1, 9],
[2, 1, 0, 1, 0],
]
);

expect(flagCounter).toBe(4);
expect(isSolved).toStrictEqual(true);
expect(playerField).toStrictEqual([
[f, f, 1, 1, 2],
[f, 3, 1, 0, 0],
[1, 1, 0, 1, 1],
[1, 0, 0, 1, f],
[2, 1, 0, 1, 0],
]);
});
});
});
11 changes: 7 additions & 4 deletions src/helpers/openCell.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { CellState, Coords, Field } from './Field';
import { checkItemInField, getNeigboursItems } from './CellsManipulator';
import { detectSolvedPuzzle } from './detectSolvedPullze';

/**
* Open cell in the player field using game field info
* @param {Coords} coords
* @param {Field} playerField
* @param {Field} gameField
* @returns {Field}
* @returns {[Field, boolean, number]}
*/
export const openCell = (
coords: Coords,
playerField: Field,
gameField: Field
): Field => {
): [Field, boolean, number] => {
const { empty, hidden, bomb } = CellState;

const [y, x] = coords;
Expand All @@ -33,13 +34,15 @@ export const openCell = (
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;

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

return [playerField, isSolved, flagCounter];
};
39 changes: 35 additions & 4 deletions src/helpers/setFlag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Set flag action', () => {
[h, h, h],
];

const newPlayerField = setFlag([0, 0], playerField, gameField);
const [newPlayerField] = setFlag([0, 0], playerField, gameField);

expect(newPlayerField).toStrictEqual([
[1, h, h],
Expand All @@ -38,7 +38,7 @@ describe('Set flag action', () => {
[h, h, h],
];

const playerFieldAfterFirstClick = setFlag(
const [playerFieldAfterFirstClick] = setFlag(
[0, 0],
playerField,
gameField
Expand All @@ -50,7 +50,7 @@ describe('Set flag action', () => {
[h, h, h],
]);

const playerFieldAfterSecondClick = setFlag(
const [playerFieldAfterSecondClick] = setFlag(
[0, 0],
playerField,
gameField
Expand All @@ -62,7 +62,7 @@ describe('Set flag action', () => {
[h, h, h],
]);

const playerFieldAfterThirdClick = setFlag(
const [playerFieldAfterThirdClick] = setFlag(
[0, 0],
playerField,
gameField
Expand All @@ -75,4 +75,35 @@ describe('Set flag action', () => {
]);
});
});
describe('Detect win state', () => {
it('5*5 solved case', () => {
const [playerField, isSolved, flagCounter] = setFlag(
[1, 0],
[
[f, f, 1, 1, 2],
[h, 3, 1, 0, 0],
[1, 1, 0, 1, 1],
[1, 0, 0, 1, f],
[2, 1, 0, 1, 0],
],
[
[9, 9, 1, 1, 2],
[9, 3, 1, 0, 0],
[1, 1, 0, 1, 1],
[1, 0, 0, 1, 9],
[2, 1, 0, 1, 0],
]
);

expect(flagCounter).toBe(4);
expect(isSolved).toStrictEqual(true);
expect(playerField).toStrictEqual([
[f, f, 1, 1, 2],
[f, 3, 1, 0, 0],
[1, 1, 0, 1, 1],
[1, 0, 0, 1, f],
[2, 1, 0, 1, 0],
]);
});
});
});
9 changes: 6 additions & 3 deletions src/helpers/setFlag.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { CellState, Coords, Field } from './Field';
import { detectSolvedPuzzle } from './detectSolvedPullze';

/**
* Set flag to the cell
* @param {Coords} coords
* @param {Field} playerField
* @param {Field} gameField
* @returns {[Field, FlagCounter]}
* @returns {[Field, boolean, number]}
*/
export const setFlag = (
coords: Coords,
playerField: Field,
gameField: Field
): Field => {
): [Field, boolean, number] => {
const [y, x] = coords;
const cell = playerField[y][x];

Expand All @@ -29,5 +30,7 @@ export const setFlag = (
break;
}

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

return [playerField, isSolved, flagCounter];
};
Loading