2023-05-28 13:44:27 +02:00
|
|
|
import {beforeEach, expect, test} from "vitest";
|
2023-05-23 22:01:41 +02:00
|
|
|
import possibleMovesAlgorithm from "../../src/game/possibleMovesAlgorithm";
|
|
|
|
import {testMap} from "../../src/game/map";
|
|
|
|
import {Character, PacMan} from "../../src/game/character";
|
2023-05-28 13:44:27 +02:00
|
|
|
import {Direction} from "../../src/game/direction";
|
2023-05-23 22:01:41 +02:00
|
|
|
|
|
|
|
let pacMan: Character;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-05-28 13:44:27 +02:00
|
|
|
pacMan = new PacMan("yellow", {end: {x: 3, y: 3}, direction: Direction.up});
|
2023-05-23 22:01:41 +02:00
|
|
|
});
|
|
|
|
|
2023-05-24 20:14:23 +02:00
|
|
|
test("Pac-Man rolls one from start, should return one position", () => {
|
2023-05-23 22:01:41 +02:00
|
|
|
const result = possibleMovesAlgorithm(testMap, pacMan, 1);
|
|
|
|
expect(result.length).toBe(1);
|
2023-05-28 18:22:44 +02:00
|
|
|
expect(result[0].path?.length).toBe(0);
|
|
|
|
expect(result).toEqual([{end: {x: 3, y: 2}, direction: Direction.up, path: []}]);
|
2023-05-23 22:01:41 +02:00
|
|
|
});
|
|
|
|
|
2023-05-24 20:14:23 +02:00
|
|
|
test("Pac-Man rolls two from start, should return one position", () => {
|
2023-05-23 22:01:41 +02:00
|
|
|
const result = possibleMovesAlgorithm(testMap, pacMan, 2);
|
|
|
|
expect(result.length).toBe(1);
|
2023-05-28 18:22:44 +02:00
|
|
|
expect(result[0].path?.length).toBe(1);
|
|
|
|
expect(result).toEqual([{end: {x: 3, y: 1}, direction: Direction.up, path: [{x: 3, y: 2}]}]);
|
2023-05-23 22:01:41 +02:00
|
|
|
});
|
|
|
|
|
2023-05-24 20:14:23 +02:00
|
|
|
test("Pac-Man rolls three from start, should return two positions", () => {
|
2023-05-23 22:01:41 +02:00
|
|
|
const result = possibleMovesAlgorithm(testMap, pacMan, 3);
|
|
|
|
expect(result.length).toBe(2);
|
2023-05-28 18:22:44 +02:00
|
|
|
arrayEquals(result, [{end: {x: 2, y: 1}, direction: Direction.left, path: [{x: 3, y: 2}, {x: 3, y: 1}]},
|
|
|
|
{end: {x: 4, y: 1}, direction: Direction.right, path: [{x: 3, y: 2}, {x: 3, y: 1}]}]);
|
2023-05-23 22:01:41 +02:00
|
|
|
});
|
|
|
|
|
2023-05-24 20:14:23 +02:00
|
|
|
test("Pac-Man rolls four from start, should return two positions", () => {
|
2023-05-23 22:01:41 +02:00
|
|
|
const result = possibleMovesAlgorithm(testMap, pacMan, 4);
|
|
|
|
expect(result.length).toBe(2);
|
2023-05-28 18:22:44 +02:00
|
|
|
arrayEquals(result, [{
|
|
|
|
end: {x: 1, y: 1},
|
|
|
|
direction: Direction.left,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 2, y: 1}]
|
|
|
|
}, {
|
|
|
|
end: {x: 5, y: 1},
|
|
|
|
direction: Direction.right,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 4, y: 1}]
|
|
|
|
}]);
|
2023-05-23 22:01:41 +02:00
|
|
|
});
|
|
|
|
|
2023-05-24 20:14:23 +02:00
|
|
|
test("Pac-Man rolls five from start, should return four positions", () => {
|
2023-05-23 22:01:41 +02:00
|
|
|
const result = possibleMovesAlgorithm(testMap, pacMan, 5);
|
|
|
|
expect(result.length).toBe(4);
|
2023-05-28 18:22:44 +02:00
|
|
|
arrayEquals(result, [{
|
|
|
|
end: {x: 5, y: 0},
|
|
|
|
direction: Direction.up,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 4, y: 1}, {x: 5, y: 1}]
|
|
|
|
}, {
|
|
|
|
end: {x: 6, y: 1},
|
|
|
|
direction: Direction.right,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 4, y: 1}, {x: 5, y: 1}]
|
|
|
|
}, {
|
|
|
|
end: {x: 1, y: 2},
|
|
|
|
direction: Direction.down,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 2, y: 1}, {x: 1, y: 1}]
|
|
|
|
}, {
|
|
|
|
end: {x: 5, y: 2},
|
|
|
|
direction: Direction.down,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 4, y: 1}, {x: 5, y: 1}]
|
|
|
|
}
|
|
|
|
]);
|
2023-05-23 22:01:41 +02:00
|
|
|
});
|
|
|
|
|
2023-05-24 20:14:23 +02:00
|
|
|
test("Pac-Man rolls six from start, should return six positions", () => {
|
2023-05-23 22:01:41 +02:00
|
|
|
const result = possibleMovesAlgorithm(testMap, pacMan, 6);
|
|
|
|
expect(result.length).toBe(6);
|
2023-05-28 18:22:44 +02:00
|
|
|
arrayEquals(result, [
|
|
|
|
{
|
|
|
|
end: {x: 1, y: 3},
|
|
|
|
direction: Direction.down,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 2, y: 1}, {x: 1, y: 1}, {x: 1, y: 2}]
|
|
|
|
}, {
|
|
|
|
end: {x: 0, y: 5},
|
|
|
|
direction: Direction.right,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 4, y: 1}, {x: 5, y: 1}, {x: 5, y: 0}]
|
|
|
|
}, {
|
|
|
|
end: {x: 5, y: 3},
|
|
|
|
direction: Direction.down,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 4, y: 1}, {x: 5, y: 1}, {x: 5, y: 2}]
|
|
|
|
}, {
|
|
|
|
end: {x: 7, y: 1},
|
|
|
|
direction: Direction.right,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 4, y: 1}, {x: 5, y: 1}, {x: 6, y: 1}]
|
|
|
|
}, {
|
|
|
|
end: {x: 10, y: 5},
|
|
|
|
direction: Direction.left,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 4, y: 1}, {x: 5, y: 1}, {x: 5, y: 0}]
|
|
|
|
}, {
|
|
|
|
end: {x: 5, y: 10},
|
|
|
|
direction: Direction.up,
|
|
|
|
path: [{x: 3, y: 2}, {x: 3, y: 1}, {x: 4, y: 1}, {x: 5, y: 1}, {x: 5, y: 0}]
|
|
|
|
}
|
|
|
|
]);
|
2023-05-23 22:01:41 +02:00
|
|
|
});
|
|
|
|
|
2023-05-28 14:39:53 +02:00
|
|
|
test("Pac-Man rolls four from position [5,1] (right), should return 11", () => {
|
|
|
|
pacMan.follow({end: {x: 5, y: 1}, direction: Direction.right});
|
|
|
|
const result = possibleMovesAlgorithm(testMap, pacMan, 4);
|
|
|
|
expect(result.length).toBe(11); // TODO a character can't move to a different character's spawn
|
|
|
|
});
|
|
|
|
|
2023-05-28 13:44:27 +02:00
|
|
|
test("Pac-Man rolls three from position [1,5] (left), should return 5", () => {
|
|
|
|
pacMan.follow({end: {x: 1, y: 5}, direction: Direction.left});
|
|
|
|
const result = possibleMovesAlgorithm(testMap, pacMan, 3);
|
2023-05-28 18:22:44 +02:00
|
|
|
arrayEquals(result, [
|
|
|
|
{end: {x: 1, y: 2}, direction: Direction.up, path: [{x: 1, y: 4}, {x: 1, y: 3}]},
|
|
|
|
{end: {x: 1, y: 8}, direction: Direction.down, path: [{x: 1, y: 6}, {x: 1, y: 7}]},
|
|
|
|
{end: {x: 5, y: 1}, direction: Direction.down, path: [{x: 0, y: 5}, {x: 5, y: 0}]},
|
|
|
|
{end: {x: 9, y: 5}, direction: Direction.left, path: [{x: 0, y: 5}, {x: 10, y: 5}]},
|
|
|
|
{end: {x: 5, y: 9}, direction: Direction.up, path: [{x: 0, y: 5}, {x: 5, y: 10}]},
|
|
|
|
]);
|
2023-05-28 13:44:27 +02:00
|
|
|
expect(result.length).toBe(5);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("Pac-Man rolls six from position [1,5] (down), should return 17", () => {
|
|
|
|
pacMan.follow({end: {x: 1, y: 5}, direction: Direction.down});
|
2023-05-23 22:01:41 +02:00
|
|
|
const result = possibleMovesAlgorithm(testMap, pacMan, 6);
|
2023-05-28 13:44:27 +02:00
|
|
|
expect(result.length).toBe(17);
|
2023-05-23 22:01:41 +02:00
|
|
|
});
|
|
|
|
|
2023-05-28 19:39:43 +02:00
|
|
|
test("Pac-Man rolls six from position [7,1] (right), path to [9,5] should be five tiles long", () => {
|
|
|
|
pacMan.follow({end: {x: 7, y: 1}, direction: Direction.right});
|
|
|
|
const result = possibleMovesAlgorithm(testMap, pacMan, 6);
|
|
|
|
expect(result[0].path?.length).toBe(5);
|
|
|
|
});
|
|
|
|
|
2023-05-23 22:01:41 +02:00
|
|
|
function arrayEquals<T extends any[]>(result: T, expected: T, message?: string): void {
|
|
|
|
for (const item of expected) {
|
|
|
|
expect(result, message).toContainEqual(item);
|
|
|
|
}
|
|
|
|
}
|