From c15e70595971c72266875ef74dc6864638d183dc Mon Sep 17 00:00:00 2001 From: Jan Kaiser Date: Mon, 20 Mar 2023 12:17:55 +0100 Subject: [PATCH] feat: do not render apple on edge (#6) --- .gitignore | 2 +- src/app/game/game.service.ts | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index e09b283..559703e 100644 --- a/.gitignore +++ b/.gitignore @@ -42,4 +42,4 @@ testem.log Thumbs.db # Firebase -./firebase \ No newline at end of file +.firebase/ \ No newline at end of file diff --git a/src/app/game/game.service.ts b/src/app/game/game.service.ts index 7e1e99c..fe11234 100644 --- a/src/app/game/game.service.ts +++ b/src/app/game/game.service.ts @@ -160,15 +160,28 @@ export class GameService { }); } - private randomCoordinates(): Coordinates { + private randomCoordinates(avoidEdges?: boolean): Coordinates { + let xMin = 0; + let yMin = 0; + let xMax = this.gridSize?.width - 1 ?? 9; + let yMax = this.gridSize.height - 1 ?? 9; + + if (avoidEdges) { + xMin++; + yMin++; + xMax--; + yMax--; + } + const x = Math.floor(Math.random() * (xMax - xMin + 1)) + xMin; + const y = Math.floor(Math.random() * (yMax - yMin + 1)) + yMin; return { - x: Math.floor(Math.random() * (this.gridSize?.width ?? 0)), - y: Math.floor(Math.random() * (this.gridSize?.height ?? 0)), + x, + y, }; } private randomFoodCoordinates(): Coordinates { - const randomCoordinates = this.randomCoordinates(); + const randomCoordinates = this.randomCoordinates(true); return this.snake.segments.some((segment) => GameUtils.arePointsEqual(randomCoordinates, segment), )