From b2255e212fbb2d500b32417f898a22cf6320f56e Mon Sep 17 00:00:00 2001 From: Aaron Delasy Date: Wed, 16 Oct 2024 13:53:49 +0300 Subject: [PATCH 1/3] Fix dead zombies killing player --- simulators/zombie-survival/ZombieSurvival.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/simulators/zombie-survival/ZombieSurvival.ts b/simulators/zombie-survival/ZombieSurvival.ts index 5c4a2a1..e76edba 100644 --- a/simulators/zombie-survival/ZombieSurvival.ts +++ b/simulators/zombie-survival/ZombieSurvival.ts @@ -134,6 +134,10 @@ export class ZombieSurvival { break; } + if (zombie.dead()) { + continue; + } + zombie.walk(); } } From a00b6366b0e1f21d9a0d1dc93a408a80af8a56bc Mon Sep 17 00:00:00 2001 From: Aaron Delasy Date: Wed, 16 Oct 2024 14:11:08 +0300 Subject: [PATCH 2/3] Rollback and cover with tests --- .../zombie-survival/ZombieSurvival.spec.ts | 17 +++++++++++++++++ simulators/zombie-survival/ZombieSurvival.ts | 4 ---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/simulators/zombie-survival/ZombieSurvival.spec.ts b/simulators/zombie-survival/ZombieSurvival.spec.ts index 44e0ed8..509809f 100644 --- a/simulators/zombie-survival/ZombieSurvival.spec.ts +++ b/simulators/zombie-survival/ZombieSurvival.spec.ts @@ -360,3 +360,20 @@ test("player gets killed behind walls", () => { expect(game.finished()).toBeTruthy(); }); + +test("zombie dies after player killing it", () => { + const game = new ZombieSurvival([ + ["P", " "], + [" ", "Z"], + ]); + + game.step(); + game.step(); + + expect(game.getState()).toStrictEqual([ + ["P", " "], + [" ", " "], + ]); + + expect(game.finished()).toBeTruthy(); +}); diff --git a/simulators/zombie-survival/ZombieSurvival.ts b/simulators/zombie-survival/ZombieSurvival.ts index e76edba..5c4a2a1 100644 --- a/simulators/zombie-survival/ZombieSurvival.ts +++ b/simulators/zombie-survival/ZombieSurvival.ts @@ -134,10 +134,6 @@ export class ZombieSurvival { break; } - if (zombie.dead()) { - continue; - } - zombie.walk(); } } From a08c832115aed964491c4c02ecd667c658b0eda5 Mon Sep 17 00:00:00 2001 From: Aaron Delasy Date: Wed, 16 Oct 2024 14:47:17 +0300 Subject: [PATCH 3/3] Implement Pythagorean theorem for closest entity calculation --- simulators/zombie-survival/Position.ts | 4 -- .../zombie-survival/ZombieSurvival.spec.ts | 54 ++++++++++++++++++- .../zombie-survival/lib/closestEntity.ts | 17 +++--- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/simulators/zombie-survival/Position.ts b/simulators/zombie-survival/Position.ts index 61a08f9..09614ba 100644 --- a/simulators/zombie-survival/Position.ts +++ b/simulators/zombie-survival/Position.ts @@ -2,7 +2,3 @@ export interface Position { x: number; y: number; } - -export function positionAsNumber(position: Position): number { - return position.x + position.y; -} diff --git a/simulators/zombie-survival/ZombieSurvival.spec.ts b/simulators/zombie-survival/ZombieSurvival.spec.ts index 509809f..4851456 100644 --- a/simulators/zombie-survival/ZombieSurvival.spec.ts +++ b/simulators/zombie-survival/ZombieSurvival.spec.ts @@ -361,7 +361,7 @@ test("player gets killed behind walls", () => { expect(game.finished()).toBeTruthy(); }); -test("zombie dies after player killing it", () => { +test("player kills zombie and it doesn't hit afterwards", () => { const game = new ZombieSurvival([ ["P", " "], [" ", "Z"], @@ -377,3 +377,55 @@ test("zombie dies after player killing it", () => { 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(); +}); diff --git a/simulators/zombie-survival/lib/closestEntity.ts b/simulators/zombie-survival/lib/closestEntity.ts index 2facd5e..82d2267 100644 --- a/simulators/zombie-survival/lib/closestEntity.ts +++ b/simulators/zombie-survival/lib/closestEntity.ts @@ -1,13 +1,14 @@ 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) { @@ -15,16 +16,18 @@ export function closestEntity(entity: Entity, targets: Entity[]): Entity { 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; }