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 44e0ed8..4851456 100644 --- a/simulators/zombie-survival/ZombieSurvival.spec.ts +++ b/simulators/zombie-survival/ZombieSurvival.spec.ts @@ -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(); +}); 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; }