So i'm having a problem here. The code will go into an infinite loop or seems to break and the population seems to stop growing at a low number.
But this only happens sometimes.
At the end of every 12 months it's supposed to show the year and population and it's working, but only some of the time.
I suppose the inconsistency of the population numbers could just be coincidence, but it doesn't seem like it to me and the infiniteloop or whatever is happening that makes it take a longer time to process than i'm willing to wait points to there being a problem, but I can't see it.
//y = year
//py = previous year
// Age is based on months, not years
py = 0;
var Women = new Array();
var Men = new Array();
var Woman = {ID: 0, Age: 240, SinceLastBirth: 0, Pregnant: "no", MonthsPregnant: 0, Dead: "no"};
var Man = {ID: 0, Age: 240, Soldier: "no", Dead: "no"};
Women.push(Woman);
Men.push(Man);
for(i = 0; i < 18001; i++) {
for(x = 0; x < Women.length; x++) {
Women[x].Age++;
}
for(x = 0; x < Women.length; x++) {
if(Women[x].Age <= 168 && Women[x].Dead == "no") {
DeathCheck = Math.random();
if(DeathCheck <= 0.00166) {
Women[x].Dead = "yes";
}
}
}
for(x = 0; x < Women.length; x++) {
if(Women[x].Pregnant == "yes" && Women[x].Dead == "no") {
MiscarryCheck = Math.random()
if(MiscarryCheck <= 0.036){
Women[x].Pregnant = "no"
Women[x].MonthsPregnant = 0;
} else {
Women[x].MonthsPregnant++;
}
}
if(Women[x].MonthsPregnant >= 9 && Women[x].Dead == "no") {
Sex = Math.floor((Math.random() *2) +1);
if(Sex == 2) {
Man = {ID: 0, Age: 0, Soldier: "no", Dead: "no"};
Men.push(Man);
} else {
Woman = {ID: 0, Age: 0, SinceLastBirth: 0, Pregnant: "no", MonthsPregnant: 0, Dead: "no"};
Women.push(Woman);
}
BirthDeathCheck = Math.random();
if(BirthDeathCheck < 0.01) {
Women[x].Dead = "yes";
} else {
Women[x].MonthsPregnant = 0;
Women[x].SinceLastBirth = 48;
}
}
}
for(x = 0; x < Women.length; x++) {
if(Women[x].Age >= 168 && Women[x].Age <= 1860 && Women[x].SinceLastBirth <= 0 && Women[x].Dead == "no") {
PregnantCheck = Math.random();
if(PregnantCheck <= 0.05 && Women[x].Pregnant == "no") {
Women[x].Pregnant = "yes";
}
}
if(Women.SinceLastBirth > 0 && Women[x].Dead == "no") {
Women.SinceLastBirth--;
}
}
m = 0;
y = 0;
for(x = 0; x <= i; x++) {
if(m == 12) {
y++;
m = 0;
}
m++
}
if(y > py) {
Population = 0;
for(x = 0; x < Men.length; x++) {
if(Men[x].Dead == "no") {
Population++
}
}
for(x = 0; x < Women.length; x++) {
if(Women[x].Dead == "no") {
Population++
}
}
document.write(y + " : " + Population + "<br />");
py = y;
}
}