Skip to content

Commit

Permalink
Merge pull request #35 from delasy/fix-dead-zombies
Browse files Browse the repository at this point in the history
Fix calculation of closest entity
  • Loading branch information
webdevcody authored Oct 16, 2024
2 parents b12b019 + a08c832 commit b95d0ff
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
4 changes: 0 additions & 4 deletions simulators/zombie-survival/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@ export interface Position {
x: number;
y: number;
}

export function positionAsNumber(position: Position): number {
return position.x + position.y;
}
69 changes: 69 additions & 0 deletions simulators/zombie-survival/ZombieSurvival.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,72 @@ test("player gets killed behind walls", () => {

expect(game.finished()).toBeTruthy();
});

test("player kills zombie and it doesn't hit afterwards", () => {
const game = new ZombieSurvival([
["P", " "],
[" ", "Z"],
]);

game.step();
game.step();

expect(game.getState()).toStrictEqual([
["P", " "],
[" ", " "],
]);

expect(game.finished()).toBeTruthy();
});

test.only("player kills closest zombie", () => {
const game = new ZombieSurvival([
[" ", " ", "R", " ", "Z"],
[" ", " ", " ", " ", "B"],
[" ", "P", " ", "R", " "],
["Z", " ", " ", " ", " "],
[" ", " ", "B", " ", " "],
]);

game.step();

expect(game.getState()).toStrictEqual([
[" ", " ", "R", "Z", " "],
[" ", " ", " ", " ", "B"],
[" ", "P", " ", "R", " "],
[" ", "Z", " ", " ", " "],
[" ", " ", "B", " ", " "],
]);

game.step();

expect(game.getState()).toStrictEqual([
[" ", " ", "R", " ", " "],
[" ", " ", " ", "Z", "B"],
[" ", "P", " ", "R", " "],
[" ", " ", " ", " ", " "],
[" ", " ", "B", " ", " "],
]);

game.step();

expect(game.getState()).toStrictEqual([
[" ", " ", "R", " ", " "],
[" ", " ", "Z", " ", "B"],
[" ", "P", " ", "R", " "],
[" ", " ", " ", " ", " "],
[" ", " ", "B", " ", " "],
]);

game.step();

expect(game.getState()).toStrictEqual([
[" ", " ", "R", " ", " "],
[" ", " ", " ", " ", "B"],
[" ", "P", " ", "R", " "],
[" ", " ", " ", " ", " "],
[" ", " ", "B", " ", " "],
]);

expect(game.finished()).toBeTruthy();
});
17 changes: 10 additions & 7 deletions simulators/zombie-survival/lib/closestEntity.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import { Entity } from "../entities/Entity";
import { positionAsNumber } from "../Position";

export interface ClosestEntityScore {
score: number;
distance: number;
target: Entity;
}

export function closestEntity(entity: Entity, targets: Entity[]): Entity {
const entityPosition = positionAsNumber(entity.getPosition());
const entityPosition = entity.getPosition();
const x1 = entityPosition.x;
const y1 = entityPosition.y;
const scores: ClosestEntityScore[] = [];

for (const target of targets) {
if (target.dead()) {
continue;
}

const targetPosition = positionAsNumber(target.getPosition());
const score = Math.abs(entityPosition - targetPosition);
const targetPosition = target.getPosition();
const x2 = targetPosition.x;
const y2 = targetPosition.y;
const distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

scores.push({ target, score });
scores.push({ distance, target });
}

if (scores.length === 0) {
throw new Error("No alive targets found");
}

scores.sort((a, b) => a.score - b.score);
scores.sort((a, b) => a.distance - b.distance);
return scores[0].target;
}

0 comments on commit b95d0ff

Please sign in to comment.