Skip to content

Commit

Permalink
feat(engine): updated getNextBoardState function
Browse files Browse the repository at this point in the history
  • Loading branch information
wialy committed Mar 6, 2024
1 parent 0e4e49b commit f3f99c6
Show file tree
Hide file tree
Showing 9 changed files with 689 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { expect, test } from 'vitest';

import { getArrowSymbol } from '../../../../ui/utils/get-arrow-symbol';
import { DIRECTIONS, VECTOR_ZERO } from '../../../constants';
import { createEntity } from '../../create-entity';
import { getMultipliedVector } from '../../get-multiplied-vector';
import { getNextBoardState } from '..';

test.each(
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]),
)(
'should move one of 4 %s to empty field and stop others',
(_symbol, direction) => {
const firstPosition = direction;
const firstVelocity = getMultipliedVector({
multiplier: -1,
vector: firstPosition,
});
const secondPosition = { x: direction.y, y: direction.x };
const secondVelocity = getMultipliedVector({
multiplier: -1,
vector: secondPosition,
});
const thirdPosition = getMultipliedVector({
multiplier: -1,
vector: direction,
});
const thirdVelocity = getMultipliedVector({
multiplier: -1,
vector: thirdPosition,
});
const fourthPosition = { x: -direction.y, y: -direction.x };
const fourthVelocity = getMultipliedVector({
multiplier: -1,
vector: fourthPosition,
});

const entities = [
createEntity('floor'),
createEntity('floor', { position: firstPosition }),
createEntity('floor', { position: secondPosition }),
createEntity('floor', { position: thirdPosition }),
createEntity('floor', { position: fourthPosition }),
createEntity('movable', {
id: 'movable-1',
position: firstPosition,
velocity: firstVelocity,
}),
createEntity('movable', {
id: 'movable-2',
position: secondPosition,
velocity: secondVelocity,
}),
createEntity('movable', {
id: 'movable-3',
position: thirdPosition,
velocity: thirdVelocity,
}),
createEntity('movable', {
id: 'movable-4',
position: fourthPosition,
velocity: fourthVelocity,
}),
];

const result = getNextBoardState({ board: { entities } }).board.entities;

expect(result).toBeDefined();
expect(result).toHaveLength(9);
expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
position: VECTOR_ZERO,
type: 'movable',
velocity: expect.not.objectContaining(VECTOR_ZERO),
}),
expect.objectContaining({
type: 'movable',
velocity: VECTOR_ZERO,
}),
expect.objectContaining({
type: 'movable',
velocity: VECTOR_ZERO,
}),
expect.objectContaining({
type: 'movable',
velocity: VECTOR_ZERO,
}),
]),
);
},
);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'vitest';

import { createEntity } from '../create-entity/create-entity';
import { getNextBoardState } from '.';
import { createEntity } from '../../create-entity/create-entity';
import { getNextBoardState } from '..';

describe('getNextBoardState', () => {
test('should return the same board', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect, test } from 'vitest';

import { getArrowSymbol } from '../../../../ui/utils/get-arrow-symbol';
import { DIRECTIONS } from '../../../constants';
import { createEntity } from '../../create-entity';
import { getNextBoardState } from '..';

test.each(
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]),
)(
"should move one %s to empty field and preserve it's velocity",
(_symbol, direction) => {
const entities = [
createEntity('floor'),
createEntity('floor', { position: direction }),
createEntity('movable', { id: 'movable', velocity: direction }),
];

const result = getNextBoardState({ board: { entities } }).board.entities;

expect(result).toBeDefined();
expect(result).toHaveLength(3);
expect(result).toContainEqual(
expect.objectContaining({
position: direction,
type: 'movable',
velocity: direction,
}),
);
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect, test } from 'vitest';

import { getArrowSymbol } from '../../../../ui/utils/get-arrow-symbol';
import { DIRECTIONS, VECTOR_ZERO } from '../../../constants';
import { createEntity } from '../../create-entity';
import { getNextBoardState } from '..';

test.each(
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]),
)('should stop one when there is no floor %s', (_symbol, direction) => {
const entities = [
createEntity('floor'),
createEntity('movable', { id: 'movable', velocity: direction }),
];

const result = getNextBoardState({ board: { entities } }).board.entities;

expect(result).toBeDefined();
expect(result).toHaveLength(2);
expect(result).toContainEqual(
expect.objectContaining({
id: 'movable',
position: VECTOR_ZERO,
velocity: VECTOR_ZERO,
}),
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { expect, test } from 'vitest';

import { getArrowSymbol } from '../../../../ui/utils/get-arrow-symbol';
import { DIRECTIONS, VECTOR_ZERO } from '../../../constants';
import { createEntity } from '../../create-entity';
import { getMultipliedVector } from '../../get-multiplied-vector';
import { getNextBoardState } from '..';

test.each(
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]),
)(
"should move one of three %s to empty field, preserve it's velocity and stop others",
(_symbol, direction) => {
const firstPosition = direction;
const firstVelocity = getMultipliedVector({
multiplier: -1,
vector: direction,
});
const secondPosition = { x: direction.y, y: direction.x };
const secondVelocity = getMultipliedVector({
multiplier: -1,
vector: secondPosition,
});
const thirdPosition = getMultipliedVector({
multiplier: -1,
vector: direction,
});
const thirdVelocity = direction;

const entities = [
createEntity('floor'),
createEntity('floor', { position: firstPosition }),
createEntity('floor', { position: secondPosition }),
createEntity('floor', { position: thirdPosition }),
createEntity('movable', {
id: 'movable-1',
position: firstPosition,
velocity: firstVelocity,
}),
createEntity('movable', {
id: 'movable-2',
position: secondPosition,
velocity: secondVelocity,
}),
createEntity('movable', {
id: 'movable-3',
position: thirdPosition,
velocity: thirdVelocity,
}),
];

const result = getNextBoardState({ board: { entities } }).board.entities;

expect(result).toBeDefined();
expect(result).toHaveLength(7);
expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
position: VECTOR_ZERO,
type: 'movable',
velocity: expect.not.objectContaining(VECTOR_ZERO),
}),
expect.objectContaining({
type: 'movable',
velocity: VECTOR_ZERO,
}),
expect.objectContaining({
type: 'movable',
velocity: VECTOR_ZERO,
}),
]),
);
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { expect, test } from 'vitest';

import { getArrowSymbol } from '../../../../ui/utils/get-arrow-symbol';
import { DIRECTIONS, VECTOR_ZERO } from '../../../constants';
import { createEntity } from '../../create-entity';
import { getMultipliedVector } from '../../get-multiplied-vector';
import { getNextBoardState } from '..';

test.each(
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]),
)(
'should move both dices %s to empty field, stop them, and remove another (line)',
(_symbol, direction) => {
const entities = [
createEntity('floor'),
createEntity('floor', { position: direction }),
createEntity('floor', {
position: getMultipliedVector({ multiplier: -1, vector: direction }),
}),
createEntity('dice', {
id: 'dice-1',
position: getMultipliedVector({
multiplier: -1,
vector: direction,
}),
value: 1,
velocity: direction,
}),
createEntity('dice', {
id: 'dice-2',
position: direction,
value: 1,
velocity: getMultipliedVector({ multiplier: -1, vector: direction }),
}),
];

const result = getNextBoardState({ board: { entities } }).board.entities;

expect(result).toBeDefined();
expect(result).toHaveLength(5);
expect(result).toContainEqual(
expect.objectContaining({
isFresh: true,
position: VECTOR_ZERO,
type: 'dice',
value: 2,
velocity: VECTOR_ZERO,
}),
);
expect(result).toContainEqual(
expect.objectContaining({
isRemoved: true,
position: VECTOR_ZERO,
type: 'dice',
value: 1,
velocity: VECTOR_ZERO,
}),
);
},
);

test.each(
DIRECTIONS.map((direction) => [getArrowSymbol(direction), direction]),
)(
'should move both dice %s to empty field, preserve velocity of removed one (corner)',
(_symbol, direction) => {
const firstPosition = direction;
const firstVelocity = getMultipliedVector({
multiplier: -1,
vector: firstPosition,
});
const secondPosition = { x: direction.y, y: direction.x };
const secondVelocity = getMultipliedVector({
multiplier: -1,
vector: secondPosition,
});
const entities = [
createEntity('floor'),
createEntity('floor', { position: firstPosition }),
createEntity('floor', { position: secondPosition }),
createEntity('dice', {
id: 'movable-1',
position: firstPosition,
value: 1,
velocity: firstVelocity,
}),
createEntity('dice', {
id: 'movable-2',
position: secondPosition,
value: 1,
velocity: secondVelocity,
}),
];

const result = getNextBoardState({
board: { entities },
}).board.entities;

expect(result).toBeDefined();
expect(result).toHaveLength(5);
expect(result).toContainEqual(
expect.objectContaining({
isRemoved: true,
position: VECTOR_ZERO,
type: 'dice',
velocity: VECTOR_ZERO,
}),
);
expect(result).toContainEqual(
expect.objectContaining({
isFresh: true,
position: VECTOR_ZERO,
type: 'dice',
value: 2,
velocity: VECTOR_ZERO,
}),
);
},
);
Loading

0 comments on commit f3f99c6

Please sign in to comment.