Skip to content

Commit e32360d

Browse files
committed
added generic Player abstraction
1 parent 58b7919 commit e32360d

File tree

5 files changed

+113
-54
lines changed

5 files changed

+113
-54
lines changed

classes/Beast.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
11
<?php
22

3-
class Beast
3+
class Beast extends Player
44
{
55
public $name = "Beast";
66

7-
private $beast_health_min = 60;
8-
private $beast_health_max = 90;
7+
private $health_min = 60;
8+
private $health_max = 90;
99

10-
private $beast_strength_min = 60;
11-
private $beast_strength_max = 90;
10+
private $strength_min = 60;
11+
private $strength_max = 90;
1212

13-
private $beast_defense_min = 40;
14-
private $beast_defense_max = 60;
13+
private $defense_min = 40;
14+
private $defense_max = 60;
1515

16-
private $beast_speed_min = 40;
17-
private $beast_speed_max = 60;
16+
private $speed_min = 40;
17+
private $speed_max = 60;
1818

19-
private $beast_luck_min = 25;
20-
private $beast_luck_max = 40;
21-
22-
private $beast_health_last = 0;
23-
private $beast_strength_last = 0;
24-
private $beast_defense_last = 0;
25-
private $beast_speed_last = 0;
26-
private $beast_luck_last = 0;
19+
private $luck_min = 25;
20+
private $luck_max = 40;
2721

2822
public function __construct()
2923
{
30-
$this->health = rand($this->beast_health_min, $this->beast_health_max);
31-
$this->strength = rand($this->beast_strength_min, $this->beast_strength_max);
32-
$this->defense = rand($this->beast_defense_min, $this->beast_defense_max);
33-
$this->speed = rand($this->beast_speed_min, $this->beast_speed_max);
34-
$this->luck = rand($this->beast_luck_min, $this->beast_luck_max);
24+
$this->health = rand($this->health_min, $this->health_max);
25+
$this->strength = rand($this->strength_min, $this->strength_max);
26+
$this->defense = rand($this->defense_min, $this->defense_max);
27+
$this->speed = rand($this->speed_min, $this->speed_max);
28+
$this->luck = rand($this->luck_min, $this->luck_max);
3529
}
3630
}

classes/Game.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,20 @@ public function __construct($ui)
1818

1919
public function display_stats()
2020
{
21-
$this->ui->display("--");
2221
$this->display_individual_stats($this->orderus);
2322
$this->display_individual_stats($this->beast);
24-
$this->ui->display("--");
2523
}
2624

2725
private function display_individual_stats($player)
2826
{
29-
$this->ui->display([
30-
"$player->name:",
31-
" Health: " . $player->health,
32-
" Strength: " . $player->strength,
33-
" Defense: " . $player->defense,
34-
" Speed: " . $player->speed,
35-
" Luck: " . $player->luck,
36-
]);
27+
$changes = $player->stat_changes();
28+
if (count($changes) > 0) {
29+
$this->ui->display("| $player->name:");
30+
foreach ($changes as $change) {
31+
$this->ui->display("| " . $change->type . ": " . $change->value);
32+
}
33+
}
34+
$player->save_stats();
3735
}
3836

3937
public function play_round()

classes/Orderus.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
11
<?php
22

3-
class Orderus
3+
class Orderus extends Player
44
{
55
public $name = "Orderus";
66

7-
private $orderus_health_min = 70;
8-
private $orderus_health_max = 100;
7+
private $health_min = 70;
8+
private $health_max = 100;
99

10-
private $orderus_strength_min = 70;
11-
private $orderus_strength_max = 80;
10+
private $strength_min = 70;
11+
private $strength_max = 80;
1212

13-
private $orderus_defense_min = 45;
14-
private $orderus_defense_max = 55;
13+
private $defense_min = 45;
14+
private $defense_max = 55;
1515

16-
private $orderus_speed_min = 40;
17-
private $orderus_speed_max = 50;
16+
private $speed_min = 40;
17+
private $speed_max = 50;
1818

19-
private $orderus_luck_min = 10;
20-
private $orderus_luck_max = 30;
21-
22-
private $orderus_health_last = 0;
23-
private $orderus_strength_last = 0;
24-
private $orderus_defense_last = 0;
25-
private $orderus_speed_last = 0;
26-
private $orderus_luck_last = 0;
19+
private $luck_min = 10;
20+
private $luck_max = 30;
2721

2822
public function __construct()
2923
{
30-
$this->health = rand($this->orderus_health_min, $this->orderus_health_max);
31-
$this->strength = rand($this->orderus_strength_min, $this->orderus_strength_max);
32-
$this->defense = rand($this->orderus_defense_min, $this->orderus_defense_max);
33-
$this->speed = rand($this->orderus_speed_min, $this->orderus_speed_max);
34-
$this->luck = rand($this->orderus_luck_min, $this->orderus_luck_max);
24+
$this->health = rand($this->health_min, $this->health_max);
25+
$this->strength = rand($this->strength_min, $this->strength_max);
26+
$this->defense = rand($this->defense_min, $this->defense_max);
27+
$this->speed = rand($this->speed_min, $this->speed_max);
28+
$this->luck = rand($this->luck_min, $this->luck_max);
3529
}
3630
}

classes/Player.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?
2+
3+
Class Player
4+
{
5+
public $name;
6+
7+
private $health_min;
8+
private $health_max;
9+
10+
private $strength_min;
11+
private $strength_max;
12+
13+
private $defense_min;
14+
private $defense_max;
15+
16+
private $speed_min;
17+
private $speed_max;
18+
19+
private $luck_min;
20+
private $luck_max;
21+
22+
private $health_last = 0;
23+
private $strength_last = 0;
24+
private $defense_last = 0;
25+
private $speed_last = 0;
26+
private $luck_last = 0;
27+
28+
public function stat_changes()
29+
{
30+
$stat_changes = array();
31+
if ($this->health != $this->health_last) {
32+
$change = new StdClass();
33+
$change->type = "Health";
34+
$change->value = $this->health;
35+
array_push($stat_changes, $change);
36+
}
37+
if ($this->strength != $this->strength_last) {
38+
$change = new StdClass();
39+
$change->type = "Strength";
40+
$change->value = $this->strength;
41+
array_push($stat_changes, $change);
42+
}
43+
if ($this->defense != $this->defense_last) {
44+
$change = new StdClass();
45+
$change->type = "Defense";
46+
$change->value = $this->defense;
47+
array_push($stat_changes, $change);
48+
}
49+
if ($this->speed != $this->speed_last) {
50+
$change = new StdClass();
51+
$change->type = "Speed";
52+
$change->value = $this->speed;
53+
array_push($stat_changes, $change);
54+
}
55+
if ($this->luck != $this->luck_last) {
56+
$change = new StdClass();
57+
$change->type = "Luck";
58+
$change->value = $this->luck;
59+
array_push($stat_changes, $change);
60+
}
61+
return $stat_changes;
62+
}
63+
64+
public function save_stats()
65+
{
66+
$this->health_last = $this->health;
67+
$this->strength_last = $this->strength;
68+
$this->defense_last = $this->defense;
69+
$this->speed_last = $this->speed;
70+
$this->luck_last = $this->luck;
71+
}
72+
}

index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
include_once('classes/UI.php');
44
include_once('classes/Game.php');
55
include_once('classes/Narration.php');
6+
include_once('classes/Player.php');
67
include_once('classes/Orderus.php');
78
include_once('classes/Beast.php');
89

0 commit comments

Comments
 (0)