-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw2.cpp
More file actions
458 lines (378 loc) · 9.92 KB
/
Copy pathhw2.cpp
File metadata and controls
458 lines (378 loc) · 9.92 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
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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <curses.h>
#include <termios.h>
#include <fcntl.h>
#include <iostream>
#define ROW 10
#define COLUMN 50
#define log_river ' '
#define log_log '='
#define log_frog '0'
#define in_game 1
#define game_win 2
#define game_lose 3
#define game_quit 4
pthread_mutex_t gamesstatus;
pthread_mutex_t frogstatus;
pthread_mutex_t mapstatus;
int game_status = in_game;
const int loglen = 15;
int *log_init = new int[ROW];//define the start position of each row.
char map[ROW+10][COLUMN] ;
struct Node{
int x , y;
Node( int _x , int _y ) : x( _x ) , y( _y ) {};
Node(){} ;
} frog ;
bool check_gamestatus();
int kbhit(void);
void *frog_ctl(void *t);
void *update_map(void *t);
void *logs_move( void *t );
int main( int argc, char *argv[] ){
// Initialize the river map and frog's starting position
memset( map , 0, sizeof( map ) ) ;
int i , j ;
for( i = 1; i < ROW; ++i ){
for( j = 0; j < COLUMN - 1; ++j )
map[i%(COLUMN-1)][j%(COLUMN-1)] = ' ' ;
}
for( j = 0; j < COLUMN - 1; ++j )
map[ROW][j%(COLUMN-1)] = map[0][j%(COLUMN-1)] = '|' ;
for( j = 0; j < COLUMN - 1; ++j )
map[0][j%(COLUMN-1)] = map[0][j%(COLUMN-1)] = '|' ;
frog = Node( ROW, (COLUMN-1) / 2 ) ;
map[frog.x%(COLUMN-1)][frog.y%(COLUMN-1)] = '0' ;
//initialize the logs
for (int i = 0; i<ROW; i++){
if(i == 0) continue;
srand(i);
log_init[i] = rand()%(COLUMN-1);
//printf("%d\n", log_init[i]);
//printf("____________row: %d ____________", i);
for (int j = 0; j<loglen; j++){
int tmpj = (log_init[i]+j)%(COLUMN-1);
//std::cout<<tmpj<<" ";
map[i%(COLUMN-1)][tmpj%(COLUMN-1)] = log_log;
}
//std::cout<<std::endl;
}
//Print the map into screen
system("clear");
for(int i = 0; i <= ROW; ++i){
puts(map[i%(COLUMN-1)]);
}
/* Create pthreads for wood move and frog control. */
pthread_mutex_init(&gamesstatus, NULL);
pthread_mutex_init(&frogstatus, NULL);
pthread_mutex_init(&mapstatus, NULL);
pthread_t threads[ROW+2];//9 logs + 1 frog + 1 map + 1 dump
long log_num;
int logthread[ROW];
for (log_num = 1; log_num < ROW; log_num++){
if (log_num == 0){
continue;
}else{
logthread[log_num] = pthread_create(&threads[log_num], NULL, logs_move, (void *)log_num);
if(logthread[log_num] != 0){
printf("ERROR: return code from log pthread_create() is %d\n", logthread[log_num]);
exit(-1);
}
}
}
int frogthread;
frogthread = pthread_create(&threads[ROW], NULL, frog_ctl, NULL);
if (frogthread != 0){
printf("ERROR: return code from frog_control pthread_create() is %d\n", frogthread);
exit(-1);
}
int mapthread;
mapthread = pthread_create(&threads[ROW+1], NULL, update_map, NULL);
if (mapthread != 0){
printf("ERROR: return code from map pthread_create() is %d\n", mapthread);
exit(-1);
}
int return_code;
// pthread_join for sychronizing the program; logs, frog and status.
for (int i = 1; i < ROW+2; i++)
{
if (i == 0 ) continue;
return_code = pthread_join(threads[i], NULL);
if (return_code != 0)
{
printf("ERROR: return code from status pthread_join() is %d\n", return_code);
exit(-1);
}
}
/* Display the output for user: win, lose or quit. */
system("clear");
//printf("\033[H\033[2J");
switch (game_status)
{
case game_win:
printf("You win the game!\n");
break;
case game_lose:
printf("You lose the game!\n");
break;
case game_quit:
printf("You quit the game!\n");
break;
default:
break;
}
pthread_mutex_destroy(&mapstatus);
pthread_mutex_destroy(&frogstatus);
pthread_mutex_destroy(&gamesstatus);
pthread_exit(NULL);
return 0;
}
// Determine a keyboard is hit or not. If yes, return 1. If not, return 0.
int kbhit(void){
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
bool check_gamestatus(){
pthread_mutex_lock(&gamesstatus);
bool tmp = (game_status == in_game);
pthread_mutex_unlock(&gamesstatus);
if (tmp) return true;
else{
return false;
}
}
void *update_map(void *t){
while(check_gamestatus()){
//printf("this is map thread!\n");
pthread_mutex_lock(&frogstatus);
int x = frog.x;
int y = frog.y;
pthread_mutex_unlock(&frogstatus);
pthread_mutex_lock(&mapstatus);
for (int j = 0; j < COLUMN - 1; ++j)
map[ROW%(COLUMN-1)][j%(COLUMN-1)] = map[0][j%(COLUMN-1)] = '|';
for (int j = 0; j < COLUMN - 1; ++j)
map[0][j%(COLUMN-1)] = map[0][j%(COLUMN-1)] = '|';
map[x%(COLUMN-1)][y%(COLUMN-1)] = log_frog;
system("clear");
//printf("\033[H\033[2J");
for( int i = 0; i <= ROW; ++i){
//output the updated map to terminal
puts(map[i%(COLUMN-1)]);
}
pthread_mutex_unlock(&mapstatus);
usleep(100000);
if (game_status != in_game){
break;
}
}
pthread_exit(NULL);
}
void *frog_ctl(void *t){
while(check_gamestatus()){
//printf("this is frog thread!\n");
if(kbhit()){
char sig;
std::cin>> sig;
switch (sig)
{
case 'w'://move up
pthread_mutex_lock(&frogstatus);
frog.x -= 1;
pthread_mutex_unlock(&frogstatus);
break;
case 'W':
pthread_mutex_lock(&frogstatus);
frog.x -= 1;
pthread_mutex_unlock(&frogstatus);
break;
case 'a'://move left
pthread_mutex_lock(&frogstatus);
if (frog.x == ROW && frog.y%(COLUMN -1) == 0 ){
}else{
frog.y -= 1;
}
pthread_mutex_unlock(&frogstatus);
break;
case 'A':
pthread_mutex_lock(&frogstatus);
if (frog.x == ROW && frog.y%(COLUMN -1) == 0 ){
}else{
frog.y -= 1;
}
pthread_mutex_unlock(&frogstatus);
break;
case 'd'://move right
pthread_mutex_lock(&frogstatus);
if (frog.x == ROW && frog.y%(COLUMN -1) == (COLUMN -1) ){
}else{
frog.y += 1;
}
pthread_mutex_unlock(&frogstatus);
break;
case 'D':
pthread_mutex_lock(&frogstatus);
if (frog.x == ROW && frog.y%(COLUMN -1) == (COLUMN -1) ){
}else{
frog.y += 1;
}
pthread_mutex_unlock(&frogstatus);
break;
case 's'://move down
pthread_mutex_lock(&frogstatus);
frog.x += 1;
pthread_mutex_unlock(&frogstatus);
break;
case 'S':
pthread_mutex_lock(&frogstatus);
frog.x += 1;
pthread_mutex_unlock(&frogstatus);
break;
case 'q':
pthread_mutex_lock(&gamesstatus);
game_status = game_quit;
pthread_mutex_unlock(&gamesstatus);
break;
case 'Q':
pthread_mutex_lock(&gamesstatus);
game_status = game_quit;
pthread_mutex_unlock(&gamesstatus);
break;
default:
break;
}
}
pthread_mutex_lock(&frogstatus);
int x = frog.x;
int y = frog.y;
pthread_mutex_unlock(&frogstatus);
pthread_mutex_lock(&mapstatus);
bool in_water = (map[x%(COLUMN-1)][y%(COLUMN-1)] == log_river);
pthread_mutex_unlock(&mapstatus);
if (x < 0 || x > ROW || (x != ROW && y >= (COLUMN -1)) || (x != ROW && y <= 0) || in_water){
pthread_mutex_lock(&gamesstatus);
game_status = game_lose;
pthread_mutex_unlock(&gamesstatus);
}else if (x == 0){
pthread_mutex_lock(&gamesstatus);
game_status = game_win;
pthread_mutex_unlock(&gamesstatus);
}
pthread_mutex_lock(&gamesstatus);
int st = game_status;
pthread_mutex_unlock(&gamesstatus);
if (st != in_game){
break;
}
}
pthread_exit(NULL);
}
void *logs_move( void *t ){
long lognum = (long)t;
int log_num = lognum;
//printf("this is logthrad %d\n", log_num);
int startpos = log_init[log_num];
/* Check game's status */
while(check_gamestatus()){
//printf("this is logthrad %d\n", log_num);
/* Move the logs */
if (log_num%2 == 0){//log not singular moves left
startpos = (startpos -1 + COLUMN-1)%(COLUMN-1);//add COLUMN-1 to avoid negative number
/* system("clear");
printf("startpos:_________ %d\n", startpos);
sleep(5); */
}else{//log singular moves right
startpos = (startpos +1 + COLUMN-1)%(COLUMN-1);
/* system("clear");
printf("startpos:_________ %d\n", startpos);
sleep(5); */
}
//update the frog
pthread_mutex_lock(&frogstatus);
int x = frog.x;
int y = frog.y;
pthread_mutex_unlock(&frogstatus);
//update the frog with log
int endpos = (startpos+loglen)%(COLUMN-1);
pthread_mutex_lock(&mapstatus);
if (x == log_num){
if (startpos <= endpos){
if (y >= startpos && y < endpos){
if (x%2 == 0){
pthread_mutex_lock(&frogstatus);
frog.y -= 1;
pthread_mutex_unlock(&frogstatus);
}else{
pthread_mutex_lock(&frogstatus);
frog.y += 1;
pthread_mutex_unlock(&frogstatus);
}
}
}else{
if (!(y >= endpos && y < startpos)){
if (x%2 == 0){
pthread_mutex_lock(&frogstatus);
frog.y -= 1;
pthread_mutex_unlock(&frogstatus);
}else{
pthread_mutex_lock(&frogstatus);
frog.y += 1;
pthread_mutex_unlock(&frogstatus);
}
}
}
}
//update the logs
if (startpos <= endpos){
for(int i=0; i<startpos; i++){
map[log_num%(COLUMN-1)][i%(COLUMN-1)] = log_river;
}
for(int j=endpos; j<COLUMN;j++){
map[log_num%(COLUMN-1)][j%(COLUMN-1)] = log_river;
}
for(int k=startpos; k<endpos; k++){
map[log_num%(COLUMN-1)][k%(COLUMN-1)] = log_log;
}
}else{
for(int i=0; i<endpos; i++){
map[log_num%(COLUMN-1)][i%(COLUMN-1)] = log_log;
}
for(int j=endpos; j<startpos; j++){
map[log_num%(COLUMN-1)][j%(COLUMN-1)] = log_river;
}
for(int k=startpos; k<COLUMN; k++){
map[log_num%(COLUMN-1)][k%(COLUMN-1)] = log_log;
}
}
pthread_mutex_unlock(&mapstatus);
//system("clear");
usleep(100000);
if (game_status != in_game){
break;
}
}
pthread_exit(NULL);
/* Check keyboard hits, to change frog's position or quit the game. */
/* Print the map on the screen */
}