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 with createSlice #55

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 @@ -302,3 +302,7 @@
### Game module by TDD (Ducks)

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

### Game module by TDD with createSlice

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/55/files)
122 changes: 116 additions & 6 deletions src/modules/GameWithRedux/game.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CellState, Field } from '@/core/Field';

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

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

describe('Game reducer', () => {
const level = 'beginner';
Expand All @@ -14,7 +14,7 @@ describe('Game reducer', () => {
isGameStarted: false,
isWin: false,
settings: GameSettings[level],
bombs: 0,
bombs: 1,
flagCounter: 0,
gameField: [
[9, 1],
Expand All @@ -26,9 +26,9 @@ describe('Game reducer', () => {
],
};

describe('Action openCell simple case', () => {
describe('Check action openCell', () => {
it('Check openCell to cell with a number', () => {
expect(reducer(baseInitialState, openCell([1, 1]))).toEqual({
expect(reducer(baseInitialState, actions.openCell([1, 1]))).toEqual({
...baseInitialState,
isGameStarted: true,
playerField: [
Expand All @@ -38,7 +38,7 @@ describe('Game reducer', () => {
});
});
it('Check openCell to cell with a bomb', () => {
expect(reducer(baseInitialState, openCell([0, 0]))).toEqual({
expect(reducer(baseInitialState, actions.openCell([0, 0]))).toEqual({
...baseInitialState,
isGameStarted: false,
isWin: false,
Expand All @@ -58,7 +58,7 @@ describe('Game reducer', () => {
isGameStarted: true,
playerField: playerFieldWithFlag,
},
openCell([1, 1])
actions.openCell([1, 1])
)
).toEqual({
...baseInitialState,
Expand All @@ -67,4 +67,114 @@ describe('Game reducer', () => {
});
});
});

describe('Check action setFlag', () => {
it('Check setFlag', () => {
const flagState = reducer(baseInitialState, actions.setFlag([1, 1]));

expect(flagState).toEqual({
...baseInitialState,
isGameStarted: true,
flagCounter: 1,
playerField: [
[h, h],
[h, f],
],
});

const weakFlagState = reducer(flagState, actions.setFlag([1, 1]));

expect(weakFlagState).toEqual({
...baseInitialState,
isGameStarted: true,
flagCounter: 1,
playerField: [
[h, h],
[h, w],
],
});

expect(reducer(weakFlagState, actions.setFlag([1, 1]))).toEqual({
...baseInitialState,
isGameStarted: true,
});
});
});

describe('Win flow', () => {
it('Setup flag on the last stage', () => {
const state1 = reducer(baseInitialState, actions.openCell([1, 0]));
const state2 = reducer(state1, actions.openCell([0, 1]));
const state3 = reducer(state2, actions.openCell([1, 1]));
const state4 = reducer(state3, actions.setFlag([0, 0]));

expect(state4).toEqual({
...baseInitialState,
isGameStarted: false,
isWin: true,
isGameOver: true,
flagCounter: 1,
playerField: [
[f, 1],
[1, 1],
],
});
});
it('Open cell on the last stage', () => {
const state1 = reducer(baseInitialState, actions.setFlag([0, 0]));
const state2 = reducer(state1, actions.openCell([1, 0]));
const state3 = reducer(state2, actions.openCell([0, 1]));
const state4 = reducer(state3, actions.openCell([1, 1]));

expect(state4).toEqual({
...baseInitialState,
isGameStarted: false,
isWin: true,
isGameOver: true,
flagCounter: 1,
playerField: [
[f, 1],
[1, 1],
],
});
});
});

describe('Check reset and changeLevel', () => {
it('Reset game action should reset game to the default state', () => {
const nextState = reducer(baseInitialState, actions.reset());
expect(nextState).toEqual(
expect.objectContaining({
level,
time: 0,
bombs: 10,
isGameOver: false,
isGameStarted: false,
isWin: false,
settings: [9, 10],
flagCounter: 0,
})
);
expect(nextState.gameField).toHaveLength(9);
expect(nextState.playerField).toHaveLength(9);
});
it('changeLevel should setup new game level', () => {
const level = 'intermediate';
const nextState = reducer(baseInitialState, actions.changeLevel(level));
expect(nextState).toEqual(
expect.objectContaining({
level,
time: 0,
bombs: 44,
isGameOver: false,
isGameStarted: false,
isWin: false,
settings: [16, 44],
flagCounter: 0,
})
);
expect(nextState.gameField).toHaveLength(16);
expect(nextState.playerField).toHaveLength(16);
});
});
});
86 changes: 40 additions & 46 deletions src/modules/GameWithRedux/game.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnyAction, Reducer } from 'redux';
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

import {
Field,
Expand All @@ -9,6 +9,7 @@ import {
} from '@/core/Field';
import { LevelNames, GameSettings } from '@/modules/GameSettings';
import { openCell as openCellHandler } from '@/core/openCell';
import { setFlag } from '@/core/setFlag';

export interface State {
level: LevelNames;
Expand All @@ -23,8 +24,7 @@ export interface State {
flagCounter: number;
}

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

Expand All @@ -42,53 +42,47 @@ export const getInitialState = (): State => {
};
};

// 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;

export const { reducer, actions } = createSlice({
name: 'game',
initialState: getInitialState(),
reducers: {
openCell: (state, { payload }: PayloadAction<Coords>) => {
const { playerField, gameField } = state;
try {
const [newPlayerField, isSolved] = openCellHandler(
coords,
payload,
playerField,
gameField
);

return {
...state,
isGameStarted: true,
isGameOver: isSolved,
isWin: isSolved,
playerField: newPlayerField,
};
state.isGameStarted = !isSolved;
state.isGameOver = isSolved;
state.isWin = isSolved;
state.playerField = newPlayerField;
} catch (error) {
return {
...state,
isGameStarted: false,
isGameOver: true,
isWin: false,
playerField: gameField,
};
state.isGameStarted = false;
state.isGameOver = true;
state.isWin = false;
state.playerField = gameField;
}
}
default:
return state;
}
};
},
setFlag: (state, { payload }: PayloadAction<Coords>) => {
const { playerField, gameField, flagCounter, bombs } = state;

const [newPlayerField, isSolved, newFlagCounter] = setFlag(
payload,
playerField,
gameField,
flagCounter,
bombs
);
state.isGameStarted = !isSolved;
state.isGameOver = isSolved;
state.isWin = isSolved;
state.flagCounter = newFlagCounter;
state.playerField = newPlayerField;
},
reset: ({ level }) => getInitialState(level),
changeLevel: (state, { payload }: PayloadAction<LevelNames>) =>
getInitialState(payload),
},
});