Skip to content

Instantly share code, notes, and snippets.

@Shiggiddie
Created October 22, 2014 17:48
Show Gist options
  • Save Shiggiddie/1026c53d2240e78b51b1 to your computer and use it in GitHub Desktop.
Save Shiggiddie/1026c53d2240e78b51b1 to your computer and use it in GitHub Desktop.
// Artificial Stupidity
function SmartPlantEater() {
this.energy = 20;
this.last_reproduction = 0;
}
SmartPlantEater.prototype.act = function(view) {
var space = view.find(" ");
if (this.energy > 60 && space && this.last_reproduction > 60) {
this.last_reproduction = 0;
return {type: "reproduce", direction: space};
}
var plant = view.find("*");
if (plant && view.findAll("*").length >= 2) {
this.last_reproduction += 1;
return {type: "eat", direction: plant};
}
if (space) {
this.last_reproduction += 1;
return {type: "move", direction: space};
}
};
animateWorld(new LifelikeWorld(
["############################",
"##### ######",
"## *** **##",
"# *##** ** O *##",
"# *** O ##** *#",
"# O ##*** #",
"# ##** #",
"# O #* #",
"#* #** O #",
"#*** ##** O **#",
"##**** ###*** *###",
"############################"],
{"#": Wall,
"O": SmartPlantEater,
"*": Plant}
));
// Preditors
function Tiger() {
this.energy = 100;
this.last_reproduction = 0;
}
Tiger.prototype.act = function(view) {
var space = view.findAll(" ").length > 2 ? view.find(" ") : view.find("*");
if (this.energy > 60 && space && this.last_reproduction > 60) {
this.last_reproduction = 0;
return {type: "reproduce", direction: space};
}
var plantEater = view.find("O");
if (plantEater) {
this.last_reproduction += 1;
return {type: "eat", direction: plantEater};
}
if (space) {
this.last_reproduction += 1;
return {type: "move", direction: space};
}
};
animateWorld(new LifelikeWorld(
["####################################################",
"# #### **** ###",
"# * @ ## ######## OO ##",
"# * ## O O **** *#",
"# ##* ########## *#",
"# ##*** * **** **#",
"#* ** # * *** ######### **#",
"#* ** # * # * **#",
"# ## # O # *** ######",
"#* @ # # * O # #",
"#* # ###### ** #",
"### **** *** ** #",
"# O @ O #",
"# * ## ## ## ## ### * #",
"# ** # * ##### O #",
"## ** O O # # *** *** ### ** #",
"### # ***** ****#",
"####################################################"],
{"#": Wall,
"@": Tiger,
"O": SmartPlantEater, // from previous exercise
"*": Plant}
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment