Skip to content

Commit cdf1ac8

Browse files
tiagomarijnh
authored andcommitted
Rename act method parameter in Chapter 7
1 parent 549bb99 commit cdf1ac8

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

07_elife.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,9 @@ First we'll write a ((plant)), which is a rather simple life-form.
943943
function Plant() {
944944
this.energy = 3 + Math.random() * 4;
945945
}
946-
Plant.prototype.act = function(context) {
946+
Plant.prototype.act = function(view) {
947947
if (this.energy > 15) {
948-
var space = context.find(" ");
948+
var space = view.find(" ");
949949
if (space)
950950
return {type: "reproduce", direction: space};
951951
}
@@ -971,11 +971,11 @@ now define a plant eater.
971971
function PlantEater() {
972972
this.energy = 20;
973973
}
974-
PlantEater.prototype.act = function(context) {
975-
var space = context.find(" ");
974+
PlantEater.prototype.act = function(view) {
975+
var space = view.find(" ");
976976
if (this.energy > 60 && space)
977977
return {type: "reproduce", direction: space};
978-
var plant = context.find("*");
978+
var plant = view.find("*");
979979
if (plant)
980980
return {type: "eat", direction: plant};
981981
if (space)

code/solutions/07_1_artificial_stupidity.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ function SmartPlantEater() {
22
this.energy = 30;
33
this.direction = "e";
44
}
5-
SmartPlantEater.prototype.act = function(context) {
6-
var space = context.find(" ");
5+
SmartPlantEater.prototype.act = function(view) {
6+
var space = view.find(" ");
77
if (this.energy > 90 && space)
88
return {type: "reproduce", direction: space};
9-
var plants = context.findAll("*");
9+
var plants = view.findAll("*");
1010
if (plants.length > 1)
1111
return {type: "eat", direction: randomElement(plants)};
12-
if (context.look(this.direction) != " " && space)
12+
if (view.look(this.direction) != " " && space)
1313
this.direction = space;
1414
return {type: "move", direction: this.direction};
1515
};

code/solutions/07_2_predators.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ function SmartPlantEater() {
22
this.energy = 30;
33
this.direction = "e";
44
}
5-
SmartPlantEater.prototype.act = function(context) {
6-
var space = context.find(" ");
5+
SmartPlantEater.prototype.act = function(view) {
6+
var space = view.find(" ");
77
if (this.energy > 90 && space)
88
return {type: "reproduce", direction: space};
9-
var plants = context.findAll("*");
9+
var plants = view.findAll("*");
1010
if (plants.length > 1)
1111
return {type: "eat", direction: randomElement(plants)};
12-
if (context.look(this.direction) != " " && space)
12+
if (view.look(this.direction) != " " && space)
1313
this.direction = space;
1414
return {type: "move", direction: this.direction};
1515
};
@@ -20,12 +20,12 @@ function Tiger() {
2020
// Used to track the amount of prey seen per turn in the last six turns
2121
this.preySeen = [];
2222
}
23-
Tiger.prototype.act = function(context) {
23+
Tiger.prototype.act = function(view) {
2424
// Average number of prey seen per turn
2525
var seenPerTurn = this.preySeen.reduce(function(a, b) {
2626
return a + b;
2727
}, 0) / this.preySeen.length;
28-
var prey = context.findAll("O");
28+
var prey = view.findAll("O");
2929
this.preySeen.push(prey.length);
3030
// Drop the first element from the array when it is longer than 6
3131
if (this.preySeen.length > 6)
@@ -35,10 +35,10 @@ Tiger.prototype.act = function(context) {
3535
if (prey.length && seenPerTurn > 0.25)
3636
return {type: "eat", direction: randomElement(prey)};
3737

38-
var space = context.find(" ");
38+
var space = view.find(" ");
3939
if (this.energy > 400 && space)
4040
return {type: "reproduce", direction: space};
41-
if (context.look(this.direction) != " " && space)
41+
if (view.look(this.direction) != " " && space)
4242
this.direction = space;
4343
return {type: "move", direction: this.direction};
4444
};

0 commit comments

Comments
 (0)