-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.php
More file actions
315 lines (275 loc) · 7.66 KB
/
Game.php
File metadata and controls
315 lines (275 loc) · 7.66 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
* © 2020 n8blake
* SantA Bingo
* Create a Bingo Game
*
* calledNumbers: an array of integers, with the first being 0
* gameID: Int, database id of game
* type: array of strings for each game type: bingo, xes, window, blackout
* start_time, timestamp from database of when game started
* end_time, timestamp from database of when game ended
*
* Initialize the middle value (n2), to be 0
* add 0 to the array of 'calledNumbers'
*
* calledNumbers is an array of integers of numbers
* that have been 'called' for a given game.
*
*/
class Game
{
public $gameID;
public $active;
public $types;
public $calledNumbers;
private $start_time;
private $end_time;
//protected $gameManager;
function __construct()
{
$this->calledNumbers = [0];
$this->types = ["bingo"];
}
// public function setGameManager($manager){
// $this->gameManager = $manager;
// }
// public function save(){
// if(isset($this->gameManager)){
// $gameManager->update($this);
// }
// }
// Return the next number in the game
public function callNextNumber(){
$newNumber = 0;
if(count($this->calledNumbers) >=76){
throw new Exception('All Numbers Called.');
}
do {
$newNumber = rand(1, 75);
} while(in_array($newNumber, $this->calledNumbers));
array_push($this->calledNumbers, $newNumber);
}
//pass an array of integers
public function setNumbers($json){
$numbers = json_decode($json);
$this->calledNumbers = $numbers;
sort($this->calledNumbers);
}
public function resetNumbers(){
$this->calledNumbers = [0];
}
public function getNumbers(){
return $this->calledNumbers;
}
public function getTypes(){
return $this->types;
}
public function addType($type){
// echo "\n";
// echo json_encode($type);
// echo "\n";
// echo json_encode($this->types);
if(!in_array($type, $this->types)){
array_push($this->types, $type);
}
}
public function removeType($type){
//echo "<br>Removing: " . $type . "<br>";
if(in_array($type, $this->types)){
//echo "<br>found: " . $type . "<br>";
$index = array_search($type, $this->types);
//echo $index;
$_types = array();
for($i = 0; $i < count($this->types); $i++){
if($i !== $index){
array_push($_types, $this->types[$i]);
}
}
$this->types = $_types;
}
}
public function setType($type){
$this->types = [$type];
}
public function setGame($game){
if( !isset($game["gameID"]) ||
!isset($game["game_types"]) ||
!isset($game["start_time"]) ||
!isset($game["called_numbers"]) ||
!isset($game["active"])
){
throw new Exception("Invalid Game");
}
$this->gameID = $game["gameID"];
$this->types = json_decode($game["game_types"]);
$this->start_time = $game["start_time"];
$this->end_time = $game["end_time"];
$this->calledNumbers = json_decode($game["called_numbers"]);
$this->active = $game["active"];
}
// standard game
// all numbers of a single letter for a card have been called (a column win)
// the same index of every letter from a card has been called (a row win)
// a diagonal (i.e. S0, a1, n2, t3, A4) for a card has been called (a diagonal win)
// Has this card won?
// If yes, return an array of how many
// ways it has won
// If no, return an empty array.
public function bingo($_card){
$card;
if(isset($_card->card)){
$card = $_card->card;
} else {
$card = $_card;
}
$bingos = [];
$bingos['bingo'] = false;
if(isset($_card->id)){
$bingos['cardID'] = $_card->id;
}
// check of a column win
$bingoColumns = [];
foreach ($card as $letter => $column) {
// echo "<br><br>";
// echo json_encode($letter);
// echo "<br>";
// echo json_encode($column);
$columnBingo = true;
foreach ($column as $number) {
//echo "<br>";
//echo $number;
if(!in_array($number, $this->calledNumbers)){
$columnBingo = false;
}
}
//echo "<br>columnBingo: " . $letter . " " . json_encode($columnBingo);
if($columnBingo && !in_array($letter, $bingoColumns)) {
array_push($bingoColumns, $letter);
$bingos['bingo'] = true;
}
$bingo = $columnBingo;
}
//echo json_encode($bingoColumns);
// check for a row win
// the same index for every column must be called
//
$bingoRows = [];
for($row = 0; $row < 5; $row++){
$rowBingo = true;
foreach ($card as $letter => $column) {
$number = $column[$row];
//echo "<br>";
//echo json_encode($column[$row]);
if(!in_array($number, $this->calledNumbers)){
$rowBingo = false;
}
}
if($rowBingo && !in_array($row, $bingoRows)) {
array_push($bingoRows, $row);
$bingos['bingo'] = true;
}
}
// check for a diagonal win \ or /
// that is:
// S[0], a[1], n[2], t[3], A[4] are all called
// OR
// S[4], a[3], n[2], t[1], A[0] are all called
$bingoDiagonals = [];
//echo "<pre>";
//var_dump($card);
//echo $card->card->S[0];
//echo "</pre>";
if( in_array($card->S[0], $this->calledNumbers) &&
in_array($card->a[1], $this->calledNumbers) &&
in_array($card->n[2], $this->calledNumbers) &&
in_array($card->t[3], $this->calledNumbers) &&
in_array($card->A[4], $this->calledNumbers)
){
array_push($bingoDiagonals, 1);
$bingos['bingo'] = true;
}
if( in_array($card->S[4], $this->calledNumbers) &&
in_array($card->a[3], $this->calledNumbers) &&
in_array($card->n[2], $this->calledNumbers) &&
in_array($card->t[1], $this->calledNumbers) &&
in_array($card->A[0], $this->calledNumbers)
){
array_push($bingoDiagonals, 2);
$bingos['bingo'] = true;
}
$bingos['columns'] = $bingoColumns;
$bingos['rows'] = $bingoRows;
$bingos['diagonals'] = $bingoDiagonals;
//$bingos['bingo'] = false;
return $bingos;
}
public function normalBingo($card){
return (
count($this->bingo($card)['diagonals']) > 0 ||
count($this->bingo($card)['columns']) > 0 ||
count($this->bingo($card)['rows'])
);
}
// X
// both diagonals for a card have been called
public function xes($card){
return count($this->bingo($card)['diagonals']) == 2;
}
// Window
// The S column and A colums have been called AND
// the a0, n0, t0 AND a4, n4, t4 for a card have
// been called
public function window($card){
return (
in_array("S", $this->bingo($card)['columns']) &&
in_array("A", $this->bingo($card)['columns']) &&
in_array(0, $this->bingo($card)['rows']) &&
in_array(4, $this->bingo($card)['rows'])
);
}
// BLACKOUT
// All the numbers for a card have been called
public function blackout($card){
return count($this->bingo($card)['columns']) == 5;
}
public function checkCard($type, $card){
switch ($type) {
case 'X':
return $this->xes($card);
break;
case 'window':
return $this->window($card);
break;
case 'blackout':
return $this->blackout($card);
break;
default:
return $this->normalBingo($card);
break;
}
}
public function checkForBingos($players, $cardManager){
$game = $this;
$bingos = array();
foreach ($game->types as $type) {
foreach ($players as $player) {
$email = $player->email;
$cards = $cardManager->getCards($email);
foreach ($cards as $card) {
$check = $game->checkCard($type, $card);
$bingoData = array();
if($check){
if(!isset($bingos[$email])){
$bingos[$email] = array();
}
$_data = $game->bingo($card);
array_push($bingos[$email], $_data);
}
}
}
}
return $bingos;
}
}
?>