-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_modules.c
572 lines (484 loc) · 9.44 KB
/
main_modules.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "main_modules.h"
board *init()
{
board *b = malloc(sizeof(board));
// b->vars = malloc(sizeof(var));
// b->vars->idx=-1;
// b->vars->next=NULL;
for (int i = 0; i < 16; i++)
{
b->cells[i] = malloc(sizeof(cell));
b->cells[i]->label = malloc(sizeof(node));
b->cells[i]->val = 0;
b->cells[i]->label->next = NULL;
}
#ifdef DEBUG
// printf("Initialized! Address is %d\n", b);
#endif
return b;
}
void printBoard(board *b)
{
printf("\t\u250c");
for(int i = 0; i<31; i++){
if(i==7|| i==15 || i== 23) printf("\u252c");
else printf("\u2500");
}
printf("\u2510\n");
for (int i = 0; i < 4; i++)
{
printf("\t\u2502");
if(b->cells[i * 4 + 0]->val){
printf(" %5d ", b->cells[i * 4 + 0]->val);
}
else{
printf(" ");
}
printf("\u2502");
if(b->cells[i * 4 + 1]->val){
printf(" %5d ", b->cells[i * 4 + 1]->val);
}
else{
printf(" ");
}
printf("\u2502");
if(b->cells[i * 4 + 2]->val){
printf(" %5d ", b->cells[i * 4 + 2]->val);
}
else{
printf(" ");
}
printf("\u2502");
if(b->cells[i * 4 + 3]->val){
printf(" %5d ", b->cells[i * 4 + 3]->val);
}
else{
printf(" ");
}
printf("\u2502\n");
if(i==3) printf("\t\u2514");
else printf("\t\u251c");
for(int j = 0; j<31; j++){
if(j==7 || j==15 || j== 23){
if(i==3)printf("\u2534");
else printf("\u253c");
}
else printf("\u2500");
}
if(i==3) printf("\u2518\n\n");
else printf("\u2524\n");
}
prompt();
#ifdef DEBUG
printf("\n");
for(int i=0; i<16; i++){
printf("%d: val %d\n", i, b->cells[i]->val);
//printLabel(b->cells[i], i);
}
printf("-------\n");
#endif
}
void printState(board *b){
for(int i = 0; i<15; i++){
fprintf(stderr, "%d\40", b->cells[i]->val);
}
fprintf(stderr, "%d", b->cells[15]->val);
for(int i = 0; i<16; i++){
printLabel(b->cells[i], i);
}
fprintf(stderr, "\n");
}
int checkPair(int x, int y)
{
if (x > 0 && x < 5 && y > 0 && y < 5)
{
return 0;
}
red();
printf("Error. Expected coordinates in range 1 to 4.\n");
reset();
return 1;
}
int getIndex(int x, int y)
{
return (x - 1) * 4 + (y - 1);
}
int getIndexByLabel(board *b, char *identifier)
{
for (int i = 0; i < 16; i++)
{
if (findLabel(b->cells[i], identifier))
return i;
}
return -1;
}
void addLabel(cell *c, char *id)
{
node *l = c->label;
while (l->next)
{
l = l->next;
}
l->id = id;
l->next = malloc(sizeof(node));
l->next->next = NULL;
#ifdef DEBUG
// printLabel(c);
#endif
}
/*
void addLabel(var* v, char* id, int idx){
char *idptr = strtok(id, " ");
char *idptr2 = strtok(id, ".");
char *identifier = malloc(strlen(idptr2));
strcpy(identifier, idptr2);
while(v->next){
v = v->next;
}
v->id = identifier;
v->idx = idx;
v->next = malloc(sizeof(var));
v->next->next=NULL;
}
*/
void dropLabels(cell *c)
{
node *first = c->label;
node *l = first->next;
node *next;
if(!l) return;
while (l->next)
{
next = l->next;
free(l->id);
free(l);
l = next;
}
first->id = NULL;
first->next = NULL;
c->label = first;
}
int findLabel(cell *c, char *id)
{
node *l = c->label;
while (l->next)
{
if (strcmp(l->id, id) == 0)
{
return 1;
}
l = l->next;
}
return 0;
}
void printLabel(cell *c, int idx)
{
node *l = c->label;
int flag=0;
if(l->next){
fprintf(stderr, "\40%d,%d%s",(idx/4)+1, idx%4 +1, l->id);
l = l->next;
flag=1;
}
// printf("%d\n", l);
while (l->next)
{
fprintf(stderr, ",%s", l->id);
l = l->next;
}
}
int getValue(cell *c)
{
return c->val;
}
void putValue(cell *c, int val)
{
c->val = val;
}
void move(int dir, int op, board *b, int interpretation)
{
char *directions[4] = {"LEFT", "UP", "RIGHT", "DOWN"};
char *operations[4] = {"ADD", "SUB", "MUL", "DIV"};
int directionConstants[4][4] = {{0, 4, 1, 4},{0, 16, 4, 1},{3, -1, -1, 4},{12, -4, -4, 1}};
int S = directionConstants[dir][0];
int E = directionConstants[dir][1];
int I = directionConstants[dir][2];
int O = directionConstants[dir][3];
int oldState[16];
for(int i=0; i<16; i++){
oldState[i]=b->cells[i]->val;
}
for(int i=0; i<4; i++){
cell *tempArr[4] = {NULL, NULL, NULL, NULL};
int curr=0;
for(int j = S + i*O; j != E + i*O; j+=I){
if(b->cells[j]->val){
tempArr[curr] = b->cells[j];
curr++;
}
}
#ifdef DEBUG
printf("Pre Compression\n");
#endif
if(!interpretation){
for(int index = 0; index < curr-1 ; index++){
if(tempArr[index]->val == tempArr[index+1]->val){
tempArr[index]->val = compute(tempArr[index]->val, op);
tempArr[index+1]->val = 0;
concatLabels(tempArr[index], tempArr[index+1]);
dropLabels(tempArr[index+1]);
}
}
}
else{
for(int index=curr; index<4; index++){
tempArr[index]=malloc(sizeof(cell));
tempArr[index]->label=malloc(sizeof(node));
tempArr[index]->val = 0;
tempArr[index]->label->next = NULL;
}
for(int index = 0; index < curr-1 ; index++){
if(tempArr[index]->val == tempArr[index+1]->val){
tempArr[index]->val = compute(tempArr[index]->val, op);
if(op==1) dropLabels(tempArr[index]);
else concatLabels(tempArr[index], tempArr[index+1]);
dropLabels(tempArr[index+1]);
for(int k=index; k<curr-2; k++){
tempArr[k+1]=tempArr[k+2];
}
tempArr[curr-1]=malloc(sizeof(cell));
tempArr[curr-1]->label=malloc(sizeof(node));
tempArr[curr-1]->val = 0;
tempArr[curr-1]->label->next = NULL;
}
}
}
#ifdef DEBUG
printf("Computation\n");
printf("curr is %d\n", curr);
#endif
cell *compTempArr[4] = {NULL, NULL, NULL, NULL};
if(!interpretation){
int k=0;
for(int j = 0; j<curr; j++){
if(tempArr[j]->val){
compTempArr[k] = tempArr[j];
k++;
}
}
}
#ifdef DEBUG
printf("Post Compression\n");
#endif
if(!interpretation){
int k = 0;
for(int j = S + i*O; j != E + i*O; j+=I){
if(compTempArr[k]){
b->cells[j] = compTempArr[k];
k++;
}
else{
b->cells[j] = malloc(sizeof(cell));
b->cells[j]->label = malloc(sizeof(node));
b->cells[j]->val = 0;
b->cells[j]->label->next = NULL;
}
}
}
else{
int k=0;
for(int j = S + i*O; j != E + i*O; j+=I){
b->cells[j] = tempArr[k];
k++;
}
}
}
int changed = 0;
for(int i=0; i<16; i++){
if(oldState[i]!=b->cells[i]->val){
changed = 1;
}
}
if(!changed){
green();
printf("Performed %s %s. Board hasn't changed.\n", operations[op], directions[dir]);
reset();
}
else{
if(addRandomTile(b)){
green();
printf("Performed %s %s. Board updated. Random tile added.\n", operations[op], directions[dir]);
reset();
}
else {
green();
printf("Performed %s %s. Board is full. No random tile added.\n", operations[op], directions[dir]);
reset();
}
}
fflush(stdout);
}
int addRandomTile(board *b){
int full=1;
for(int i=0; i<16; i++){
if(b->cells[i]->val==0){
full=0;
break;
}
}
if(full){
return 0;
}
while(1){
int num = rand()%16;
int newVal = rand()%2;
if(newVal) newVal=4;
else newVal=2;
if(b->cells[num]->val==0){
assign(b->cells[num], newVal);
return 1;
}
}
}
void concatLabels(cell *a, cell *b){
node *l = b->label;
while(l->next){
addLabel(a, l->id);
l = l->next;
}
}
int compute(int x, int op){
//ADD SUB MUL DIV
if(op==0)
return 2*x;
else if(op==1)
return 0;
else if(op==2)
return x*x;
else if(op==3)
return 1;
return -1;
}
void assign(cell *c, int val)
{
putValue(c, val);
if(val==0) dropLabels(c);
}
void name(cell *c, char *identifier)
{
green();
printf("Setting (%d, %d) to \"%s\"\n", x, y, identifier);
reset();
addLabel(c, identifier);
}
int query(cell *c)
{
return getValue(c);
}
void red()
{
printf("\033[1;31m");
printf("2048> ");
}
void redf()
{
printf("\033[1;31m");
}
void green()
{
printf("\033[1;32m");
printf("2048> ");
}
void greenf()
{
printf("\033[1;32m");
}
void cyan()
{
printf("\033[1;33m");
}
void reset()
{
printf("\033[0m");
}
void prompt()
{
printf("----> ");
}
void printHelp()
{
cyan();
printf("The 2048 Interpreter\n");
printf("Written by Jaskaran Singh Bhatia in C for CSF363.\n\n");
reset();
greenf();
printf("Syntax: Keywords are uppercase. End Commands with a Period \".\"\n");
reset();
printf("\nCommands:\n\n");
greenf();
printf("\t1. MOVES: ");
reset();
redf();
printf("<operation> <direction>");
reset();
printf(".\n\t where operation = {ADD, SUBTRACT, MULTIPLY, DIVIDE}\n\t and direction = {LEFT, RIGHT, UP, DOWN}\n");
redf();
printf("\t Example: \"ADD LEFT.\"\n\n\n");
reset();
greenf();
printf("\t2. ASSIGNMENT: ");
reset();
printf("ASSIGN ");
redf();
printf("<value>");
reset();
printf(" TO ");
redf();
printf("<x>");
reset();
printf(",");
redf();
printf("<y>");
reset();
printf(".\n\t assigns a value (integer) to the cell at (x,y)\n");
redf();
printf("\t Example: \"ASSIGN 7 TO 2,3.\"\n\n\n");
reset();
greenf();
printf("\t3. NAMING: ");
reset();
printf("VAR ");
redf();
printf("<varname>");
reset();
printf(" IS ");
redf();
printf("<x>");
reset();
printf(",");
redf();
printf("<y>");
reset();
printf(".\n\t assigns a name (alphabet string) to the cell at (x,y)\n");
redf();
printf("\t Example: \"VAR block IS 4,4.\"\n\n\n");
reset();
greenf();
printf("\t4. QUERY: ");
reset();
printf("VALUE IN ");
redf();
printf("<x>");
reset();
printf(",");
redf();
printf("<y>");
reset();
printf(".\n\t Prints the value of the tile at (x,y)\n");
redf();
printf("\t Example: \"VALUE IN 1,4.\"\n\n\n");
reset();
}