-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimals-base.js
96 lines (91 loc) · 1.84 KB
/
animals-base.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
// This file contains the Animal interface and base animal implementations
var animalsBase = [Stone, Lion, Bear];
var animalsBaseCount =
{
's': 0,
'l': 0,
'b': 0
}
// Animal interface
function Animal() {}
Animal.prototype.name = function() {
// Return the letter representing this animal
return "z";
};
Animal.prototype.fight = function(opponent) {
// r = rock, p = paper, s = scissors, x = suicide
return "x";
};
Animal.prototype.move = function(surroundings) {
// u = up, r = right, d = down, l = left, h = hold
return "h";
}
// Animal: Stone
function Stone(){
Animal.call(this);
};
Stone.prototype = new Animal();
Stone.prototype.constructor = Stone;
Stone.prototype.name = function() {
return "s";
}
Stone.prototype.fight = function() {
return "r";
}
// Animal: Lion
function Lion(){
Animal.call(this);
this.lastMove = "r";
};
Lion.prototype = new Animal();
Lion.prototype.constructor = Lion;
Lion.prototype.name = function() {
return "l";
}
Lion.prototype.fight = function() {
return Math.floor(Math.random() * 2) == 1 ? "p" : "s";
}
Lion.prototype.move = function() {
if(this.lastMove == "d") {
this.lastMove = "r";
return this.lastMove;
} else {
this.lastMove = "d";
return this.lastMove;
}
}
// Animal: Bear
function Bear() {
Animal.call(this);
this.lastMove = "l";
this.lastMoveCount = 4;
}
Bear.prototype = new Animal();
Bear.prototype.construction = Bear;
Bear.prototype.name = function() {
return "b";
}
Bear.prototype.fight = function() {
return "p";
}
Bear.prototype.move = function() {
if(this.lastMoveCount >= 4) {
this.lastMoveCount = 0;
switch (this.lastMove) {
case "d" :
this.lastMove = "r";
break;
case "r" :
this.lastMove = "u";
break;
case "u" :
this.lastMove = "l";
break;
case "l" :
this.lastMove = "d";
break;
}
}
this.lastMoveCount++;
return this.lastMove;
}