-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimals-wolves.js
127 lines (114 loc) · 2.5 KB
/
animals-wolves.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var animalsWolves = [LazyWolf, EmoWolf, SheepWolf];
// Animal: LazyWolf
function LazyWolf() {
Animal.call(this);
this.last = 0;
this.attacks = ['r', 'p', 's', 'x'];
};
LazyWolf.prototype = new Animal();
LazyWolf.prototype.constructor = LazyWolf;
LazyWolf.prototype.name = function() {
return 'w';
}
LazyWolf.prototype.fight = function(opponent) {
switch (opponent) {
case 'b':
case 'l':
return 's';
case 's':
return 'r'; // faker!
default:
return this.attacks[last++%3];
}
}
LazyWolf.prototype.move = function(surroundings) {
if (surroundings[0][1] == 'l')
return 'l';
if (surroundings[1][0] == 'l')
return 'u';
return 'h';
}
// Animal: EmoWolf
function EmoWolf() {
Animal.call(this);
};
EmoWolf.prototype = new Animal();
EmoWolf.prototype.constructor = EmoWolf;
EmoWolf.prototype.name = function() {
return 'w';
}
EmoWolf.prototype.fight = function(opponent) {
return 'x';
}
EmoWolf.prototype.move = function(surroundings) {
return 'h';
}
// Animal SheepWolf
function SheepWolf() {
Animal.call(this);
this.animalWeights =
{
'w': -3,
's': -1,
' ': 0,
'h': 1,
'l': -2,
'b': -1
}
};
SheepWolf.prototype = new Animal();
SheepWolf.prototype.constructor = SheepWolf;
SheepWolf.prototype.name = function() {
return 'w';
}
SheepWolf.prototype.fight = function(opponent) {
switch(opponent){
case 'b':
return 's';
case 'l':
return 's';
case 's':
return 'p';
default:
return 'p';
}
}
SheepWolf.prototype.move = function(surroundings) {
var xWeight = 0;
var yWeight = 0;
// Northwest
xWeight += this.animalWeights[surroundings[0][0]];
yWeight += this.animalWeights[surroundings[0][0]];
// North
yWeight += this.animalWeights[surroundings[0][1]];
// Northeast
xWeight += this.animalWeights[surroundings[0][2]];
yWeight += this.animalWeights[surroundings[0][2]];
// West
xWeight += this.animalWeights[surroundings[1][0]];
// East
xWeight += this.animalWeights[surroundings[1][2]];
// Southwest
xWeight += this.animalWeights[surroundings[2][0]];
yWeight += this.animalWeights[surroundings[2][0]];
// South
yWeight += this.animalWeights[surroundings[2][1]];
// South
xWeight += this.animalWeights[surroundings[2][2]];
yWeight += this.animalWeights[surroundings[2][2]];
if (Math.abs(xWeight) < Math.abs(yWeight)) {
if (yWeight > 0) {
return 'u';
} else {
return 'd';
}
} else if (Math.abs(yWeight) < Math.abs(xWeight)) {
if (xWeight > 0) {
return 'r';
} else {
return 'l';
}
}
// Sit still if no-one's around
return 'h';
}