-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlista.c
370 lines (290 loc) · 8.54 KB
/
lista.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define PIECE_MAX 7
#define INF 1123
#define PIECE_LENGTH 11
#define PIECE_HEIGHT 4
#define PIECES_PER_LINE 4
typedef struct _pecaDomino{
int numberRight;
int numberLeft;
struct _pecaDomino *right;
struct _pecaDomino *left;
}TppecaDomino;
TppecaDomino * new_list(){
return NULL;
}
void print_deck(TppecaDomino * deck){
int i, piece_ind;
int head_x, head_y;
TppecaDomino * p;
if(deck == NULL) return;
char ** matrix = (char**)malloc(PIECE_HEIGHT * 10 * sizeof(char*));
int number_order[6][2] = {
{1, 1},
{2, 3},
{2, 1},
{1, 3},
{1, 2},
{2, 2}
};
for(i = 0; i < PIECE_HEIGHT * 10; i++){
matrix[i] = (char*)malloc(PIECE_LENGTH * PIECES_PER_LINE * sizeof(char) + sizeof(char));
memset(matrix[i], ' ', PIECE_LENGTH * 4 * sizeof(char));
matrix[i][PIECE_LENGTH * PIECES_PER_LINE] = '\0';
}
for(p = deck, piece_ind = 0; p != NULL; p = p->right, piece_ind++){
head_x = piece_ind % PIECES_PER_LINE * PIECE_LENGTH;
head_y = piece_ind / PIECES_PER_LINE * PIECE_HEIGHT;
for(i = 0; i < PIECE_LENGTH; i++){
matrix[head_y][head_x + i] = '-';
matrix[head_y + PIECE_HEIGHT - 1][head_x + i] = '-';
}
for(i = 0; i < PIECE_HEIGHT; i++){
matrix[head_y + i][head_x] = '|';
matrix[head_y + i][head_x + PIECE_LENGTH -1] = '|';
}
for(i = 1; i < PIECE_HEIGHT - 1; i++){
matrix[head_y + i][head_x + 5] = '|';
}
for(i = 0; i < p->numberLeft; i++){
matrix[head_y + number_order[i][0]][head_x + number_order[i][1]] = 'o';
}
for(i = 0; i < p->numberRight; i++){
matrix[head_y + number_order[i][0]][head_x + number_order[i][1] + 5] = 'o';
}
}
for(i = 0; i < piece_ind / PIECES_PER_LINE * PIECE_HEIGHT + PIECE_HEIGHT; i++){
printf("%s\n", matrix[i]);
}
free(matrix);
}
TppecaDomino * turn_piece(TppecaDomino * peca){
int aux = peca->numberRight;
peca->numberRight = peca->numberLeft;
peca->numberLeft = aux;
return peca;
}
int deck_size(TppecaDomino * list_a){
int i;
TppecaDomino * q;
if(list_a == NULL) return 0;
for(i = 0, q = list_a; q != NULL; q = q->right){
i++;
}
return i;
}
TppecaDomino * add_left(TppecaDomino * list_a, int numberRight, int numberLeft){
TppecaDomino * new_piece = (TppecaDomino*)malloc(sizeof(TppecaDomino));
new_piece->numberRight = numberRight;
new_piece->numberLeft = numberLeft;
if(list_a == NULL){
new_piece->right = NULL;
new_piece->left = NULL;
return new_piece;
}
new_piece->right = list_a;
list_a->left = new_piece;
new_piece->left = NULL;
return new_piece;
}
TppecaDomino * add_right(TppecaDomino * list_a, int numberRight, int numberLeft){
TppecaDomino * new_piece = (TppecaDomino*)malloc(sizeof(TppecaDomino));
TppecaDomino * r;
new_piece->numberRight = numberRight;
new_piece->numberLeft = numberLeft;
if(list_a == NULL){
new_piece->right = NULL;
new_piece->left = NULL;
return new_piece;
}
for(r = list_a; r->right != NULL; r = r->right);
r->right = new_piece;
new_piece->left = r;
new_piece->right = NULL;
return list_a;
}
TppecaDomino * new_deck(){ //retorna uma lista com todas as peças do jogo
int i, j;
TppecaDomino * deck = new_list();
for(i = 0; i <= 6; i++){
for(j = 0; j <= i; j++){
deck = add_left(deck, i, j);
}
}
return deck;
}
int left_table_number(TppecaDomino * deck_table){
//retorna o numberLeft do primeiro elemento
if(deck_table == NULL) return -1;
return deck_table->numberLeft;
}
int right_table_number(TppecaDomino * deck_table){
//retorna o numberRight do último elemento
TppecaDomino * p;
if(deck_table == NULL) return -1;
for(p = deck_table; p->right != NULL; p = p->right);
return p->numberRight;
}
int where_play(TppecaDomino * piece, TppecaDomino * table){
// Verifica se é possível jogar: Retorna 0 se não for possível,
// -1 para esquerda ou 1 para direita
if(table == NULL) return 1;
if(piece->numberLeft == right_table_number(table)) return 1;
if(piece->numberRight == right_table_number(table)){
turn_piece(piece);
return 1;
}
if(piece->numberRight == left_table_number(table)) return -1;
if(piece->numberLeft == left_table_number(table)){
turn_piece(piece);
return -1;
}
return 0;
}
int _where_play(TppecaDomino * table, TppecaDomino * player, int numberRight, int numberLeft){
//uma reutilização de where_play para poder usar a função apenas com
//os números da peça
TppecaDomino * p;
for(p = player; p != NULL; p = p->right){
if( (p->numberLeft == numberLeft && p->numberRight == numberRight) ||
(p->numberLeft == numberRight && p->numberRight == numberLeft) ){
return where_play(p, table);
}
}
return 0;
}
int number_is_in(int * freq_rank, int number){//função utilizada na função frequency_rank
//verifica se um determinado número está contido em um vetor
int i;
for(i = 0; i < PIECE_MAX; i++){
if(number == freq_rank[i]) return 1;
}
return 0;
}
int * frequency_rank(TppecaDomino * list_a){
//retorna um vetor com os números ordenados
//em ordem crescente de incidência
TppecaDomino * p;
int i, j;
int minval, minind;
int * freq = (int*)malloc(PIECE_MAX * sizeof(int));
//vetor com a frequência de cada número
int * freq_rank = (int*)malloc(PIECE_MAX * sizeof(int));
//vetor com a ordem decrescente de inciência
memset(freq, 0, PIECE_MAX * sizeof(int));
memset(freq_rank, -1, PIECE_MAX * sizeof(int));
if(list_a == NULL){
for(i = 0; i < PIECE_MAX; i++){
freq[i] = i;
}
return freq;
}
for(p = list_a; p != NULL; p = p->right){
if(p->numberLeft == p->numberRight){
freq[p->numberLeft]++;
}
else{
freq[p->numberLeft]++;
freq[p->numberRight]++;
}
}
for(i = 0; i <= 6; i++){
minval = INF;
for(j = 0; j <= 6; j++){
if(!number_is_in(freq_rank, j) && freq[j] < minval){
minval = freq[j];
minind = j;
}
}
freq_rank[i] = minind;
}
return freq_rank;
}
TppecaDomino * delete(TppecaDomino * list_a, int numberRight, int numberLeft){
TppecaDomino * piece = list_a;
if(piece == NULL) return list_a;
while(piece->right != NULL && (piece->numberRight != numberRight || piece->numberLeft != numberLeft)){
piece = piece->right;
}
if(piece->right == NULL && piece->left == NULL){
free(piece);
return NULL;
}
if(piece->left == NULL){
list_a = list_a->right;
list_a->left = NULL;
free(piece);
return list_a;
}
if(piece->right == NULL){
piece->left->right = NULL;
free(piece);
return list_a;
}
piece->left->right = piece->right;
piece->right->left = piece->left;
free(piece);
return list_a;
}
TppecaDomino * catch_piece(TppecaDomino * list_a, TppecaDomino ** deck_back){
//funçao que pesca uma peça do monte
TppecaDomino * back = *deck_back;
int size = deck_size(*deck_back);
int position, i;
srand( (unsigned)time(NULL));
if(size > 0) position = rand() % (size);
else position = 0;
if((*deck_back) == NULL){
return list_a;
}
if((*deck_back)->left == NULL && (*deck_back)->right == NULL){
list_a = add_left(list_a, (*deck_back)->numberRight, (*deck_back)->numberLeft);
*deck_back = delete((*deck_back), (*deck_back)->numberRight, (*deck_back)->numberLeft);
return list_a;
}
for(i=1; i < (position + 1); i++){
back = back->right;
}
list_a = add_left(list_a, back->numberRight, back->numberLeft);
*deck_back = delete((*deck_back), back->numberRight, back->numberLeft);
return list_a;
}
TppecaDomino * push_table(TppecaDomino * list_a, TppecaDomino ** table, int right_value, int left_value){
TppecaDomino * piece = list_a;
if(list_a == NULL) return NULL;
while(piece->right != NULL && (piece->numberRight != right_value || piece->numberLeft != left_value)){
piece = piece->right;
}
if(piece->numberRight != right_value || piece->numberLeft != left_value){
piece = list_a;
while(piece->right != NULL && (piece->numberRight != left_value || piece->numberLeft != right_value)){
piece = piece->right;
}
}
if(where_play(piece, (*table)) == 1){
(*table) = add_right((*table), piece->numberRight, piece->numberLeft);
list_a = delete(list_a, piece->numberRight, piece->numberLeft);
return list_a;
}
if(where_play(piece, (*table)) == -1){
(*table) = add_left((*table), piece->numberRight, piece->numberLeft);
list_a = delete(list_a, piece->numberRight, piece->numberLeft);
return list_a;
}
printf("\n\nIMPOSSÍVEL!\n\n"); // Teoricamente nunca entrará;
return list_a;
}
TppecaDomino * sum(TppecaDomino * list_a, TppecaDomino * list_b){
TppecaDomino * n = new_list();
TppecaDomino * p;
for(p = list_a; p != NULL; p = p->right){
n = add_right(n, p->numberRight, p->numberLeft);
}
for(p = list_b; p != NULL; p = p->right){
n = add_right(n, p->numberRight, p->numberLeft);
}
return n;
}