Skip to content

Commit

Permalink
feat: stop on max species population growth
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Hale committed Dec 16, 2023
1 parent 655e9fd commit dac50dd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ export const PREY_MATURITY_AGE_FACTOR = 0.5; // of the max age

export const PREDATOR_MATURITY_AGE_FACTOR = 0.5; // of the max age

export const MAX_CHILDREN = 3;
export const MAX_CHILDREN = 3;

export const MAX_SPECIES_POPULATION = 300;
6 changes: 3 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "./style.css";

import { CANVAS_HEIGHT, CANVAS_WIDTH, CTX_ERROR, PREDATOR_MAX_AGE, PREY_MAX_AGE } from "./constants";
import { CANVAS_HEIGHT, CANVAS_WIDTH, CTX_ERROR, MAX_SPECIES_POPULATION, PREDATOR_MAX_AGE, PREY_MAX_AGE } from "./constants";
import { createLoop, createStats, drawGrid, randomLocation } from "./helpers";
import Predator from "./models/predator";
import Prey from "./models/prey";
Expand Down Expand Up @@ -104,8 +104,8 @@ function update(dt: number) {
prey.splice(0, 0, ...$prey);
predators.splice(0, 0, ...$predators);

// terminate when either prey or predators go extinct
if (!prey.length || !predators.length) {
// terminate when either prey or predators go extinct or incase of large population
if (!prey.length || !predators.length || prey.length > MAX_SPECIES_POPULATION) {
stopBtn.click();
}
}
Expand Down

0 comments on commit dac50dd

Please sign in to comment.