-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.php
301 lines (235 loc) · 6.15 KB
/
game.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
$numPlayers = 4;
abstract class Strategy {
public $players;
/**
* This is an array of Chunk objects.
*/
public $chunks;
public function __construct() {
$this->chunks = array(new Chunk(0));
}
public function getChunk($card) {
foreach ($this->chunks as $chunk) {
if ($chunk->min <= $card->criteria && $chunk->max > $card->criteria) {
return $chunk;
}
}
throw new Exception("Couldn't find a chunk for ".$card->criteria.", shouldn't happen");
}
public function rebalance() {
global $numPlayers;
// check for max
$max = 0;
$maxVal = -1;
foreach (Chunk::$countPerPlayer as $player => $count) {
if ($count > $maxVal) {
$max = $player;
$maxVal = $count;
}
}
// migrate if imbalanced
for ($i=0; $i<10; $i++) {
// crappiest balancer ever
$min = rand(0,$numPlayers-1);
if (Chunk::$countPerPlayer[$min] < Chunk::$countPerPlayer[$max]) {
foreach ($this->chunks as $chunk) {
if ($chunk->player == $max) {
$chunk->player = $min;
Chunk::$countPerPlayer[$max]--;
Chunk::$countPerPlayer[$min]++;
return;
}
}
}
}
}
protected function checkBalance($chunk) {
if (count($chunk->cards) >= 4) {
$this->chunks[] = $chunk->split();
$this->rebalance();
}
}
abstract public function addCard($move);
abstract public function __toString();
}
class Card {
private $move;
public $criteria;
public function __construct($move) {
$this->move = $move;
}
public function getDeck() {
return (int)($this->move / 52) + 1;
}
public function getSuit() {
$suitNum = (int)($this->move / 13) % 4;
switch ($suitNum) {
case 0:
return "♠";
case 1:
return "♥";
case 2:
return "♦";
case 3:
return "♣";
}
}
public function getCard() {
$cardNum = $this->move % 13;
switch ($cardNum) {
case 9:
return "Jack";
case 10:
return "Queen";
case 11:
return "King";
case 12:
return "Ace";
default:
return $cardNum+2;
}
}
public function draw() {
echo "<div><code><div>{</div><div class=\"field\">\"criteria\" : ".json_encode($this->criteria).
", </div><div class=\"field\">\"deck\" : ".$this->getDeck().
", </div><div class=\"field\">\"suit\" : \"".$this->getSuit().
"\", </div><div class=\"field\">\"card\" : \"".$this->getCard().
"\"</div><div>}</div></code></div>";
}
}
class Chunk {
public static $countPerPlayer;
// integer, 0 --> num players-1
public $player;
public $cards;
public $min;
public $max;
public function __construct($player) {
$this->player = $player;
$this->cards = array();
Chunk::$countPerPlayer[$player]++;
}
public function setCards($cards) {
$this->cards = $cards;
}
public static function setupCounts() {
global $numPlayers;
for ($i=0; $i<$numPlayers; $i++) {
Chunk::$countPerPlayer[$i] = 0;
}
}
public function add($card) {
for ($i=0; $i<count($this->cards); $i++) {
if ($this->cards[$i]->criteria >= $card->criteria) {
array_splice($this->cards, $i, 0, array($card));
return;
}
}
$this->cards[] = $card;
}
public function split() {
$newChunk = new Chunk($this->player);
$middle = (int)(count($this->cards)/2);
// middle to eo array
$newChunk->setCards(array_slice($this->cards, $middle));
$newChunk->min = $newChunk->cards[0]->criteria;
$newChunk->max = $this->max;
// beginning to middle of array
$this->setCards(array_slice($this->cards, 0, $middle));
// keep min
$this->max = $newChunk->cards[0]->criteria;
return $newChunk;
}
public function draw() {
echo "<td><div class=\"chunkRange\"><b>".$this->min." → ".$this->max."</b></div>";
foreach ($this->cards as $card) {
$card->draw();
}
echo "</td>";
}
}
class Ascending extends Strategy {
public function __construct() {
parent::__construct();
$this->chunks[0]->min = 0;
$this->chunks[0]->max = 999999;
}
public function addCard($move) {
$card = new Card($move);
$card->criteria = $move;
$chunk = $this->getChunk($card);
$chunk->add($card);
$this->checkBalance($chunk);
return $card;
}
public function __toString() {
return "(ascending)";
}
}
class Random extends Strategy {
public function __construct() {
parent::__construct();
$this->chunks[0]->min = 0;
$this->chunks[0]->max = 999999;
}
public function addCard($move) {
$card = new Card($move);
$card->criteria = rand(0, 999999);
$chunk = $this->getChunk($card);
$chunk->add($card);
$this->checkBalance($chunk);
return $card;
}
public function __toString() {
return "(random)";
}
}
class CoarseAscendingCombo extends Strategy {
public function __construct() {
parent::__construct();
$this->chunks[0]->min = "000000 000000";
$this->chunks[0]->max = "999999 999999";
}
public function addCard($move) {
$card = new Card($move);
$card->criteria = str_pad($card->getDeck(), 6, "0", STR_PAD_LEFT)
." ".str_pad(rand(0, 999999), 6, "0", STR_PAD_LEFT);
$chunk = $this->getChunk($card);
$chunk->add($card);
$this->checkBalance($chunk);
return $card;
}
public function __toString() {
return "Coarse-grained ascending + random";
}
}
function drawTable($chunks) {
// this is memory-inefficient, but chunks are stored by range and displayed
// by player.
global $numPlayers;
$player = array();
for ($i=0; $i<$numPlayers; $i++) {
$player[] = array();
}
foreach ($chunks as $chunk) {
$player[$chunk->player][] = $chunk;
}
$more = 4;
$count = 0;
while ($more > 0 && $count < 10) {
echo "<tr>";
$more = 0;
for ($i=0; $i<4; $i++) {
if (!isset($player[$i][$count])) {
echo "<td></td>";
continue;
}
$more++;
$player[$i][$count]->draw();
}
echo "</tr>";
$count++;
}
}
?>