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

Game module by TDD (Ducks) #54

Merged
merged 1 commit into from
Oct 13, 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 @@ -298,3 +298,7 @@
### Redux basic example

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

### Game module by TDD (Ducks)

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/54/files)
70 changes: 70 additions & 0 deletions src/modules/GameWithRedux/game.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { GameSettings } from '@/modules/GameSettings';
import { CellState, Field } from '@/core/Field';

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

import { reducer, openCell, State } from './game';

describe('Game reducer', () => {
const level = 'beginner';
const baseInitialState: State = {
level,
time: 0,
isGameOver: false,
isGameStarted: false,
isWin: false,
settings: GameSettings[level],
bombs: 0,
flagCounter: 0,
gameField: [
[9, 1],
[1, 1],
],
playerField: [
[h, h],
[h, h],
],
};

describe('Action openCell simple case', () => {
it('Check openCell to cell with a number', () => {
expect(reducer(baseInitialState, openCell([1, 1]))).toEqual({
...baseInitialState,
isGameStarted: true,
playerField: [
[h, h],
[h, 1],
],
});
});
it('Check openCell to cell with a bomb', () => {
expect(reducer(baseInitialState, openCell([0, 0]))).toEqual({
...baseInitialState,
isGameStarted: false,
isWin: false,
isGameOver: true,
playerField: baseInitialState.gameField,
});
});
it('Check openCell to cell with a flag', () => {
const playerFieldWithFlag = [
[h, h],
[h, f],
] as Field;
expect(
reducer(
{
...baseInitialState,
isGameStarted: true,
playerField: playerFieldWithFlag,
},
openCell([1, 1])
)
).toEqual({
...baseInitialState,
isGameStarted: true,
playerField: playerFieldWithFlag,
});
});
});
});
94 changes: 94 additions & 0 deletions src/modules/GameWithRedux/game.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { AnyAction, Reducer } from 'redux';

import {
Field,
CellState,
generateFieldWithDefaultState,
fieldGenerator,
Coords,
} from '@/core/Field';
import { LevelNames, GameSettings } from '@/modules/GameSettings';
import { openCell as openCellHandler } from '@/core/openCell';

export interface State {
level: LevelNames;
time: number;
bombs: number;
isGameOver: boolean;
isGameStarted: boolean;
isWin: boolean;
settings: [number, number];
playerField: Field;
gameField: Field;
flagCounter: number;
}

export const getInitialState = (): State => {
const level = 'beginner';
const settings = GameSettings[level];
const [size, bombs] = settings;

return {
level,
time: 0,
bombs,
isGameOver: false,
isGameStarted: false,
isWin: false,
settings,
flagCounter: 0,
playerField: generateFieldWithDefaultState(size, CellState.hidden),
gameField: fieldGenerator(size, bombs / (size * size)),
};
};

// Actions
const OPEN_CELL = 'modules/GameWithRedux/OPEN_CELL';

// Action Creators
export const openCell = (coords: Coords): AnyAction => ({
type: OPEN_CELL,
payload: { coords },
});

// Reducer
export const reducer: Reducer<State> = (
state = getInitialState(),
action: AnyAction
) => {
const { playerField, gameField } = state;

switch (action.type) {
case OPEN_CELL: {
const {
payload: { coords },
} = action;

try {
const [newPlayerField, isSolved] = openCellHandler(
coords,
playerField,
gameField
);

return {
...state,
isGameStarted: true,
isGameOver: isSolved,
isWin: isSolved,
playerField: newPlayerField,
};
} catch (error) {
return {
...state,
isGameStarted: false,
isGameOver: true,
isWin: false,
playerField: gameField,
};
}
}
default:
return state;
}
};