-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathorcs.js
39 lines (33 loc) · 791 Bytes
/
orcs.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
function randomName() {
var names = ["Zuul", "Gnarr", "Farzar"]
return names[Math.floor(Math.random()*names.length)]
}
function OrcRepository(orcs, swords) {
this.orcs = orcs
this.swords = swords
}
OrcRepository.prototype.getArmed = function () {
if (this.orcs > 0 && this.swords > 0) {
this.orcs -= 1
this.swords -= 1
return Orc.withSword();
}
return false
}
OrcRepository.prototype.add = function (orc) {
this.orcs += 1
if (orc.weapon == "sword") this.swords += 1
}
function Orc(name, weapon) {
this.name = name
this.weapon = weapon
}
Orc.withSword = function () {
return new Orc(randomName(), "sword")
}
repo = new OrcRepository(1, 1)
orc = repo.getArmed()
console.log(orc)
console.log(repo.getArmed())
repo.add(orc)
console.log(repo.getArmed())