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

Refactoring openCell action #32

Merged
merged 4 commits into from
Aug 19, 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,7 @@
### Test refactoring

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

### Set flag action

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/32/files)
111 changes: 0 additions & 111 deletions src/helpers/CellsManipulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
incrementNeibours,
getNeigboursItems,
checkItemInField,
openCell,
} from './CellsManipulator';

const { empty: e, hidden: h, bomb: b } = CellState;
Expand Down Expand Up @@ -221,113 +220,3 @@ 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],
]);
});
});
});
45 changes: 1 addition & 44 deletions src/helpers/CellsManipulator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cell, CellState, Coords, Field } from './Field';
import { Cell, Coords, Field } from './Field';

/**
* Get neighbour cells indexes
Expand Down Expand Up @@ -48,46 +48,3 @@ 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 playerCell = playerField[y][x];
const gameCell = gameField[y][x];

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

playerField[y][x] = gameCell;

return playerField;
};
114 changes: 114 additions & 0 deletions src/helpers/openCell.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { CellState } from './Field';
import { openCell } from './openCell';

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

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],
]);
});
});
});
45 changes: 45 additions & 0 deletions src/helpers/openCell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { CellState, Coords, Field } from './Field';
import { checkItemInField, getNeigboursItems } from './CellsManipulator';

/**
* 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 playerCell = playerField[y][x];
const gameCell = gameField[y][x];

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

playerField[y][x] = gameCell;

return playerField;
};
Loading