-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
233 lines (184 loc) · 5.65 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
class Player
{
private $diceInCup = [];
private $name;
private $position;
private $point;
public function getDiceInCup()
{
return $this->diceInCup;
}
public function getName()
{
return $this->name;
}
public function getPosition()
{
return $this->position;
}
public function __construct($numberOfDice, $position, $name = '')
{
$this->point = 0;
$this->position = $position;
$this->name = $name;
for ($i = 0; $i < $numberOfDice; $i++) {
array_push($this->diceInCup, new Dice());
}
}
public function addPoint($point)
{
$this->point += $point;
}
public function getPoint()
{
return $this->point;
}
public function play()
{
foreach($this->diceInCup as $dice){
$dice->roll();
}
}
public function removeDice($key)
{
unset($this->diceInCup[$key]);
}
public function insertDice($dice)
{
array_push($this->diceInCup, $dice);
}
}
class Dice
{
private $topSideVal;
public function getTopSideVal()
{
return $this->topSideVal;
}
public function roll()
{
$this->topSideVal = rand(1,6);
return $this;
}
public function setTopSideVal($topSideVal)
{
$this->topSideVal = $topSideVal;
return $this;
}
}
class Game
{
private $players = [];
private $round;
private $numberOfPlayer;
private $numberOfDicePerPlayer;
public function __construct($numberOfPlayer, $numberOfDicePerPlayer)
{
$this->round = 0;
$this->numberOfPlayer = $numberOfPlayer;
$this->numberOfDicePerPlayer = $numberOfDicePerPlayer;
for ($i = 0; $i < $this->numberOfPlayer; $i++) {
$this->players[$i] = new Player($this->numberOfDicePerPlayer, $i, $i + 1);
}
}
private function displayRound()
{
echo "<strong>Turn {$this->round}</strong><br/>\r\n";
return $this;
}
private function displayTopSideDice($title = 'Roll the dice')
{
echo "<span><u>{$title} :</u></span><br/>";
foreach ($this->players as $player) {
echo "Player #{$player->getName()} ({$player->getPoint()}) : ";
$diceTopSide = '';
foreach ($player->getDiceInCup() as $dice) {
$diceTopSide .= $dice->getTopSideVal() . ", ";
}
if(!$diceTopSide) {
$diceTopSide = '_ (Stop playing because it has no dice)';
}
echo rtrim($diceTopSide, ', ') . "<br/>\r\n";
}
echo "<br/>\r\n";
return $this;
}
public function lastPlayerHasDice()
{
foreach ($this->players as $player) {
if(sizeof($player->getDiceInCup())){
echo "Game ends because only player <strong>#{$player->getName()}</strong> has dice. <br/>\r\n";
return $this;
}
}
}
public function displayWinner($player)
{
echo "Game won by player <strong>#{$player->getName()}</strong> because it has more points than other players. <br/>\r\n";
return $this;
}
public function start()
{
echo "Player = {$this->numberOfPlayer}, Dice = {$this->numberOfDicePerPlayer}<br/><br/>\r\n";
while (true) {
$this->round++;
$diceCarryForward = [];
foreach ($this->players as $player) {
$player->play();
}
$this->displayRound()->displayTopSideDice();
foreach ($this->players as $index => $player) {
$tempDiceArray = [];
foreach ($player->getDiceInCup() as $diceIndex => $dice) {
if ($dice->getTopSideVal() == 6) {
$player->addPoint(1);
$player->removeDice($diceIndex);
}
if ($dice->getTopSideVal() == 1) {
if ($player->getPosition() == ($this->numberOfPlayer - 1)) {
$this->players[0]->insertDice($dice);
$player->removeDice($diceIndex);
} else {
array_push($tempDiceArray, $dice);
$player->removeDice($diceIndex);
}
}
}
$diceCarryForward[$index + 1] = $tempDiceArray;
if (array_key_exists($index, $diceCarryForward) && count($diceCarryForward[$index]) > 0) {
foreach ($diceCarryForward[$index] as $dice) {
$player->insertDice($dice);
}
$diceCarryForward = [];
}
}
$this->displayTopSideDice("After Evaluation");
$playerHasDice = $this->numberOfPlayer;
foreach ($this->players as $player) {
if (count($player->getDiceInCup()) <= 0) {
$playerHasDice--;
}
}
if ($playerHasDice == 1) {
$this->lastPlayerHasDice();
$this->displayWinner($this->getWinner());
break;
}
}
}
private function getWinner()
{
$winner = null;
$highscore = 0;
foreach ($this->players as $player) {
if ($player->getPoint() > $highscore) {
$highscore = $player->getPoint();
$winner = $player;
}
}
return $winner;
}
}
$game = new Game(3, 4);
$game->start();