-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex2_upd.c
529 lines (468 loc) · 14.5 KB
/
ex2_upd.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
/******************************************
* Student name: Atara Razin
* Student: 327348934
* Course Exercise Group: 8923106
* Exercise name: Exercise 2
******************************************/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#define SIZE 16
unsigned int timeTillAddNewNumber;
int numbers[SIZE];
pid_t pidOfProcess1;
char numberInStringForm[40000];
void makeNewBoard();
void addANewTwo();
void moveLeft();
void moveRight();
void moveUp();
void moveDown();
void consolidateToLeft(int start, int end);
void combineSameNumbersLeft(int start, int end);
void consolidateToRight(int start, int end);
void combineSameNumbersRight(int start, int end);
void consolidateToUp(int start, int end);
void combineSameNumbersUp(int start, int end);
void consolidateToDown(int start, int end);
void combineSameNumbersDown(int start, int end);
void exitElegantly(int sig);
int sendAndWriteResults();
void fromArrayToString();
int didLoose();
int didWin();
void sendWonOrLost(int i);
/***********************************************************************
* function name: main
* input: the pid of the other process the one that prints
* output: returns 0 if it gets to the end
* operation:defines both sigactions and mask. sets the alarm and runs a
* while loop that either moves or makes a new board. checks if it won or
* lost meaning if the game ended.
*************************************************************************/
int main(int argc, char* argv[]){
//for sigalarm
struct sigaction usr_action;
sigset_t block_mask;
sigfillset (&block_mask);
//lets sigint happen
sigdelset(&block_mask,SIGINT);
usr_action.sa_handler = addANewTwo;
usr_action.sa_mask = block_mask;
usr_action.sa_flags = 0;
sigaction (SIGALRM, &usr_action, NULL);
///for sigint
struct sigaction usr_action1;
sigset_t block_mask1;
sigemptyset(&block_mask1);
usr_action1.sa_handler = exitElegantly;
usr_action1.sa_mask = block_mask1;
usr_action1.sa_flags = 0;
sigaction (SIGINT, &usr_action1, NULL);
int loose= 0;
int win = 0;
pidOfProcess1 = atoi(argv[1]);
char inputChar;
makeNewBoard();
while(!loose && !win){
alarm(timeTillAddNewNumber);
//makes getchar work witout enter
system("stty cbreak -echo");
inputChar = getchar();
system("stty cooked echo");
switch(inputChar){
case 'A':
moveLeft();
sendAndWriteResults();
break;
case 'D':
moveRight();
sendAndWriteResults();
break;
case 'X':
moveDown();
sendAndWriteResults();
break;
case 'W':
moveUp();
sendAndWriteResults();
break;
case 'S':
makeNewBoard();
break;
default:
break;
}
if(didLoose() == 1){
loose = 1;
}
if(didWin() == 1){
win =1;
}
}
/*sends thesee negatives instead of numbers to
notify theother that the game ended*/
if(loose == 1){
sendWonOrLost(-2);
}
if(win == 1){
sendWonOrLost(-1);
exit(1);
}
return 0;
}
/***********************************************************************
* function name: makeNewBoard
* input: none
* output: none
* operation:defines all the numbers as 0. then gets the first two and then
* the second. amkes sure theyre not the same numbers. calls function
* to send and write results
*************************************************************************/
void makeNewBoard(){
int firstTwo, secondTwo;
int i;
for(i =0; i<SIZE;i++){
numbers[i] = 0;
}
srand(time(NULL));
firstTwo = rand() % SIZE;
while((secondTwo = rand() %SIZE) == firstTwo);
numbers[firstTwo] = 2;
numbers[secondTwo] = 2;
sendAndWriteResults();
}
/***********************************************************************
* function name: moveLeft
* input: none
* output: none
* operation:algorithm:
* move all to the left as much as they
* can combine all that can to the left spot
* move all to the left as much as they can
*************************************************************************/
void moveLeft() {
int i;
for (i = 0; i < SIZE; i += 4) {
consolidateToLeft(i, i + 3);
combineSameNumbersLeft(i, i + 3);
consolidateToLeft(i, i + 3);
}
}
/***********************************************************************
* function name: consolidateToLeft
* input: int start, int end
* output: none
* operation: consoladates all leftwise
*************************************************************************/
void consolidateToLeft(int start, int end) {
int emptySpace = start;
int i;
for (i = start; i <= end; i++) {
if (numbers[i] != 0) {
numbers[emptySpace] = numbers[i];
emptySpace++;
}
}
for (i = emptySpace; i <= end; i++) {
numbers[i] = 0;
}
}
/***********************************************************************
* function name: combineSameNumbersLeft
* input: int start, int end
* output: none
* operation: if two numbers are the same adds them
*************************************************************************/
void combineSameNumbersLeft(int start, int end) {
int i = start + 1;
while (numbers[i] != 0 && i<=end) {
if (numbers[i] == numbers[i - 1]) {
numbers[i - 1] += numbers[i];
numbers[i] = 0;
}
i++;
}
}
/***********************************************************************
* function name: moveRight
* input: none
* output: none
* operation:algorithm:
* move all to the rigth as much as they
* can combine all that can to the right spot
* move all to the right as much as they can
*************************************************************************/
void moveRight() {
int i;
for (i = 0; i < SIZE; i += 4) {
consolidateToRight(i, i + 3);
combineSameNumbersRight(i, i + 3);
consolidateToRight(i, i + 3);
}
}
/***********************************************************************
* function name: consolidateToRight
* input: int start, int end
* output: none
* operation: consoladates all rightwise
*************************************************************************/
void consolidateToRight(int start, int end) {
int emptySpace = end;
int i;
for (i = end; i >= start; i--) {
if (numbers[i] != 0) {
numbers[emptySpace] = numbers[i];
emptySpace--;
}
}
for (i = emptySpace; i >= start; i--) {
numbers[i] = 0;
}
}
/***********************************************************************
* function name: combineSameNumbersRight
* input: int start, int end
* output: none
* operation: if two numbers are the same adds them
*************************************************************************/
void combineSameNumbersRight(int start, int end) {
int i = end -1;
while (numbers[i] != 0 && i>=start) {
if (numbers[i] == numbers[i + 1]) {
numbers[i + 1] += numbers[i];
numbers[i] = 0;
}
i--;
}
}
/***********************************************************************
* function name: moveUp
* input: none
* output: none
* operation:algorithm:
* move all to the up as much as they
* can combine all that can to the up spot
* move all up as much as they can
*************************************************************************/
void moveUp() {
int i;
for(i = 0; i<4; i++){
consolidateToUp(i,i+12);
combineSameNumbersUp(i,i+12);
consolidateToUp(i,i+12);
}
}
/***********************************************************************
* function name: consolidateToUp
* input: int start, int end
* output: none
* operation: consoladates all upwise
*************************************************************************/
void consolidateToUp(int start, int end) {
int emptySpace = start;
int i;
for (i = start; i <= end; i+=4) {
if (numbers[i] != 0) {
numbers[emptySpace] = numbers[i];
emptySpace+=4;
}
}
for (i = emptySpace; i <= end; i+=4) {
numbers[i] = 0;
}
}
/***********************************************************************
* function name: combineSameNumbersUp
* input: int start, int end
* output: none
* operation: if two numbers are the same adds them
*************************************************************************/
void combineSameNumbersUp(int start, int end) {
int i = start + 4;
while (numbers[i] != 0 && i<=end) {
if (numbers[i] == numbers[i - 4]) {
numbers[i - 4] += numbers[i];
numbers[i] = 0;
}
i+=4;
}
}
/***********************************************************************
* function name: moveDown
* input: none
* output: none
* operation:algorithm:
* move all down as much as they
* can combine all that can down spot
* move all down as much as they can
*************************************************************************/
void moveDown() {
int i;
for(i = 0; i<4; i++){
consolidateToDown(i,i+12);
combineSameNumbersDown(i,i+12);
consolidateToDown(i,i+12);
}
}
/***********************************************************************
* function name: consolidateToDown
* input: int start, int end
* output: none
* operation: consoladates all rightwise
*************************************************************************/
void consolidateToDown(int start, int end) {
int emptySpace = end;
int i;
for (i = end; i >= start; i-=4) {
if (numbers[i] != 0) {
numbers[emptySpace] = numbers[i];
emptySpace-=4;
}
}
for (i = emptySpace; i >= start; i-=4) {
numbers[i] = 0;
}
}
/***********************************************************************
* function name: combineSameNumberDown
* input: int start, int end
* output: none
* operation: if two numbers are the same adds them
*************************************************************************/
void combineSameNumbersDown(int start, int end) {
int i = end - 4;
while (numbers[i] != 0 && i>=start) {
if (numbers[i] == numbers[i + 4]) {
numbers[i + 4] += numbers[i];
numbers[i] = 0;
}
i-=4;
}
}
/***********************************************************************
* function name: exitElegantly
* input: none
* output: none
* operation: exits with 1
*************************************************************************/
void exitElegantly(int sig){
exit(1);
}
/***********************************************************************
* function name: addANewTwo
* input: int sig
* output: none
* operation: checks where there are open slots and amonst them it puts in
* a two. then it makes a new timetilladdnewnumber. calls and send results
*************************************************************************/
void addANewTwo(int sig){
int temp[SIZE]={0};
int numOfOpenSlots=0;
int i;
//checks open slots
for(i =0; i<SIZE;i++){
if(numbers[i] == 0){
temp[numOfOpenSlots] = i;
numOfOpenSlots++;
}
}
srand(time(NULL));
int addATwo = rand() % numOfOpenSlots;
numbers[temp[addATwo]] = 2;
timeTillAddNewNumber = (rand() % 5) + 1;
sendAndWriteResults();
}
/***********************************************************************
* function name: sendAndWriteResults
* input: none
* output: an int
* operation: makes a new waiting time. then writes to stdout. sends with
* kill a sigusr to the other process.
*************************************************************************/
int sendAndWriteResults(){
//make a new waiting time
srand ( time(NULL) );
timeTillAddNewNumber = (rand() % 5) + 1;
//write to STDOUT
fromArrayToString();
write(1,numberInStringForm, strlen(numberInStringForm));
//send signal to process 1
if(kill (pidOfProcess1,SIGUSR1)<0){
perror("error sending write and restuls!\n");
exit(-1);
}
}
/***********************************************************************
* function name: fromArrayToString
* input: none
* output: an int
* operation: converts from an array of ints to string format. with sprintf
*************************************************************************/
void fromArrayToString(){
char string[100000] = {0};
memset(numberInStringForm,0,40000);
sprintf(string,"%d%c",numbers[0],',');
strcpy(numberInStringForm,string);
int i;
for(i =1; i<SIZE-1;i++) {
memset(string,0,40000);
sprintf(string,"%d%c",numbers[i],',');
strcat(numberInStringForm,string);
}
memset(string,0,40000);
sprintf(string,"%d\n",numbers[SIZE -1]);
strcat(numberInStringForm,string);
}
/***********************************************************************
* function name: didLoose
* input: none
* output: an int
* operation: checks if theres an open slot if not we lost.
*************************************************************************/
int didLoose(){
int i;
for(i =0; i<SIZE;i++){
if(numbers[i] == 0){
return 0;
}
}
return 1;
}
/***********************************************************************
* function name: didWin
* input: none
* output: an int (if we won or not)
* operation: checks if theres a 2048 on the board. if yes we won.
*************************************************************************/
int didWin(){
int i;
for(i =0; i<SIZE;i++){
if(numbers[i] == 2048){
return 1;
}
}
return 0;
}
/***********************************************************************
* function name: sendWonOrLost
* input: the number (-1 or -2)
* output: none
* operation: sends the single number with a comma as a message. sends
* with kill
*************************************************************************/
void sendWonOrLost(int i){
char message[12];
sprintf(message,"%i",i);
strcat(message, ",");
write(1,message, strlen(message));
//send signal to process 1
if(kill (pidOfProcess1,SIGUSR1)<0){
perror("error!\n");
exit(-1);
}
exit(1);
}