Skip to content

Commit 82cbbdb

Browse files
committed
added attack routines and narration
1 parent e32360d commit 82cbbdb

File tree

5 files changed

+157
-5
lines changed

5 files changed

+157
-5
lines changed

classes/Game.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22

33
class Game
44
{
5-
private $turns = 5; // temporary for initial commit
5+
private $turns = 50; // temporary for initial commit
66

7-
public function __construct($ui)
7+
public $is_running = true;
8+
private $first_attack = true;
9+
private $orderus_attacks;
10+
11+
public function __construct($ui, $narration)
812
{
913
$this->ui = $ui;
14+
$this->narration = $narration;
1015
$this->winner = new stdClass();
1116
$this->winner->is_orderus = false;
1217
$this->winner->is_beast = false;
1318
$this->orderus = new Orderus();
1419
$this->beast = new Beast();
1520
}
1621

17-
public $is_running = true;
18-
1922
public function display_stats()
2023
{
2124
$this->display_individual_stats($this->orderus);
@@ -36,8 +39,47 @@ private function display_individual_stats($player)
3639

3740
public function play_round()
3841
{
42+
if ($this->first_attack) {
43+
if ($this->orderus->speed() > $this->beast->speed()) {
44+
$this->ui->display($this->narration->orderus_first_attack_speed());
45+
$this->orderus_attacks = true;
46+
} else if ($this->orderus->speed() < $this->beast->speed()) {
47+
$this->ui->display($this->narration->beast_first_attack_speed());
48+
$this->orderus_attacks = false;
49+
} else if ($this->orderus->luck() > $this->beast->luck()) {
50+
$this->ui->display($this->narration->orderus_first_attack_luck());
51+
$this->orderus_attacks = true;
52+
} else {
53+
$this->ui->display($this->narration->beast_first_attack_luck());
54+
$this->orderus_attacks = false;
55+
}
56+
$this->first_attack = false;
57+
$this->ui->display_blank();
58+
}
59+
60+
if ($this->orderus_attacks) {
61+
$this->ui->display($this->narration->orderus_attacks());
62+
$this->orderus->attack($this->beast);
63+
} else {
64+
$this->ui->display($this->narration->beast_attacks());
65+
$this->beast->attack($this->orderus);
66+
}
67+
$this->orderus_attacks = !$this->orderus_attacks;
68+
3969
// temporary for initial commit
4070
$this->turns--;
4171
$this->is_running = ($this->turns > 0);
72+
73+
if (($this->orderus->health() <= 0) || ($this->beast->health() <= 0)) {
74+
$this->is_running = false;
75+
}
76+
77+
if ($this->orderus->health() <= 0) {
78+
$this->winner->is_beast = true;
79+
}
80+
81+
if ($this->beast->health() <= 0) {
82+
$this->winner->is_orderus = true;
83+
}
4284
}
4385
}

classes/Narration.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,36 @@ public function round()
2727
return $this->pick_narration($this->round_narrations);
2828
}
2929

30+
public function orderus_first_attack_speed()
31+
{
32+
return $this->pick_narration($this->orderus_first_attack_speed_narrations);
33+
}
34+
35+
public function orderus_first_attack_luck()
36+
{
37+
return $this->pick_narration($this->orderus_first_attack_luck_narrations);
38+
}
39+
40+
public function beast_first_attack_speed()
41+
{
42+
return $this->pick_narration($this->beast_first_attack_speed_narrations);
43+
}
44+
45+
public function beast_first_attack_luck()
46+
{
47+
return $this->pick_narration($this->beast_first_attack_luck_narrations);
48+
}
49+
50+
public function orderus_attacks()
51+
{
52+
return $this->pick_narration($this->orderus_attacks_narrations);
53+
}
54+
55+
public function beast_attacks()
56+
{
57+
return $this->pick_narration($this->beast_attacks_narrations);
58+
}
59+
3060
public function won()
3161
{
3262
return $this->pick_narration($this->won_narrations);
@@ -68,10 +98,47 @@ public function draw()
6898
"Another skirmish ensues.",
6999
"They go at it again.",
70100
"Once again, they fight!",
101+
"With equal determination, hero and foe glare upon each other.",
102+
"The two circle each other, slowly stepping side to side, as they watch for an opening.",
71103
"The battle continues.",
72104
"They clash...",
73105
);
74106

107+
private $orderus_first_attack_speed_narrations = array(
108+
"Orderus is faster. He attacks first!"
109+
);
110+
111+
private $beast_first_attack_speed_narrations = array(
112+
"Beast is faster. It attacks first!"
113+
);
114+
115+
private $orderus_first_attack_luck_narrations = array(
116+
"Orderus is luckier. He attacks first!"
117+
);
118+
119+
private $beast_first_attack_luck_narrations = array(
120+
"Beast is luckier. It attacks first!"
121+
);
122+
123+
private $orderus_attacks_narrations = array(
124+
"Orderus attacks...",
125+
"One, two! One, two! And through and through...",
126+
"Orderus' vorpal blade goes snicker-snack!",
127+
"Orderus takes a stance and foins with his sword!",
128+
"The mighty blade of Orderus swoops at the beast.",
129+
"With calm determination, Orderus lunges forward...",
130+
"With a spectacular mid-air flip, Orderus swooshes his blade upon the beast."
131+
);
132+
133+
private $beast_attacks_narrations = array(
134+
"The wild beast attacks...",
135+
"The wild beast snarls and leaps forward with gleaming claws.",
136+
"The wild beast bounds to and fro, before lunging forward with bared teeth.",
137+
"Growling menacingly, the wild beast stands on its hind feet and raises its paws...",
138+
"Preparing to prance, the wild beast rears down for the big win.",
139+
"The wild beast leaps into the air with a wide arc, landing with its sharp claws."
140+
);
141+
75142
private $won_narrations = array(
76143
"The game hath ended, but it is not the end, for Orderus hath once again emerged as victor.",
77144
"Huzzah! Orderus has vanquished the foe yet again!",

classes/Player.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,38 @@ public function save_stats()
6969
$this->speed_last = $this->speed;
7070
$this->luck_last = $this->luck;
7171
}
72+
73+
public function health()
74+
{
75+
return $this->health;
76+
}
77+
78+
public function strength()
79+
{
80+
return $this->health;
81+
}
82+
83+
public function defense()
84+
{
85+
return $this->defense;
86+
}
87+
88+
public function speed()
89+
{
90+
return $this->speed;
91+
}
92+
93+
public function luck()
94+
{
95+
return $this->luck;
96+
}
97+
98+
public function attack($defender)
99+
{
100+
$damage = $this->strength - $defender->defense;
101+
$defender->health -= $damage;
102+
if ($defender->health < 0) {
103+
$defender->health = 0; // it just makes more sense
104+
}
105+
}
72106
}

classes/UI.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ public function display($content)
1212
echo "$content\n";
1313
}
1414
}
15+
16+
public function display_blank()
17+
{
18+
$this->display("");
19+
}
1520
}

index.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99

1010
$ui = new UI();
1111
$narration = new Narration();
12-
$game = new Game($ui);
12+
$game = new Game($ui, $narration);
1313

1414
$ui->display($narration->start());
1515
$game->display_stats();
16+
$ui->display_blank();
1617

1718
while ($game->is_running) {
1819
$ui->display($narration->round());
20+
$ui->display_blank();
21+
1922
$game->play_round();
2023
$game->display_stats();
24+
$ui->display_blank();
2125
}
2226

2327
if ($game->winner->is_orderus) {

0 commit comments

Comments
 (0)