-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi8x8_client.c
442 lines (375 loc) · 9.63 KB
/
rpi8x8_client.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
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <termio.h>
#include <signal.h>
#include <stdbool.h>
#include <pthread.h>
#define LEFT 1
#define RIGHT 2
#define UP 3
#define DOWN 4
//대문자 wasd
#define LEFT2 65
#define RIGHT2 68
#define UP2 87
#define DOWN2 83
#define BOMB1 32 //space
#define BOMB2 9 //Tab
/* RPI64_CLIENT .C */
int p1x; /* player 1's location */
int p1y;
int p2x; /* player 2's location */
int p2y;
bool bomb1;
bool bomb2;
int b1x; /* Bombs' location */
int b2x;
int b1y;
int b2y;
bool end; /* Game-over flag (Does game over?)*/
//char map[8][8]; /* LED Matrix 2D array */
unsigned short *map;
//unsigned shor map[8][8];
void move(int m);
int getch(void);
void die(int a);
//void key_process(int key);
/* Multi Threading : Bomb function Bomb1-Bomb2 concurrent */
void *firstbombThreadRun(){
if(bomb1 == false){
bomb1 = true;
b1x = p1x;
b1y = p1y;
//map[b1y][b1x] = 'B';
*(map+b1y*8+b1x) = 3; /* BOMB : 3*/
sleep(2);
die(1);
}
}
void *secondbombThreadRun(){
if(bomb2 == false){
bomb2 = true;
b2x = p2x;
b2y = p2y;
//map[b2y][b2x] = 'B';
*(map+b1y*8+b1x) =3;
sleep(2);
die(2);
}
}
/* RPI_CLIENT .C */
int main()
{
/*variables for the Logic */
p1x = 0; p1y = 0;
p2x = 7; p2y = 7;
bomb1 = false;
bomb2 = false;
int key;
int i,j;
map= (*unsigned short)malloc(1024);
/* Initialization */
for(i = 0; i<8; i++)
for(j = 0; j<8; j++)
*(map+i*8+j)=0;
*(map+p1y*8+p1x) = 1;
*(map+p2y*8+p2x) = 2;
pthread_t firstThread, secondThread, firstbombThread, secondbombThread;
int threadErr;
/* variables for Socket Network*/
int sockfd;
int len;
struct sockaddr_in address;
int result;
//char ch = 'A';
char ch;
/////////////////////////////////////////
/* Socket (네트워크) variables */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(9734);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1){
perror("oops: client3");
exit(1);
}
/////////////////////////////////////////
//Main Logic : 게임
while(1){
if(end){
end = false;
sleep(7);
p1x = 0; p1y = 0;
p2x = 7; p2y = 7;
bomb1 = false;
bomb2 = false;
for(i = 0; i<8; i++)
for(j=0; j <8;j++)
*(map+i*8+j) =0;
//memset(map, 0 ,64);
*(map+p1y*8+p1x) = 1; /* P1's Location */
*(map+p2y*8+p2x) = 2; /* P2's Location */
}
printf("input: ");
//scanf("%c\n", &ch);
key = getch(); // 3. 방향키가 입력瑛 때 224 00 이 버퍼에 있다. 224부터 빼준다.
//key_process(key);
if(key== 27|| key ==0) {
key = getch();
key = getch();
switch(key) {
case 68:
printf("1p left\n");
move(LEFT);
break;
case 67:
printf("1p right\n");
move(RIGHT);
break;
case 65:
printf("1p up\n");
move(UP);
break;
case 66:
printf("1p down\n");
move(DOWN);
break;
}
}
else{
if(key == BOMB1){
if(threadErr = pthread_create(&firstbombThread, NULL, firstbombThreadRun, NULL)){
printf("Thread Err = %d", threadErr);
}
}else if(key == BOMB2){
if(threadErr = pthread_create(&secondbombThread, NULL, secondbombThreadRun, NULL)){
printf("Thread Err = %d", threadErr);
}
}else if(key == UP2|| key == 119){
printf("2p up\n");
move(UP2);
}else if(key == LEFT2 || key == 97){
printf("2p left\n");
move(LEFT2);
}else if(key == DOWN2 || key == 115){
printf("2p down\n");
move(DOWN2);
}else if(key == RIGHT2 || key == 100){
printf("2p right\n");
move(RIGHT2);
}
else
printf("NOPE\n");
}
if(end) continue;
for(i = 0; i<8; i++){
for(j = 0; j<8; j++) printf("%u",*(map+i*8+j));
printf("\n");
}
//write(sockfd, &ch, 1);
//read(sockfd, &ch, 1);
/* socket size = char(1)x8x8 */
/* last bit(65): Win-flag (isWin?->false/default:0, True:'1' or '2' or '3' (W/L/D) )*/
write(sockfd, &map, 65);
read(sockfd, &map, 65);
//printf("char from server = %c\n", ch);
if(end){
printf("quit by Win\n");
close(sockfd);
exit(0);
}
else {
printf("not quit \n");
}
}
}
/*
void key_process(int key){
if(key== 27|| key ==0) {
key = getch();
key = getch();
switch(key) {
case 68:
printf("1p left\n");
move(LEFT);
break;
case 67:
printf("1p right\n");
move(RIGHT);
break;
case 65:
printf("1p up\n");
move(UP);
break;
case 66:
printf("1p down\n");
move(DOWN);
break;
}
}
else{
if(key == BOMB1){
if(threadErr = pthread_create(&firstbombThread, NULL, firstbombThreadRun, NULL)){
printf("Thread Err = %d", threadErr);
}
}else if(key == BOMB2){
if(threadErr = pthread_create(&secondbombThread, NULL, secondbombThreadRun, NULL)){
printf("Thread Err = %d", threadErr);
}
}else if(key == UP2|| key == 119){
printf("2p up\n");
move(UP2);
}else if(key == LEFT2 || key == 97){
printf("2p left\n");
move(LEFT2);
}else if(key == DOWN2 || key == 115){
printf("2p down\n");
move(DOWN2);
}else if(key == RIGHT2 || key == 100){
printf("2p right\n");
move(RIGHT2);
}
else
printf("NOPE\n");
}
}
}
*/
void die(int a){
printf("BOMB!!!!!\n");
if(a == 1){
bomb1 = false;
*(map+b1y*8+b1x) = 0; /* 기존위치를 0으로*/
if(((p1x>=b1x-3 && p1x<=b1x+3) && (p1y>=b1y-3 && p1y<=b1y+3))&&((p2x>=b1x-3 && p2x<= b1x+3) && (p2y>=b1y-3 && p2y<=b1y+3))){
printf("DRAW\n");
//map[64] = 3; /*map[64]는 winner 비트 winner bit = 3(Draw) */
end = true;
}else if((p1x>=b1x-3 && p1x<=b1x+3) && (p1y>=b1y-3 && p1y<=b1y+3)){
printf("2P WIN!!!!!!!\n!!!!congraturation!!!!!!\n");
printf("2P WIN!!!!!!!\n!!!!congraturation!!!!!!\n");
printf("2P WIN!!!!!!!\n!!!!congraturation!!!!!!\n");
printf("2P WIN!!!!!!!\n!!!!congraturation!!!!!!\n");
printf("2P WIN!!!!!!!\n!!!!congraturation!!!!!!\n");
//map[64] = 2; /* winner bit = 2(2pwin) */
end = true;
}else if((p2x>=b1x-3 && p2x<= b1x+3) && (p2y>=b1y-3 && p2y<=b1y+3)){
printf("1P WIN!!!!!!\n!!!!!!congraturation!!!!!!\n");
printf("1P WIN!!!!!!\n!!!!!!congraturation!!!!!!\n");
printf("1P WIN!!!!!!\n!!!!!!congraturation!!!!!!\n");
printf("1P WIN!!!!!!!\n!!!!congraturation!!!!!!\n");
//map[64] = 1; /* winner bit = 1(1pwin) */
end = true;
}else{
printf("continue\n");
}
}else{
bomb2 = false;
*(map+b2y*8+b2x) = 0;
if(((p1x>=b2x-3 && p1x<=b2x+3) && (p1y>=b2y-3 && p1y<=b2y+3))&&((p2x>=b2x-3 && p2x<= b2x+3) && (p2y>=b2y-3 && p2y<=b2y+3))){
printf("DRaW\n");
//map[64] = 3; /* winner bit = 3(Draw) */
end = true;
}else if((p1x>=b2x-3 && p1x<=b2x+3) && (p1y>=b2y-3 && p1y<=b2y+3)){
printf("2P WIN\n");
//map[64] = 2; /* winner bit = 2(2PWIN) */
end = true;
}else if((p2x>=b2x-3 && p2x<= b2x+3) && (p2y>=b2y-3 && p2y<=b2y+3)){
printf("1P WIN\n");
//map[64] = 1; /* winner bit = 1(1PWIN) */
end = true;
}else{
printf("continue\n");
}
}
}
void move(int m){
switch(m){
case LEFT:
if(p1x>0){
if(*(map+p1y*8+p1x) != 3) /* 'B' == 3*/
*(map+p1y*8+p1x) = 0;
p1x--;
*(map+p1y*8+p1x) = 1;
}
break;
case RIGHT:
if(p1x<7){
if(*(map+p1y*8+p1x) != 3)
*(map+p1y*8+p1x) = 0;
p1x++;
*(map+p1y*8+p1x) = 1;
}
break;
case UP:
if(p1y>0){
if(*(map+p1y*8+p1x) != 3)
*(map+p1y*8+p1x) = 0;
p1y--;
*(map+p1y*8+p1x) = 1;
}
break;
case DOWN:
if(p1y<7){
if(*(map+p1y*8+p1x) != 3)
*(map+p1y*8+p1x) = 0;
p1y++;
*(map+p1y*8+p1x) = 1;
}
break;
case LEFT2:
if(p2x>0){
if(*(map+p2y*8+p2x) != 3)
*(map+p2y*8+p2x) = 0;
p2x--;
*(map+p2y*8+p2x) = 2;
}
break;
case RIGHT2:
if(p2x<7){
if(*(map+p2y*8+p2x) != 3)
*(map+p2y*8+p2x) = 0;
p2x++;
*(map+p2y*8+p2x) = 2;
}
break;
case UP2:
if(p2y>0){
if(*(map+p2y*8+p2x) != 3)
*(map+p2y*8+p2x) = 0;
p2y--;
*(map+p2y*8+p2x) = 2;
}
break;
case DOWN2:
if(p2y<7){
if(*(map+p2y*8+p2x) != 3)
*(map+p2y*8+p2x) = 0;
p2y++;
*(map+p2y*8+p2x) = 2;
}
break;
}
}
int getch(void){
int ch;
struct termios buf;
struct termios save;
tcgetattr(0, &save);
buf = save;
buf.c_lflag &= ~(ICANON|ECHO);
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
tcsetattr(0, TCSAFLUSH, &buf);
ch = getchar();
tcsetattr(0, TCSAFLUSH, &save);
return ch;
}