-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBattlefield.php
59 lines (47 loc) · 1.85 KB
/
Battlefield.php
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
<?php
class Battlefield {
private $humans;
private $zombies;
private $enraged;
public function __construct(Humans $humans, Zombies $zombies) {
$this->humans = $humans;
$this->zombies = $zombies;
$this->enraged = false;
echo 'This war is a battle for survival between Humans and Zombies.<br>';
$this->startBattle();
}
public function startBattle() {
while ($this->humans->alive() > 0 && $this->zombies->alive() > 0) {
$human = $this->getRandomHuman();
$zombie = $this->getRandomZombie();
if ($human instanceof Human && !$this->enraged) {
$buff = 0;
mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() < 0.2 ? $human->shots($zombie, $buff) : $zombie->bites($human);
} else if ($human instanceof Human && $this->enraged){
$buff = 10;
mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax() < 0.4 ? $human->shots($zombie, $buff) : $zombie->bites($human);
}
if ($human instanceof Child) {
$zombie->eats($human);
$this->enraged = true;
echo '<strong>The humans are now Enraged</strong><br>';
}
if ($zombie->getHealth() <= 0) {
$this->zombies->isDead();
}
if ($human->getHealth() <= 0) {
$this->humans->isDead();
}
}
}
public function getRandomHuman() {
array_values($this->humans->getMembers());
$index = array_rand($this->humans->getMembers(), 1);
return $this->humans->getMember($index);
}
public function getRandomZombie() {
array_values($this->zombies->getMembers());
$index = array_rand($this->zombies->getMembers(), 1);
return $this->zombies->getMember($index);
}
}