-
Notifications
You must be signed in to change notification settings - Fork 48
/
functions.php
92 lines (82 loc) · 2.58 KB
/
functions.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
function get_ships()
{
return array(
'starfighter' => array(
'name' => 'Jedi Starfighter',
'weapon_power' => 5,
'jedi_factor' => 15,
'strength' => 30,
),
'cloakshape_fighter' => array(
'name' => 'CloakShape Fighter',
'weapon_power' => 2,
'jedi_factor' => 2,
'strength' => 70,
),
'super_star_destroyer' => array(
'name' => 'Super Star Destroyer',
'weapon_power' => 70,
'jedi_factor' => 0,
'strength' => 500,
),
'rz1_a_wing_interceptor' => array(
'name' => 'RZ-1 A-wing interceptor',
'weapon_power' => 4,
'jedi_factor' => 4,
'strength' => 50,
),
);
}
/**
* Our complex fighting algorithm!
*
* @return array With keys winning_ship, losing_ship & used_jedi_powers
*/
function battle(array $ship1, $ship1Quantity, array $ship2, $ship2Quantity)
{
$ship1Health = $ship1['strength'] * $ship1Quantity;
$ship2Health = $ship2['strength'] * $ship2Quantity;
$ship1UsedJediPowers = false;
$ship2UsedJediPowers = false;
while ($ship1Health > 0 && $ship2Health > 0) {
// first, see if we have a rare Jedi hero event!
if (didJediDestroyShipUsingTheForce($ship1)) {
$ship2Health = 0;
$ship1UsedJediPowers = true;
break;
}
if (didJediDestroyShipUsingTheForce($ship2)) {
$ship1Health = 0;
$ship2UsedJediPowers = true;
break;
}
// now battle them normally
$ship1Health = $ship1Health - ($ship2['weapon_power'] * $ship2Quantity);
$ship2Health = $ship2Health - ($ship1['weapon_power'] * $ship1Quantity);
}
if ($ship1Health <= 0 && $ship2Health <= 0) {
// they destroyed each other
$winningShip = null;
$losingShip = null;
$usedJediPowers = $ship1UsedJediPowers || $ship2UsedJediPowers;
} elseif ($ship1Health <= 0) {
$winningShip = $ship2;
$losingShip = $ship1;
$usedJediPowers = $ship2UsedJediPowers;
} else {
$winningShip = $ship1;
$losingShip = $ship2;
$usedJediPowers = $ship1UsedJediPowers;
}
return array(
'winning_ship' => $winningShip,
'losing_ship' => $losingShip,
'used_jedi_powers' => $usedJediPowers,
);
}
function didJediDestroyShipUsingTheForce(array $ship)
{
$jediHeroProbability = $ship['jedi_factor'] / 100;
return mt_rand(1, 100) <= ($jediHeroProbability*100);
}