forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_1_artificial_stupidity.js
More file actions
33 lines (32 loc) · 1012 Bytes
/
07_1_artificial_stupidity.js
File metadata and controls
33 lines (32 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function SmartPlantEater() {
this.energy = 30;
this.direction = "e";
}
SmartPlantEater.prototype.act = function(context) {
var space = context.find(" ");
if (this.energy > 90 && space)
return {type: "reproduce", direction: space};
var plants = context.findAll("*");
if (plants.length > 1)
return {type: "eat", direction: randomElement(plants)};
if (context.look(this.direction) != " " && space)
this.direction = space;
return {type: "move", direction: this.direction};
};
animateWorld(new LifelikeWorld(
["############################",
"##### ######",
"## *** **##",
"# *##** ** O *##",
"# *** O ##** *#",
"# O ##*** #",
"# ##** #",
"# O #* #",
"#* #** O #",
"#*** ##** O **#",
"##**** ###*** *###",
"############################"],
{"#": Wall,
"O": SmartPlantEater,
"*": Plant}
));