/
TetrisServer.c
680 lines (618 loc) · 15.9 KB
/
TetrisServer.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
dzt.c
Author:Wang-Kangjin
E-mail:kangjin.wang@gmail.com
Data:Thursday 31 October 2013
Tetris server model
*/
#include <curses.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <signal.h>
#define PANELX 28
#define PANELY 15
#define PANELSTARTX 3
#define PANELSTARTY 2
#define PANELNEXTX 5
#define PANELNEXTY 37
#define SERVER_PORT 8888
#define BUFFERSIZE 420 //TCP发送数据时缓冲区的大小 28*15
#define CLIENTPOSX 3 //客户面板的长度
#define CLIENTPOSY 79 //客户面板的宽度
typedef struct block {
int group_id;
int id;
int pos_x;
int pos_y;
int color;
int data[4][2];
}block;
void initblock(block*);
void init_background(int x,int y);
int init_network();
void nextblock();
void initpanel();
void printpanel(int, int);
void printclientpanel(WINDOW *win,int (*arr)[PANELY]); //net work
void printnextpanel(int ,int);
void start();
void * keylistener(void *arg);
void * server_send(void *arg);
void saveclient(int *);
void puttobuff(int *);
void goahead();
bool canmovedown();
bool canmoveleft();
bool canmoveright();
bool caninput();
bool canrotate();
void rotateleft();
void movedown();
void moveleft();
void moveright();
void movemid();
void nextblock();
void removeline(int);
void mergetotemp();
void savetoprev();
void eliminate();
void copy(block *, block *); //copy next block to current block
static void sig_handler(int);
//wrote by guoruiwen
int all_shape[28][4][2]={
{{0,0},{1,0},{2,0},{3,0}},//0
{{0,0},{0,1},{0,2},{0,3}},//0
{{0,0},{1,0},{2,0},{3,0}},//0
{{0,0},{0,1},{0,2},{0,3}},//0
{{0,0},{1,0},{2,0},{2,1}},//0
{{1,0},{1,1},{1,2},{0,2}},//0
{{0,0},{0,1},{1,1},{2,1}},//00
{{0,0},{0,1},{0,2},{1,0}},//
{{0,1},{1,1},{2,0},{2,1}},// 0
{{0,0},{0,1},{0,2},{1,2}},// 0
{{0,0},{0,1},{1,0},{2,0}},// 00
{{0,0},{1,0},{1,1},{1,2}},//
{{0,0},{0,1},{1,1},{1,2}},//00
{{0,1},{1,0},{1,1},{2,0}},// 00
{{0,0},{0,1},{1,1},{1,2}},//
{{0,1},{1,0},{1,1},{2,0}},//
{{0,1},{0,2},{1,0},{1,1}},// 00
{{0,0},{1,0},{1,1},{2,1}},//00
{{0,1},{0,2},{1,0},{1,1}},//
{{0,0},{1,0},{1,1},{2,1}},//
{{0,0},{0,1},{1,0},{1,1}},//00
{{0,0},{0,1},{1,0},{1,1}},//00
{{0,0},{0,1},{1,0},{1,1}},
{{0,0},{0,1},{1,0},{1,1}},
{{0,1},{1,0},{1,1},{1,2}},// 0
{{0,1},{1,0},{1,1},{2,1}},//000
{{1,0},{1,1},{1,2},{2,1}},//
{{0,1},{1,1},{1,2},{2,1}}
};
int temp_panel[PANELX][PANELY]; //用来存要显示的面板
int prev_panel[PANELX][PANELY]; //存上一次块落下后的现场,和现在正在下落的块左与运算,就得到要显示的样子,存放到temp_panel
int client_panel[PANELX][PANELY]; //显示客户端的样子
int score=0;
block block_current,block_next;
block *b1 = &block_current; //指向当前下落的块
block *b2 = &block_next; //指向下一个要下落的块
bool over=false;
int sleeptime=500000;
int level=0;
int main(void){
start();
}
void start(){
initscr();
srand(time(0));
init_background(0,0);
refresh();
keypad(stdscr,TRUE);
pthread_t keyboard; //键盘监听线程
int err = init_network(); //初始化网络模块
if(err!=0){
mvprintw(5,60,"Network model init failed!");
}
start:
initpanel(); //把两个panel清空
initblock(b1);initblock(b2);
printpanel(PANELSTARTX,PANELSTARTY);
err = pthread_create(&keyboard,NULL,keylistener,NULL);
if(err !=0){
printf("can't create thread keyboard listener!");
exit(1);
}
//main thread make the block moving down until the gameover
while(!over){
movemid();
if(caninput(b1)){ //可以放下当前块说明游戏未结束
while( canmovedown() ){ //直到不能下落位置
//继续下落
goahead();
//显示一次
printpanel(PANELSTARTX,PANELSTARTY);
usleep(sleeptime);
}
//save temp panel to preview panel and create new block
savetoprev();
printpanel(PANELSTARTX,PANELSTARTY);
//停止下落后要消除
eliminate();
//把下一个块替换上来
nextblock();
}
else
over=true;
}
attrset(COLOR_PAIR(7));
mvprintw(21,37,"YOU DEAD!Try again?(y/n):");
int input;
int quit = 1;
while(quit){
input=getch(); //判断用户还要不要玩下去
switch(input){
case 'y':
over = 0;
attrset(COLOR_PAIR(0));
mvprintw(21,37," ");
goto start; //重新开始
break;
case 'n': endwin();quit = 0;break;
default:
mvprintw(21,37,"YOU DEAD!Try again?(y/n):");
}
}
}
void initpanel(){
int i,j;
for(i=0;i<PANELX;i++)
for(j=0;j<PANELY;j++){
temp_panel[i][j]=prev_panel[i][j]=0;
}
}
void initblock(block *b){
int line = rand()%28;
int i ,j;
//calculate the id in the group
b->id=line%4;
b->pos_x = 0;
b->pos_y = 0;
//calculate the groupid
b->group_id = line/4;
b->color = line/4+1;
//init data area
for(i=0;i<4;i++)
for(j=0;j<2;j++)
b->data[i][j]= all_shape[line][i][j];
}
void nextblock(){
copy(b1,b2);
initblock(b2);
}
//copy p_b2 to p_b1;
void copy(block *p_b1,block *p_b2){
p_b1->group_id = p_b2->group_id;
p_b1->id = p_b2->id;
p_b1->pos_x = p_b2->pos_x;
p_b1->pos_y = p_b2->pos_y;
p_b1->color = p_b2->color;
int i ,j;
for(i=0;i<4;i++)
for(j=0;j<2;j++)
p_b1->data[i][j] = p_b2->data[i][j];
}
void * keylistener(void *arg){
noecho();
int key;
while(!over){
key = getch();
if(key == 'q' || key =='Q'){
over = true;
}
switch(key){
case KEY_UP:rotateleft();break;
case KEY_LEFT:moveleft();break;
case KEY_RIGHT:moveright();break;
case KEY_DOWN:movedown();break;
}
printpanel(PANELSTARTX,PANELSTARTY);
}
pthread_exit((void *) 0);
}
//print panel at (x,y)
void printpanel(int x,int y){ //打印当前panel状态(temp_panel)
int i ,j ;
int color;
mergetotemp();
for(i=0;i<PANELX;i++,x++){
move(x,y);
for(j=0;j<PANELY;j++){
color = temp_panel[i][j];
if(color != 0){
attrset(COLOR_PAIR(color));
printw("%s","[]");
}
else{
attrset(COLOR_PAIR(0));
printw("%s"," ");
}
}
}
printnextpanel(PANELNEXTX,PANELNEXTY);
move(LINES-1,COLS-1);
refresh();
}
void printnextpanel(int x,int y){ //显示下一个图形
int arr_next[4][4];
int i,j;
int px=0,py=0;
int color;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
arr_next[i][j]=0;
for(i=0;i<4;i++){
px=b2->data[i][0];
py=b2->data[i][1];
arr_next[px][py]=b2->color;
}
for(i=0;i<4;i++,x++){
move(x,y);
for(j=0;j<4;j++){
color = arr_next[i][j];
if(color !=0)
{
attrset(COLOR_PAIR(color));
printw("%s","[]");
}
else{
attrset(COLOR_PAIR(color));
printw("%s"," ");
}
}
}
}
bool canmovedown(){ //检查有没有可消除的行。如果有就把它remove。
if(over)
return false;
bool res=true;
int i ;
for(i=0;i<4;i++){
int next_x=b1->data[i][0]+1;
int next_y=b1->data[i][1];
if(next_x>=PANELX)
return false;
res = res && (!prev_panel[next_x][next_y]);
if(!res)
return false;
}
return res;
}
bool caninput(block *p_b){
int i ;
for(i=0;i<4;i++){
int prev_x=p_b->data[i][0] ;
int prev_y=p_b->data[i][1] ;
if(prev_panel[prev_x][prev_y] != 0 || prev_y<0 || prev_y>PANELY-1)
return false;
}
return true;
}
void eliminate(){
int count=0;
int i ,j;
for(i=PANELX-1;i>=0;){
bool elim = true;
for(j=0;j<PANELY;j++){
if(prev_panel[i][j]==0)
elim = false;
}
if(elim){
count++;
for(j=0;j<PANELY;j++)
prev_panel[i][j]=0;
removeline(i);
}
else{
i--;
}
}
if(count != 0)
score=score + (1<<count);
if(level!=score/50+1){
level = score/50+1;
sleeptime= sleeptime- level*50000;
}
attrset(COLOR_PAIR(7));
mvprintw(15,37," Your Score ");
attrset(COLOR_PAIR(0));
mvprintw(16,40,"%d",score);
attrset(COLOR_PAIR(7));
mvprintw(18,37," Your Level ");
attrset(COLOR_PAIR(0));
mvprintw(19,40,"%d",level);
}
void removeline(int i){
int j;
if(i == 0){
for(j=0;j<PANELY;j++)
prev_panel[i][j]=0;
return ;
}
else{
for(j=0;j<PANELY;j++)
prev_panel[i][j]=prev_panel[i-1][j];
removeline(i-1);
}
}
void goahead(){ //继续下落
int i;
for(i=0;i<4;i++)
b1->data[i][0]++;
b1->pos_x +=1 ;
}
void mergetotemp(){ //把下落的块和底部静态部分合起来,这样才能显示
int i,j;
for(i=0;i<PANELX;i++)
for(j=0;j<PANELY;j++)
temp_panel[i][j]= prev_panel[i][j];
for(i=0;i<4;i++)
temp_panel[ b1->data[i][0] ][ b1->data[i][1]] = b1->color;
}
void rotateleft(){
if(canrotate()){
b1->id = (b1->id+1)%4;
int i ,j;
int line = b1->group_id*4 + b1->id;
for(i=0;i<4;i++){
b1->data[i][0] = all_shape[line][i][0] + b1->pos_x;
b1->data[i][1] = all_shape[line][i][1] + b1->pos_y;
}
}
}
bool canrotate(){
struct block temp;
struct block *p_temp=&temp;
copy(p_temp,b1); //先新建一个块尝试能不能转
p_temp->id = (p_temp->id +1)%4;
int i ,j;
int line = p_temp->group_id*4 + p_temp->id;
for(i=0;i<4;i++){
p_temp->data[i][0] = all_shape[line][i][0] + b1->pos_x;
p_temp->data[i][1] = all_shape[line][i][1] + b1->pos_y;
}
bool res = caninput(p_temp);
return res;
}
void movedown(){
while( canmovedown() ){
goahead();
}
}
void moveleft(){
int i;
if(canmoveleft()){
b1->pos_y--;
for(i=0;i<4;i++)
b1->data[i][1]--;
}
}
bool canmoveleft(){
int i ;
for(i=0;i<4;i++){
int x = b1->data[i][0];
int y = b1->data[i][1];
if( ((y-1) < 0) || ((prev_panel[x][y-1]) != 0) )
return false;
}
return true;
}
void moveright(){
int i;
if(canmoveright()){
b1->pos_y++;
for(i=0;i<4;i++)
b1->data[i][1]++;
}
}
bool canmoveright(){
int i ;
for(i=0;i<4;i++){
int x = b1->data[i][0];
int y = b1->data[i][1];
if( ((b1->data[i][1]+1) >= PANELY) || (prev_panel[x][y+1] != 0) )
return false;
}
return true;
}
void movemid(){
b1->pos_y=7;
int i;
for(i=0;i<4;i++)
b1->data[i][1] +=7;
}
void savetoprev(){ //保存现场。下一次显示为下面不动的部分
int i ,j;
for(i=0;i<PANELX;i++)
for(j=0;j<PANELY;j++)
prev_panel[i][j] = temp_panel[i][j];
}
void init_background(int x,int y){
int locate_Y_PLUS=y;
int locate_X_PLUS=x;
int brush_x,brush_y;
start_color() ;
init_pair(0,COLOR_WHITE,COLOR_BLACK);
init_pair(1,COLOR_BLACK,COLOR_RED);
init_pair(2,COLOR_BLACK,COLOR_GREEN);
init_pair(3,COLOR_BLACK,COLOR_YELLOW);
init_pair(4,COLOR_BLACK,COLOR_BLUE);
init_pair(5,COLOR_BLACK,COLOR_MAGENTA);
init_pair(6,COLOR_BLACK,COLOR_CYAN);
init_pair(7,COLOR_BLACK,COLOR_WHITE);
for(brush_x=0+locate_X_PLUS;brush_x<=32+locate_X_PLUS;brush_x++)
{
for(brush_y=0+locate_Y_PLUS;brush_y<=110+locate_Y_PLUS;brush_y++)
{
//"NEXT" box for player left
if( ((brush_y==36+locate_Y_PLUS)&&(4+locate_X_PLUS<=brush_x)&&(brush_x<=9+locate_X_PLUS))
||((brush_y==45+locate_Y_PLUS)&&(4+locate_X_PLUS<=brush_x)&&(brush_x<=9+locate_X_PLUS))
||((brush_x==4+locate_X_PLUS)&&(36+locate_Y_PLUS<=brush_y)&&(brush_y<=45+locate_Y_PLUS))
||((brush_x==9+locate_X_PLUS)&&(36+locate_Y_PLUS<=brush_y)&&(brush_y<=45+locate_Y_PLUS))
)
{
attrset(COLOR_PAIR(7));
mvprintw(brush_x,brush_y," ");
}
//"NEXT" box for player right
if( ((brush_y==65+locate_Y_PLUS)&&(4+locate_X_PLUS<=brush_x)&&(brush_x<=9+locate_X_PLUS))
||((brush_y==74+locate_Y_PLUS)&&(4+locate_X_PLUS<=brush_x)&&(brush_x<=9+locate_X_PLUS))
||((brush_x==4+locate_X_PLUS)&&(65+locate_Y_PLUS<=brush_y)&&(brush_y<=74+locate_Y_PLUS))
||((brush_x==9+locate_X_PLUS)&&(65+locate_Y_PLUS<=brush_y)&&(brush_y<=74+locate_Y_PLUS))
)
{
attrset(COLOR_PAIR(7));
mvprintw(brush_x,brush_y," ");
}
//inner frame
if( brush_y==32+locate_Y_PLUS
||brush_y==78+locate_Y_PLUS
||brush_y==1+locate_Y_PLUS
||brush_y==109+locate_Y_PLUS
||brush_x==1+locate_X_PLUS
||brush_x==31+locate_X_PLUS
)
{
attrset(COLOR_PAIR(7));
mvprintw(brush_x,brush_y," ");
}
// outer frame
if( brush_y==0+locate_Y_PLUS
||brush_y==110+locate_Y_PLUS
||brush_x==0+locate_X_PLUS
||brush_x==32+locate_X_PLUS
)
{
attrset(COLOR_PAIR(1));
mvprintw(brush_x,brush_y," ");
}
}
printw("%s","\n");
}/*for*/
//information area
move (4+locate_X_PLUS,48+locate_Y_PLUS);
printw("%s"," <<NEXT CUBE>> ");
move (14+locate_X_PLUS,47+locate_Y_PLUS);
printw("%s"," <<< SCORE >>> ");
attrset(COLOR_PAIR(4));
}
int init_network(){
pthread_t serversend;
pthread_t serverrecv;
int res = pthread_create(&serversend,NULL,server_send,NULL);
sleep(1);
return res;
}
void * server_send(void *arg){
//create a server socket
WINDOW *clinet_window;
clinet_window = newwin(PANELX,2*PANELY,CLIENTPOSX,CLIENTPOSY);
struct sockaddr_in server_addr;
bzero(&server_addr,sizeof(server_addr)); //clear
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
attrset(COLOR_PAIR(7));
int server_socket = socket(AF_INET,SOCK_STREAM , 0);
if(server_socket < 0)
mvwprintw(clinet_window,0,0,"CREATE SOCKET FAIL");
if(bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))) //绑定地址和socket
mvwprintw(clinet_window,0,0,"BIND SOCKET FAIL");
if(listen(server_socket,1)<0) //进入监听状态
mvwprintw(clinet_window,0,0,"LISTEN SOCKET FAIL");
mvwprintw(clinet_window,0,0,"LISTEN COMPLETE");
touchwin(clinet_window);wrefresh(clinet_window);
struct sockaddr_in client_addr;
socklen_t length = sizeof(client_addr);
mvwprintw(clinet_window,0,0,"CONNECTING... ...");
touchwin(clinet_window);wrefresh(clinet_window);
int client_socket = accept(server_socket,(struct sockaddr*)&client_addr,&length); //接收一个请求
mvwprintw(clinet_window,1,0,"CONNECTING COMPLETE");
touchwin(clinet_window);wrefresh(clinet_window);
int buffer[BUFFERSIZE];
signal(SIGPIPE,SIG_IGN);
while(1){
bzero(buffer,BUFFERSIZE);
//mvwprintw(clinet_window,2,0,"Receiving ... ...");
length = recv(client_socket,buffer,sizeof(buffer),0); //接收
if(length == 0){
break;
}
if(length <0)
{
mvwprintw(clinet_window,2,0,"recv error!");
break;
}
saveclient(buffer);
printclientpanel(clinet_window,client_panel);
puttobuff(buffer);
length = send(client_socket,buffer,sizeof(buffer),0); //发送自己当前的现场
if(length < 0)
{
break;
}
usleep(500000);
}
close(client_socket);
mvwprintw(clinet_window,2,0,"He is DEAD,you WIN !!!");
wrefresh(clinet_window);
// sleep(2);
delwin(clinet_window);
pthread_exit((void *)0);
}
void saveclient(int *buff){
int i,j;
int count = 0;
for(i=0;i<PANELX;i++)
for(j=0;j<PANELY;j++,count++)
{
client_panel[i][j] = buff[count];
}
}
void puttobuff(int *buff){
int i,j;
int count=0;
for(i=0;i<PANELX;i++)
for(j=0;j<PANELY;j++,count++){
buff[count]=temp_panel[i][j];
}
}
void printclientpanel(WINDOW *win,int (*arr)[PANELY]){
int i ,j ;
int x=0,y=0;
int color;
for(i=0;i<PANELX;i++,x++){
for(j=0,y=0;j<PANELY;j++,y+=2){
color = arr[i][j];
if(color != 0){
attrset(COLOR_PAIR(color));
mvwprintw(win,x,y,"%s","[]");
}
else{
attrset(COLOR_PAIR(0));
mvwprintw(win,x,y,"%s"," ");
}
}
}
wrefresh(win);
}
static void sig_handler(int sig){
if(sig == SIGPIPE)
{
}
}