-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomino.c
492 lines (457 loc) · 11.3 KB
/
domino.c
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* domino.c
*
* Created on: Nov 11, 2014
* Author: giovanni
*/
#include "domino.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
//ANSI colors code
#define COLOR_RESET "\x1B[0m"
#define COLOR_GREEN "\x1B[32m"
unsigned long long int typedef bign;
//header of private functions
//creates a pA with intersection of pieces user bought and compatibles pieces
pieceArray getPiecesUserBought(pieceArray playerHand, pieceArray userBought,
piece matchingPiece);
//creates a pA with intersection of doubles and compatible pieces
pieceArray getDoubles(pieceArray pA);
//search a piece inside a pieceArray and return index or NONE if hasn't
int getPieceIndex(pieceArray pA, piece p);
//checks if pieces equals with permutation
int pieceEquals(piece p, piece q);
//creates a pA with pieces that appears more times
pieceArray getMostRepeatedSides(pieceArray pA, int version);
//returns max times of side appears inside a pieceArray
int getMaxTimesOfRepetition(pieceArray pA, int version);
//get repetition times of certain side inside a pieceArray
int getRepetitionTimes(pieceArray pA, int s);
//get piece that matches the table
piece getMatchingPiece(pieceArray table);
//returns an index between 0 and n-1
int randomIndexNumber(int n);
//returns the number of possible combinations of n different elements taken two to two
bign combination(int n, int p);
//function factorial defined in R+
bign factorial(int n);
void initPiece(piece *p, int a, int b) {
p->side[0] = a;
p->side[1] = b;
}
void initPieceArray(pieceArray *pA) {
pA->piece = NULL;
pA->size = 0;
}
piece getPiece(pieceArray pA, int index) {
if (index < 0 || index >= pA.size) {
piece ret;
ret.side[0] = ERROR;
ret.side[1] = ERROR;
return ret;
} else {
return pA.piece[index];
}
}
int setPiece(pieceArray *pA, int index, piece piece) {
if (index < 0 || index >= pA->size) {
return ERROR;
} else {
pA->piece[index] = piece;
return SUCESS;
}
}
int addPiece(pieceArray *pA, int where, piece piece) {
if (where == BOTH) {
where = END;
}
if (!(where == BEGIN || where == END)) {
return ERROR;
} else {
if (pA->size != 0 && pA->piece[pA->size - 1].side[1] != piece.side[0]) {
permutPiece(&piece);
}
if ((pA->piece = realloc(pA->piece, sizeof(piece) * (pA->size + 1)))
== NULL) {
return ERROR;
}
pA->size++;
}
if (where == BEGIN) {
//increase all piece index
if (pA->size != 0 && pA->piece[0].side[0] != piece.side[1]) {
permutPiece(&piece);
}
int i;
for (i = pA->size - 1; i > 0; i--) {
pA->piece[i] = pA->piece[i - 1];
}
//insert piece first index
pA->piece[0] = piece;
} else if (where == END) {
//insert piece last index
pA->piece[pA->size - 1] = piece;
}
return SUCESS;
}
void permutPiece(piece *piece) {
int aux = piece->side[0];
piece->side[0] = piece->side[1];
piece->side[1] = aux;
}
int removePiece(pieceArray *pA, int index) {
if (index < 0 || index >= pA->size) {
return ERROR;
} else {
//decreases all pieces index from removed to the end
int i;
for (i = index; i < pA->size; i++) {
pA->piece[i] = pA->piece[i + 1];
}
if (pA->size - 1 == 0) {
free(pA->piece);
pA->piece = NULL;
pA->size--;
return SUCESS;
}
if ((pA->piece = realloc(pA->piece, sizeof(piece) * (pA->size - 1)))
== NULL) {
return ERROR;
} else {
pA->size--;
return SUCESS;
}
}
}
int generatePieces(pieceArray *pA, int version) {
//adds 1 to version because elements can make combination to itself
bign n = combination(version + 1, 2);
if (n == ERROR) {
return ERROR;
} else {
int i, j;
for (i = 0; i <= version; i++) {
for (j = i; j <= version; j++) {
piece p;
initPiece(&p, i, j);
addPiece(pA, END, p);
}
}
return SUCESS;
}
}
int distributePiece(pieceArray *from, pieceArray *to, int piecesPerPlayer) {
int i;
for (i = 0; i < piecesPerPlayer; i++) {
if (from->size == 0) {
return ERROR;
}
int rand = randomIndexNumber(from->size);
piece p = from->piece[rand];
movePiece(from, to, rand, END);
}
return SUCESS;
}
pieceResponse getFirstPlayer(pieceArray playersHand[], int playersNumber,
int version) {
pieceResponse result;
int i, j, k;
//start checking from bigger possible piece to the smaller one
for (i = version; i >= 0; i--) {
//search all players hands
for (j = 0; j < playersNumber; j++) {
pieceArray player = playersHand[j];
//search all players pieces
for (k = 0; k < player.size; k++) {
piece piece = player.piece[k];
if (piece.side[0] == i && piece.side[1] == i) {
result.player = j;
result.index = k;
return result;
}
}
}
}
result.player = NONE;
result.index = NONE;
return result;
}
int checkPieceCompatibility(pieceArray pA, piece p) {
if (pA.size == 0) {
return END;
} else {
int aux = 0;
if (pA.piece[0].side[0] == p.side[0]
|| pA.piece[0].side[0] == p.side[1]) {
aux++;
}
if (pA.piece[pA.size - 1].side[1] == p.side[0]
|| pA.piece[pA.size - 1].side[1] == p.side[1]) {
aux = aux + 2;
}
switch (aux) {
case 0:
return NOT_COMPATIBLE;
case 1:
return BEGIN;
case 2:
return END;
case 3:
return BOTH;
}
}
}
int hasCompatiblePiece(pieceArray table, pieceArray hand) {
int i;
for (i = 0; i < hand.size; i++) {
if (checkPieceCompatibility(table, hand.piece[i]) != NOT_COMPATIBLE) {
return TRUE;
}
}
return FALSE;
}
int buyPiece(pieceArray *from, pieceArray *to) {
//there's no more pieces to be bought
if (from->size == 0) {
return CANNOT_BUY;
}
int randi = randomIndexNumber(from->size);
if ((addPiece(to, END, getPiece(*from, randi)) && removePiece(from, randi))
== SUCESS) {
return SUCESS;
} else {
return ERROR;
}
}
int movePiece(pieceArray *from, pieceArray *to, int pieceIndex, int where) {
if (addPiece(to, where, from->piece[pieceIndex])
&& removePiece(from, pieceIndex)) {
return SUCESS;
} else {
return FALSE;
}
}
int getBestPieceIndexToMove(pieceArray table, pieceArray simHand, int version,
pieceArray userBought, int mode) {
//get all compatibles pieces
pieceArray compatibles;
initPieceArray(&compatibles);
int i;
for (i = 0; i < simHand.size; i++) {
if (checkPieceCompatibility(table, simHand.piece[i]) != NOT_COMPATIBLE) {
addPiece(&compatibles, END, simHand.piece[i]);
}
}
//if mode of game is EASY stops algorithm by here returning first piece
if (mode == EASY) {
return getPieceIndex(simHand, compatibles.piece[0]);
}
//if there's doubles, remove all non-doubles pieces of compatibles
pieceArray doubles = getDoubles(compatibles);
if (doubles.size != 0) {
compatibles = doubles;
}
//if mode of game is MEDIUM stops algorithm by here returning first piece
if (mode == MEDIUM) {
return getPieceIndex(simHand, compatibles.piece[0]);
}
//get all pieces that has side B user already bought
piece matchingPiece = getMatchingPiece(table);
pieceArray bought = getPiecesUserBought(compatibles, userBought,
matchingPiece);
if (bought.size != 0) {
compatibles = bought;
}
//get pieces with bigger repetition number
pieceArray repeatedSides = getMostRepeatedSides(compatibles, version);
if (repeatedSides.size != 0) {
compatibles = repeatedSides;
}
//return for HARD mode
return getPieceIndex(simHand, compatibles.piece[0]);
}
piece getMatchingPiece(pieceArray table) {
piece matchingPiece;
matchingPiece.side[0] = table.piece[0].side[0];
matchingPiece.side[1] = table.piece[table.size - 1].side[1];
return matchingPiece;
}
pieceArray getPiecesUserBought(pieceArray playerHand, pieceArray userBought,
piece matchingPiece) {
pieceArray response;
initPieceArray(&response);
int i, j;
for (i = 0; i < playerHand.size; i++) {
piece p = playerHand.piece[i];
if (p.side[0] == matchingPiece.side[0]
|| p.side[0] == matchingPiece.side[1]) {
p.side[0] = -1;
}
if (p.side[1] == matchingPiece.side[0]
|| p.side[1] == matchingPiece.side[1]) {
p.side[1] = -1;
}
for (j = 0; j < userBought.size; j++) {
piece p2 = userBought.piece[j];
if (sideEquals(p, p2)
&& !containsPiece(response, playerHand.piece[i])) {
addPiece(&response, END, playerHand.piece[i]);
}
}
}
return response;
}
int sideEquals(piece p, piece q) {
if (p.side[0] == q.side[0] || p.side[0] == q.side[1]
|| p.side[1] == q.side[0] || p.side[1] == q.side[1]) {
return TRUE;
}
return FALSE;
}
pieceArray getDoubles(pieceArray pA) {
pieceArray doubles;
initPieceArray(&doubles);
int i;
for (i = 0; i < pA.size; i++) {
piece p = pA.piece[i];
if (p.side[0] == p.side[1]) {
addPiece(&doubles, END, p);
}
}
return doubles;
}
pieceArray getMostRepeatedSides(pieceArray pA, int version) {
pieceArray mostRepeated;
initPieceArray(&mostRepeated);
int i, j;
int maxRepeats = getMaxTimesOfRepetition(pA, version);
//check max number of ocurrences of some side and remove other sides with less repetitions
for (i = 0; i <= version; i++) {
if (getRepetitionTimes(pA, i) == maxRepeats) {
for (j = 0; j < pA.size; j++) {
piece p = pA.piece[j];
if (((p.side[0] == i) || (p.side[1] == i))
&& !containsPiece(mostRepeated, p)) {
addPiece(&mostRepeated, END, p);
}
}
}
}
return mostRepeated;
}
int containsPiece(pieceArray pA, piece p) {
int i;
for (i = 0; i < pA.size; i++) {
if (pieceEquals(pA.piece[i], p)) {
return TRUE;
}
}
return FALSE;
}
int getMaxTimesOfRepetition(pieceArray pA, int version) {
int i, j, max = 0, repeats;
for (i = 0; i <= version; i++) {
repeats = getRepetitionTimes(pA, i);
if (repeats > max) {
max = repeats;
}
}
return max;
}
int getRepetitionTimes(pieceArray pA, int s) {
int i, result = 0;
for (i = 0; i < pA.size; i++) {
piece p = pA.piece[i];
if (p.side[0] == s || p.side[1] == s) {
result++;
}
}
return result;
}
int getPieceIndex(pieceArray pA, piece p) {
int i;
for (i = 0; i < pA.size; i++) {
if (pieceEquals(pA.piece[i], p)) {
return i;
}
}
return NONE;
}
int pieceEquals(piece p, piece q) {
if ((p.side[0] == q.side[0] && p.side[1] == q.side[1])
|| (p.side[1] == q.side[0] && p.side[0] == q.side[1])) {
return TRUE;
} else {
return FALSE;
}
}
void printPieceArray(pieceArray pA) {
int i;
for (i = 0; i < pA.size; i++) {
printf("[%d|%d] ", pA.piece[i].side[0], pA.piece[i].side[1]);
}
printf("\n");
}
void printHighlightedPieceArray(pieceArray pA, int index) {
int i;
for (i = 0; i < pA.size; i++) {
if (i != index) {
printf("[%d|%d] ", pA.piece[i].side[0], pA.piece[i].side[1]);
} else {
printf(COLOR_GREEN);
printf("[%d|%d] ", pA.piece[i].side[0], pA.piece[i].side[1]);
printf(COLOR_RESET);
}
}
printf("\n");
}
void highPrintf(char *format, ...) {
//reimplementation of printf with green color output
va_list argptr;
va_start(argptr, format);
printf(COLOR_GREEN);
vprintf(format, argptr);
printf(COLOR_RESET);
va_end(argptr);
}
int randomIndexNumber(int n) {
return (rand() % n);
}
bign combination(int n, int p) {
//check if function is defined in n and p
if (n >= p && p > 0) {
bign result = 1;
int i, bp, wasDiv = 0;
bign div;
//pratices to avoid overflow
if (p > (n - p)) {
bp = p;
div = factorial(n - p);
} else {
bp = n - p;
div = factorial(p);
}
for (i = n; i > bp; i--) {
result *= i;
if (!wasDiv && !(result % div)) {
result /= div;
wasDiv = 1;
}
}
return result;
} else {
return ERROR;
}
}
bign factorial(int n) {
//recursive implementation of factorial
if (n > 0) {
return n * factorial(n - 1);
} else if (!n) {
return 1;
} else {
return ERROR;
}
}