Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*/node_modules/
*.csv
157 changes: 157 additions & 0 deletions nodejs-by-kd8lvt/BULBASAUR NO HUMANS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const thisCsv = createCsvWriter({
path:'./bulbasaur no humans.csv',
header: [
{id:'tick',title:'tick'},
{id:'females',title:'females'},
{id:'males',title:'males'}
]
});


function randInt(min,max) {
return Math.round(Math.random()*(max - min + 1)) + min;
}

class Population {
constructor(sleep) {
this.mPop = 14;
this.fPop = 2;
this.eggs = this.populateEggVars();
this.totalPop = this.mPop+this.fPop;
this.sleep = sleep;
this.ticks = 0;
this.dataArr = [];
//this.start();
}

start() {
this.interval = setInterval(this.tick,this.sleep)
}

populateEggVars() {
let _tmp = [];
for (let i=0;i<20;i++) {
_tmp.push(0);
}
return _tmp;
}

tickDeaths() {
for (var i=0;i<this.fPop;i++) {
let death = randInt(1,10000);
if (death <= 243) {
this.fPop -= 1;
}
}
for (var i=0;i<this.mPop;i++) {
let death = randInt(1,10000);
if (death <= 243) {
this.mPop -= 1;
}
}
}

newEgg() {
if (this.chooseSex()) {
this.mPop += 1;
} else {
this.fPop += 1;
}
}

chooseSex() {
let s = randInt(1,8);
if (s <= 7) return true;
return false;
}

tickNewEggs() {
if (this.mPop >= 1) {
for (var i = 0; i < this.fPop; i++) {
let egg = randInt(1,100)
if (egg <= 50) this.eggs[this.eggs.length-1] += 1; //Male exists
}
} else if (this.fPop >= 1) {
for (var i = 0; i < this.fPop; i++) {
let egg = randInt(1,100);
if (egg <= 10) this.eggs[this.eggs.length-1] += 1; //Egg group / Ditto
}
}
}

tickHatches() {
if (this.eggs[0] > 0) {
console.log(`${this.eggs[0]} Bulbasaur eggs are hatching!`);
for (var i = 0; i < this.eggs[0]; i++) {
this.newEgg();
}
}
}

tickEggs() {
for (let i=0;i<this.eggs.length;i++) {
if (i == this.eggs.length - 1) {
this.eggs[i] = this.eggs[i] - this.eggs[i];
} else {
this.eggs[i] = this.eggs[i] - this.eggs[i] + this.eggs[i+1];
}
}
}

tick() {
console.clear();
this.ticks += 1;
console.log("Tick: "+this.ticks);
//for (let j=0;j < 20000;j=this.totalPop) {
let eggExists = false;
for (let j=0;j<this.eggs.length;j++) {
if (this.eggs[j] > 0) {
eggExists = true;
}
}
if (this.fPop <= 0 && eggExists == false) {
this.extinct = true;
}
this.tickNewEggs();
this.tickHatches();
this.tickDeaths();
this.tickEggs();
this.totalPop = this.mPop + this.fPop;

let waitingEggs = 0;
for (let i=0;i<this.eggs.length;i++) {
waitingEggs += this.eggs[i];
}
console.log('Total Population: '+this.totalPop);
console.log('Females: '+this.fPop);
console.log('Males: '+this.mPop);
console.log('Eggs Waiting To Hatch: '+waitingEggs);
this.dataArr.push({tick:this.ticks,females:this.fPop,males:this.mPop});
//ticks = j;
//}
}
}

let interval = null;
async function start() {
let pop = await new Population();
let ticks = 0;
interval = setInterval(()=>{
pop.tick();
if (pop.totalPop >= 20000) {
console.log('Stopped in '+ticks+' ticks due to "max population reached"');
//console.log(asciichart.plot(pop.chart,{height:100}));
thisCsv.writeRecords(pop.dataArr);
clearInterval(interval);
} else if (pop.extinct) {
console.log('Stopped in '+ticks+' ticks due to "extinct"');
//console.log(asciichart.plot(pop.chart,{height:100}));
thisCsv.writeRecords(pop.dataArr);
clearInterval(interval);
}
ticks++
},50);
}

start();
157 changes: 157 additions & 0 deletions nodejs-by-kd8lvt/BULBASAUR.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const thisCsv = createCsvWriter({
path:'./bulbasaur.csv',
header: [
{id:'tick',title:'tick'},
{id:'females',title:'females'},
{id:'males',title:'males'}
]
});


function randInt(min,max) {
return Math.round(Math.random()*(max - min + 1)) + min;
}

class Population {
constructor(sleep) {
this.mPop = 14;
this.fPop = 2;
this.eggs = this.populateEggVars();
this.totalPop = this.mPop+this.fPop;
this.sleep = sleep;
this.ticks = 0;
this.dataArr = [];
//this.start();
}

start() {
this.interval = setInterval(this.tick,this.sleep)
}

populateEggVars() {
let _tmp = [];
for (let i=0;i<20;i++) {
_tmp.push(0);
}
return _tmp;
}

tickDeaths() {
for (var i=0;i<this.fPop;i++) {
let death = randInt(1,10000);
if (death <= 243) {
this.fPop -= 1;
}
}
for (var i=0;i<this.mPop;i++) {
let death = randInt(1,10000);
if (death <= 243) {
this.mPop -= 1;
}
}
}

newEgg() {
if (this.chooseSex()) {
this.mPop += 1;
} else {
this.fPop += 1;
}
}

chooseSex() {
let s = randInt(1,8);
if (s <= 7) return true;
return false;
}

tickNewEggs() {
if (this.mPop >= 1 && this.ticks <= 100) {
for (var i = 0; i < this.fPop; i++) {
let egg = randInt(1,100)
if (egg <= 50) this.eggs[this.eggs.length-1] += 1; //Male exists
}
} else if (this.fPop >= 1) {
for (var i = 0; i < this.fPop; i++) {
let egg = randInt(1,100);
if (egg <= 10) this.eggs[this.eggs.length-1] += 1; //Egg group / Ditto
}
}
}

tickHatches() {
if (this.eggs[0] > 0) {
console.log(`${this.eggs[0]} Bulbasaur eggs are hatching!`);
for (var i = 0; i < this.eggs[0]; i++) {
this.newEgg();
}
}
}

tickEggs() {
for (let i=0;i<this.eggs.length;i++) {
if (i == this.eggs.length - 1) {
this.eggs[i] = this.eggs[i] - this.eggs[i];
} else {
this.eggs[i] = this.eggs[i] - this.eggs[i] + this.eggs[i+1];
}
}
}

tick() {
console.clear();
this.ticks += 1;
console.log("Tick: "+this.ticks);
//for (let j=0;j < 20000;j=this.totalPop) {
let eggExists = false;
for (let j=0;j<this.eggs.length;j++) {
if (this.eggs[j] > 0) {
eggExists = true;
}
}
if (this.fPop <= 0 && eggExists == false) {
this.extinct = true;
}
this.tickNewEggs();
this.tickHatches();
this.tickDeaths();
this.tickEggs();
this.totalPop = this.mPop + this.fPop;

let waitingEggs = 0;
for (let i=0;i<this.eggs.length;i++) {
waitingEggs += this.eggs[i];
}
console.log('Total Population: '+this.totalPop);
console.log('Females: '+this.fPop);
console.log('Males: '+this.mPop);
console.log('Eggs Waiting To Hatch: '+waitingEggs);
this.dataArr.push({tick:this.ticks,females:this.fPop,males:this.mPop});
//ticks = j;
//}
}
}

let interval = null;
async function start() {
let pop = await new Population();
let ticks = 0;
interval = setInterval(()=>{
pop.tick();
if (pop.totalPop >= 20000) {
console.log('Stopped in '+ticks+' ticks due to "max population reached"');
//console.log(asciichart.plot(pop.chart,{height:100}));
thisCsv.writeRecords(pop.dataArr);
clearInterval(interval);
} else if (pop.extinct) {
console.log('Stopped in '+ticks+' ticks due to "extinct"');
//console.log(asciichart.plot(pop.chart,{height:100}));
thisCsv.writeRecords(pop.dataArr);
clearInterval(interval);
}
ticks++
},50);
}

start();
12 changes: 12 additions & 0 deletions nodejs-by-kd8lvt/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- How To Run --

-- INSTALLATION: --

1. Install NodeJS - make sure you install npm with it!
2. Open your shell of choice ('CMD' on Windows, 'bash' on most Linux distros, etc etc)
3. Navigate to this folder (if you cloned this, that should be easy enough to do)
4. Run 'npm install' - this downloads the CSV package.

-- RUNNING: --

Simply run 'node "BULBASAUR.js"' or 'node "BULBASAUR NO HUMANS.js"' in your shell of choice. It's a near 1-1 of Austin's python code, but cleaned up... and then made messy again... Don't sue me.
13 changes: 13 additions & 0 deletions nodejs-by-kd8lvt/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions nodejs-by-kd8lvt/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "pokemon-population-models",
"version": "1.0.0",
"description": "",
"main": "BULBASAUR NO HUMANS.js",
"dependencies": {
"csv-writer": "^1.5.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kd8lvt/Pokemon-Population-Models.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/kd8lvt/Pokemon-Population-Models/issues"
},
"homepage": "https://github.com/kd8lvt/Pokemon-Population-Models#readme"
}
Loading