-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
41 lines (36 loc) · 1.21 KB
/
index.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
<?php
class KillingGame
{
private $people;
private $count;
private $holder;
function __construct($people)
{
$this->people = range(1,$people);
$this->count= count($people);
$this->holder=0;//the first person with the knife
}
private function killEmAll($p) {
if(count($p)==1){//base case
return $p[0];
}else{
if($p[$this->holder]==max($p)){//If at the end of circle, kill the first person and pass to the second(new first)
$killed= array_splice($p,0,1);
echo "<h5>Knife passed to person ". max($p) .". Person ".max($p) . " Has killed person " . $killed[0]. "</h5> Remeaining: ". count($p). json_encode($p)." </br>" ;
$this->holder=0;
}else{
$killed= array_splice($p, $this->holder+1,1);
echo "<h5>Knife passed to person ". $p[$this->holder] .". Person ".$p[$this->holder] ." Has killed person " . $killed[0]. "</h5>"."Remeaining: ". count($p). json_encode($p);
$this->holder=$this->holder>=(count($p)-1)?0:$this->holder+1;
}
return $this->killEmAll($p);
}
}
public function kill()
{
return $this->killEmAll($this->people);
}
}
$killingGame= new KillingGame(100);
$lastManStanding=$killingGame->kill();
echo "<strong>Last Man Standing is $lastManStanding";