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

Add openCell action #24

Merged
merged 1 commit into from
Jul 22, 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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,11 @@
[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/21)

[Pull request2](https://github.com/nickovchinnikov/minesweeper/pull/22)

[Pull request3](https://github.com/nickovchinnikov/minesweeper/pull/23)

## Section 8: React hooks advanced

### Game logic

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/24)
165 changes: 138 additions & 27 deletions src/helpers/CellsManipulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
incrementNeibours,
getNeigboursItems,
checkItemInField,
openCell,
} from './CellsManipulator';

const { empty, bomb } = CellState;
const { empty: e, hidden: h, bomb: b } = CellState;

describe('Check neigbours selectors', () => {
it('With [0, 0] coords', () => {
Expand Down Expand Up @@ -36,7 +37,7 @@ describe('Check neigbours selectors', () => {

describe('checkItemInField tests', () => {
describe('Simple cases', () => {
const field: Field = [[empty]];
const field: Field = [[e]];

it('Out of y range', () => {
expect(checkItemInField([1, 0], field)).toBe(false);
Expand All @@ -52,11 +53,11 @@ describe('checkItemInField tests', () => {
});
describe('Big field', () => {
const field: Field = [
[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],
[e, e, e, e, e],
[e, e, e, e, e],
[e, e, e, e, e],
[e, e, e, e, e],
[e, e, e, e, e],
];

it('Out of x range', () => {
Expand All @@ -80,19 +81,19 @@ describe('checkItemInField tests', () => {
describe('Check Increment Neibours', () => {
describe('Simple cases', () => {
it('Field with only one item', () => {
expect(incrementNeibours([0, 0], [[bomb]])).toStrictEqual([[bomb]]);
expect(incrementNeibours([0, 0], [[b]])).toStrictEqual([[b]]);
});
it('Field 2x2 with one mine', () => {
expect(
incrementNeibours(
[0, 0],
[
[bomb, empty],
[empty, empty],
[b, e],
[e, e],
]
)
).toStrictEqual([
[bomb, 1],
[b, 1],
[1, 1],
]);
});
Expand All @@ -101,13 +102,13 @@ describe('Check Increment Neibours', () => {
incrementNeibours(
[0, 0],
[
[bomb, empty],
[empty, bomb],
[b, e],
[e, b],
]
)
).toStrictEqual([
[bomb, 1],
[1, bomb],
[b, 1],
[1, b],
]);
});
});
Expand All @@ -117,14 +118,14 @@ describe('Check Increment Neibours', () => {
incrementNeibours(
[1, 1],
[
[empty, empty, empty],
[empty, bomb, empty],
[empty, empty, empty],
[e, e, e],
[e, b, e],
[e, e, e],
]
)
).toStrictEqual([
[1, 1, 1],
[1, bomb, 1],
[1, b, 1],
[1, 1, 1],
]);
});
Expand All @@ -133,14 +134,14 @@ describe('Check Increment Neibours', () => {
incrementNeibours(
[1, 1],
[
[0, 1, bomb],
[0, bomb, 1],
[0, 1, b],
[0, b, 1],
[0, 0, 0],
]
)
).toStrictEqual([
[1, 2, bomb],
[1, bomb, 2],
[1, 2, b],
[1, b, 2],
[1, 1, 1],
]);
});
Expand All @@ -149,14 +150,14 @@ describe('Check Increment Neibours', () => {
incrementNeibours(
[1, 1],
[
[0, 1, bomb],
[8, bomb, 1],
[0, 1, b],
[8, b, 1],
[8, 8, 8],
]
)
).toStrictEqual([
[1, 2, bomb],
[8, bomb, 2],
[1, 2, b],
[8, b, 2],
[8, 8, 8],
]);
});
Expand Down Expand Up @@ -220,3 +221,113 @@ describe('Check Increment Neibours', () => {
});
});
});

describe('Open cell action', () => {
describe('Simple cases with loose', () => {
it('Open cell with the bomb', () => {
expect(() =>
openCell(
[1, 1],
[
[h, h],
[h, h],
],
[
[1, 1],
[1, b],
]
)
).toThrow('Game Over');
});
});
describe('Open cell with number', () => {
it('Open cell with state == 1', () => {
const playerField = openCell(
[1, 1],
[
[h, h, h],
[h, h, 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],
]);
});
it('Open cell with state == 3', () => {
const playerField = openCell(
[1, 1],
[
[h, h, h],
[h, h, h],
[h, h, h],
],
[
[9, 2, 0],
[9, 3, 0],
[9, 2, 0],
]
);
expect(playerField).toStrictEqual([
[h, h, h],
[h, 3, h],
[h, h, h],
]);
});
});
describe('Open empty cell', () => {
it('Open empty cell, simple 3*3 case', () => {
const playerField = openCell(
[1, 2],
[
[h, h, h],
[h, h, h],
[h, h, h],
],
[
[1, 1, 0],
[9, 1, 0],
[1, 1, 0],
]
);
expect(playerField).toStrictEqual([
[h, 1, 0],
[h, 1, 0],
[h, 1, 0],
]);
});
it('Open empty cell 5*5 case', () => {
const playerField = openCell(
[2, 2],
[
[h, h, h, h, h],
[h, h, h, h, h],
[h, h, h, h, h],
[h, h, h, h, h],
[h, h, h, h, h],
],
[
[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(playerField).toStrictEqual([
[h, h, 1, 1, 2],
[h, 3, 1, 0, 0],
[1, 1, 0, 1, 1],
[1, 0, 0, 1, h],
[2, 1, 0, 1, h],
]);
});
});
});
54 changes: 50 additions & 4 deletions src/helpers/CellsManipulator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cell, Coords, Field } from './Field';
import { Cell, CellState, Coords, Field } from './Field';

/**
* Get neighbour cells indexes
Expand Down Expand Up @@ -37,9 +37,8 @@ export const checkItemInField = ([y, x]: Coords, { length }: Field): boolean =>
export const incrementNeibours = (coords: Coords, field: Field): Field => {
const items = getNeigboursItems(coords);

for (const item of Object.values(items)) {
if (checkItemInField(item, field)) {
const [y, x] = item;
for (const [y, x] of Object.values(items)) {
if (checkItemInField([y, x], field)) {
const cell = field[y][x];
if (cell < 8) {
field[y][x] = (cell + 1) as Cell;
Expand All @@ -49,3 +48,50 @@ export const incrementNeibours = (coords: Coords, field: Field): Field => {

return field;
};

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

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

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

if (gameCell === empty) {
playerField[y][x] = gameCell;

const items = getNeigboursItems(coords);

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

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

if (gameCell < bomb) {
playerField[y][x] = gameCell;
}
}
}
}

playerField[y][x] = gameCell;

return playerField;
};