Skip to content

Commit 58b7919

Browse files
committed
initial commit
0 parents  commit 58b7919

File tree

7 files changed

+313
-0
lines changed

7 files changed

+313
-0
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Sources:
2+
https://github.com/jfern01/php-rpg-challenge
3+
https://github.com/freelancerwebro/php-story
4+
5+
The story
6+
=====================
7+
8+
Once upon a time there was a great hero, called Orderus, with some strengths and weaknesses, as all heroes have. After battling all kinds of monsters for more than a hundred years, Orderus now has the following stats:
9+
10+
- Health: 70 - 100
11+
- Strength: 70 - 80
12+
- Defence: 45 – 55
13+
- Speed: 40 – 50
14+
- Luck: 10% - 30% (0% means no luck, 100% lucky all the time)
15+
16+
Also, he possesses 2 skills:
17+
18+
- Rapid strike: Strike twice while it’s his turn to attack; there’s a 10% chance he’ll use this skill every time he attacks
19+
- Magic shield: Takes only half of the usual damage when an enemy attacks; there’s a 20% chance he’ll use this skill every time he defends
20+
21+
Gameplay
22+
=====================
23+
24+
As Orderus walks the ever-green forests of Emagia, he encounters some wild beasts, with the following properties:
25+
26+
- Health: 60 - 90
27+
- Strength: 60 - 90
28+
- Defence: 40 – 60
29+
- Speed: 40 – 60
30+
- Luck: 25% - 40%
31+
32+
You’ll have to simulate a battle between Orderus and a wild beast, either at command line or using a web browser. On every battle, Orderus and the beast must be initialized with random properties, within their ranges.
33+
The first attack is done by the player with the higher speed. If both players have the same speed,than the attack is carried on by the player with the highest luck. After an attack, the players switch roles: the attacker now defends and the defender now attacks.
34+
35+
The damage done by the attacker is calculated with the following formula:
36+
Damage = Attacker strength – Defender defence
37+
38+
The damage is subtracted from the defender’s health. An attacker can miss their hit and do no damage if the defender gets lucky that turn.
39+
40+
Orderus’ skills occur randomly, based on their chances, so take them into account on each turn.
41+
42+
Game over
43+
=====================
44+
45+
The game ends when one of the players remain without health or the number of turns reaches 20.
46+
47+
The application must output the results each turn: what happened, which skills were used (if any), the damage done, defender’s health left.
48+
49+
If we have a winner before the maximum number of rounds is reached, he must be declared.
50+
51+
Rules
52+
=====================
53+
54+
Emagia is a land where magic does happen. Still, for this magic to work, you’ll have to follow these rules:
55+
56+
- Write code in plain PHP, no frameworks, no libraries
57+
- Make sure your application is decoupled, code reusable and scalable. For example, can a new skill easily be added to our hero?
58+
- Is your code bug-free and tested?
59+
- There’s no time limit, take your time for the best approach you can think of

classes/Beast.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
class Beast
4+
{
5+
public $name = "Beast";
6+
7+
private $beast_health_min = 60;
8+
private $beast_health_max = 90;
9+
10+
private $beast_strength_min = 60;
11+
private $beast_strength_max = 90;
12+
13+
private $beast_defense_min = 40;
14+
private $beast_defense_max = 60;
15+
16+
private $beast_speed_min = 40;
17+
private $beast_speed_max = 60;
18+
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;
27+
28+
public function __construct()
29+
{
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);
35+
}
36+
}

classes/Game.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
class Game
4+
{
5+
private $turns = 5; // temporary for initial commit
6+
7+
public function __construct($ui)
8+
{
9+
$this->ui = $ui;
10+
$this->winner = new stdClass();
11+
$this->winner->is_orderus = false;
12+
$this->winner->is_beast = false;
13+
$this->orderus = new Orderus();
14+
$this->beast = new Beast();
15+
}
16+
17+
public $is_running = true;
18+
19+
public function display_stats()
20+
{
21+
$this->ui->display("--");
22+
$this->display_individual_stats($this->orderus);
23+
$this->display_individual_stats($this->beast);
24+
$this->ui->display("--");
25+
}
26+
27+
private function display_individual_stats($player)
28+
{
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+
]);
37+
}
38+
39+
public function play_round()
40+
{
41+
// temporary for initial commit
42+
$this->turns--;
43+
$this->is_running = ($this->turns > 0);
44+
}
45+
}

classes/Narration.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
class Narration
4+
{
5+
public function pick_narration($narrations)
6+
{
7+
if (count($narrations) == 1) {
8+
return $narrations[0];
9+
} else if (count($narrations) > 1) {
10+
return $narrations[rand(0, count($narrations) - 1)];
11+
} else {
12+
return "ERROR";
13+
}
14+
}
15+
16+
public function start()
17+
{
18+
return $this->pick_narration($this->start_narrations);
19+
}
20+
21+
public function round()
22+
{
23+
if (!$this->round_narrated) {
24+
$this->round_narrated = true;
25+
return $this->pick_narration($this->round_narration_start);
26+
}
27+
return $this->pick_narration($this->round_narrations);
28+
}
29+
30+
public function won()
31+
{
32+
return $this->pick_narration($this->won_narrations);
33+
}
34+
35+
public function lost()
36+
{
37+
return $this->pick_narration($this->lost_narrations);
38+
}
39+
40+
public function draw()
41+
{
42+
return $this->pick_narration($this->draw_narrations);
43+
}
44+
45+
// gameplay utterances are inscribed below.
46+
47+
private $start_narrations = array(
48+
"Our great hero, Orderus, enters into battle with a wild beast....",
49+
[
50+
"Once upon a time, there was a great hero named Orderus.",
51+
"After more than a hundred years of battles with all kinds of monsters,",
52+
"he encountered a wild beast while walking the evergreen forests of Emagia.",
53+
],
54+
);
55+
56+
private $round_narrated = false;
57+
private $round_narration_start = array(
58+
"Orderus is encountered by a wild beast!",
59+
[
60+
"As Orderus stands in uffish thought, the Jabberwocky--",
61+
"Oh, wait; it's just a generic wild beast.",
62+
"Anyway, the wild beast, with eyes of flame,",
63+
"comes whiffling through the tulgey wood,",
64+
"and burbles as it comes!",
65+
],
66+
);
67+
private $round_narrations = array(
68+
"Another skirmish ensues.",
69+
"They go at it again.",
70+
"Once again, they fight!",
71+
"The battle continues.",
72+
"They clash...",
73+
);
74+
75+
private $won_narrations = array(
76+
"The game hath ended, but it is not the end, for Orderus hath once again emerged as victor.",
77+
"Huzzah! Orderus has vanquished the foe yet again!",
78+
"And once again, the battle is done, and Orderus lives to face the next glorious battle.",
79+
"And the winner is...Orderus! But, of course.",
80+
"Orderus won; the wild beast is toast.",
81+
);
82+
83+
private $lost_narrations = array(
84+
"Alas, Orderus is no more, for he has finally met his tragic demise. The game is thus over.",
85+
"'Tis a sad day. Orderus was no match for this particular wild beat--or perhaps he was tired.",
86+
"Against all expectations, Orderus is in fact done for.",
87+
"Orderus lost, but the wild beast is at least reasonably battered.",
88+
"Orderus, by the treacherous paw of the wild beast, has been rendered out of order.",
89+
);
90+
91+
private $draw_narrations = array(
92+
"'Tis strange, but the battle appeareth to be a draw.",
93+
);
94+
}

classes/Orderus.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
class Orderus
4+
{
5+
public $name = "Orderus";
6+
7+
private $orderus_health_min = 70;
8+
private $orderus_health_max = 100;
9+
10+
private $orderus_strength_min = 70;
11+
private $orderus_strength_max = 80;
12+
13+
private $orderus_defense_min = 45;
14+
private $orderus_defense_max = 55;
15+
16+
private $orderus_speed_min = 40;
17+
private $orderus_speed_max = 50;
18+
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;
27+
28+
public function __construct()
29+
{
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);
35+
}
36+
}

classes/UI.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
class UI
4+
{
5+
public function display($content)
6+
{
7+
if (is_array($content)) { // multiple lines
8+
foreach ($content as $line) {
9+
echo "$line\n";
10+
}
11+
} else { // single line
12+
echo "$content\n";
13+
}
14+
}
15+
}

index.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
include_once('classes/UI.php');
4+
include_once('classes/Game.php');
5+
include_once('classes/Narration.php');
6+
include_once('classes/Orderus.php');
7+
include_once('classes/Beast.php');
8+
9+
$ui = new UI();
10+
$narration = new Narration();
11+
$game = new Game($ui);
12+
13+
$ui->display($narration->start());
14+
$game->display_stats();
15+
16+
while ($game->is_running) {
17+
$ui->display($narration->round());
18+
$game->play_round();
19+
$game->display_stats();
20+
}
21+
22+
if ($game->winner->is_orderus) {
23+
$ui->display($narration->won());
24+
} else if ($game->winner->is_beast) {
25+
$ui->display($narration->lost());
26+
} else {
27+
$ui->display($narration->draw());
28+
}

0 commit comments

Comments
 (0)