An exercise in recursive objective propagation in a functional paradigm. Really just for some fun in a simulator-type virtual city.
/*crime tree
each crime can have several others (given a simple crime, we can do other crimes, as long as the victim is alive or the assailant is willing)
like: criminal X wants to rob someone. this may lead to other crimes like (defendant Y fights back) battery, 2nd degree murder, disposing of a corpse
*/
var crimes = ["rob", "beat", "kill", "insult", "dismembered", "poison", "threaten", "pee on"];
var maxCrimesPerString = 10;
function crime(w, target, cnt){
this.name = w;
this.next = null;
if(cnt < maxCrimesPerString && Math.round(Math.random() * 1) == 0){ // 50% chance, while under limit
var idx = Math.round(Math.random()*(target.dos.length-1));
var t = target.dos[idx];
if(t != "beat" && t != "insult") target.dos.splice(idx,1); // can't do some crimes more than once
if(t == "beat" && target.health > 0){ // if beat, decrease HP.
target.health = Math.max(0, target.health - 20);
if(target.health == 0){ // if beat to death, can't "kill" later
for(var zint = 0, len = target.dos.length;zint < len;zint++){
if(target.dos[zint] == "kill"){
target.dos.splice(zint,1);
break;
}
}
}
}else if(t == "kill") target.health = 0;
this.next = new crime(t, target, cnt+1);
}
this.toString = function(){
if(this.next != null) return this.name + ", " + this.next.toString();
else return this.name;
}
}
function victim(name){
this.name = name;
this.health = 100;
this.dos = []; // things that can be done to victim
for(var xint = 0;xint < crimes.length;xint++)
//this.status[crimes[xint]] = false;
this.dos.push(crimes[xint]);
}
x = new crime("rob", new victim("John Doe"), 1);
console.log(x.toString());
No comments:
Post a Comment