Skip to content

Commit

Permalink
feat: do not render apple on edge (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
honzikec committed Mar 20, 2023
1 parent 64728a2 commit c15e705
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ testem.log
Thumbs.db

# Firebase
./firebase
.firebase/
21 changes: 17 additions & 4 deletions src/app/game/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down

0 comments on commit c15e705

Please sign in to comment.