📄 nbnclient.c
字号:
}/* ALGORITMO * CHEQUEAR SI LOS BARCOS ESTAN BIEN *//* points to next pos.return TRUE . FALSE in case there are no more ships *//* this rutine uses global x,y */int nextbarco() { if(x==9) { x=0; y++; } else x++; if( y==10 ) return FALSE; else return TRUE;} /* return TRUE if in (x,y) hay barco. else FALSE *//* this rutine uses local x,y */int haybarco( int x, int y) { if( (x < 0) || ( x > 9 ) || (y < 0) || (y >9 ) ) return FALSE; if( mitabla[x][y]>=1 ) /* new on v0.45 */ return TRUE; if( mitabla[x][y] == 0 ) return FALSE; return TRUE;}/* return TRUE if board is OK .else return FALSE */int algoritmo( void ) { int i,xx,yy,barcos,subarco; for(i=0;i<4;i++) barquitos[i]=0; /* global x,y */ x=0; y=0; barcos=0; for(;;) { if(haybarco(x,y)==FALSE) { if(nextbarco()==FALSE) { if( (barquitos[3]==1) && (barquitos[2]==2) && (barquitos[1]==3) && (barquitos[0]==4) ) { return TRUE; /* tabla bien puesta */ } else { wprintw(msgboard,"\nCheck board: All numbers must be equal to 0. [4]=%i,[3]=%i,[2]=%i,[1]=%i",1-barquitos[3],2-barquitos[2],3-barquitos[1],4-barquitos[0]); return FALSE; } } } else { if( haybarco(x,y)==TRUE ) { if( (haybarco(x,y-1)==FALSE) && (haybarco(x-1,y)==FALSE) ) { /* this is the first time i check this */ subarco=1; barcos++; xx=x;yy=y; for(;;) { if( (haybarco(x-1,y+1)==TRUE) || (haybarco(x+1,y+1)==TRUE) ) { wprintw(msgboard,"\nCheck board: Collision in (x,y)=%i,%c",x,'A'+y); pmicell(x,y,RED); return FALSE; } if( (haybarco(x+1,y)==FALSE) && (haybarco(x,y+1)==FALSE) ) { x=xx;y=yy; /* restauro pos */ barquitos[subarco-1]++; /* update cantidad de tama;os de barcos */ break; } else if( haybarco(x+1,y) == TRUE ) { subarco++; barcos++; x++; } else if( haybarco(x,y+1) == TRUE ) { y++; barcos++; subarco++; } if( subarco > 4) { wprintw(msgboard,"\nCheck board: Ship longer than 4 units in (x,y)=%i,%c",x,'A'+y); pmicell(x,y,RED); return FALSE; } } /* for(;;) */ } /* if(haybarco(x,y-1)==FALSE) */ } /* if(haybarco(x,y)==TRUE) */ if(nextbarco()==FALSE) { if( (barquitos[3]==1) && (barquitos[2]==2) && (barquitos[1]==3) && (barquitos[0]==4) ) { return TRUE; /* tabla bien puesta */ } else { wprintw(msgboard,"\nCheck board: All numbers must be equal to 0. [4]=%i,[3]=%i,[2]=%i,[1]=%i",1-barquitos[3],2-barquitos[2],3-barquitos[1],4-barquitos[0]); return FALSE; } } /* if nextbarco()==FALSE) */ } /* else */ } /* for(;;) */} /* void algoritomo() */void aboutw( void ) { touchwin(about); wrefresh(about); touchwin(msgboard); globaltab();}void configw( void ) { char tempi[8]; touchwin(config); mvwprintw(config,1,10," "); mvwprintw(config,2,10," "); mvwprintw(config,3,10," "); mvwprintw(config,1,10,"%s",usuario.server_name); mvwprintw(config,2,10,"%i",usuario.port); mvwprintw(config,3,10,"%s",usuario.nombre); strcpy(ncur.confc[0],usuario.server_name); strcpy(ncur.confc[2],usuario.nombre); sprintf(tempi,"%i",usuario.port); strcpy(ncur.confc[1],tempi); wmove(config,ncur.confpos+1,ncur.conf[ncur.confpos]+10); wrefresh(config); touchwin(msgboard);}void sendmsgw( void ) { int i; touchwin(sendmesg); for(i=0;i<72;i++) mvwaddch(sendmesg,1,8+i,' '); ncur.sendpos=0; ncur.sendx=0; ncur.sendmsg[0]=0; wmove(sendmesg,1,8+ncur.sendx); wrefresh(sendmesg); touchwin(msgboard);}void rconnect(void) { if(ncur.lugarc==0) wattroncm(bconnect,COLOR_PAIR(VGOOD)); else wattroncm(bconnect,COLOR_PAIR(GOOD)); switch(usuario.play) { case 0: mvwprintw(bconnect,0,0," connEct "); break; default: mvwprintw(bconnect,0,0,"disconnEct"); break; } wrefresh(bconnect);}void rstart(void) { if(ncur.lugarc==1) wattroncm(bstart,COLOR_PAIR(VGOOD)); else wattroncm(bstart,COLOR_PAIR(GOOD)); switch(usuario.play) { case 0: case 1: mvwprintw(bstart,0,0, "Send board"); break; case 2: case 3: case 4: default: mvwprintw(bstart,0,0, " Start "); break; } wrefresh(bstart);}void rstatus(void) { if(ncur.lugarc==2) wattroncm(bstatus,COLOR_PAIR(VGOOD)); else wattroncm(bstatus,COLOR_PAIR(GOOD)); mvwprintw(bstatus,0,0, " statUs "); wrefresh(bstatus);}void rmsg(void) { if(ncur.lugarc==3) wattroncm(bmsg,COLOR_PAIR(VGOOD)); else wattroncm(bmsg,COLOR_PAIR(GOOD)); mvwprintw(bmsg,0,0, " send Msg "); wrefresh(bmsg);}void rconfig(void) { if(ncur.lugarc==4) wattroncm(bconfig,COLOR_PAIR(VGOOD)); else wattroncm(bconfig,COLOR_PAIR(GOOD)); mvwprintw(bconfig,0,0, " Config "); wrefresh(bconfig);}void rabout(void) { if(ncur.lugarc==5) wattroncm(babout,COLOR_PAIR(VGOOD)); else wattroncm(babout,COLOR_PAIR(GOOD)); mvwprintw(babout,0,0, " abOut "); wrefresh(babout);}void rquit(void) { if(ncur.lugarc==6) wattroncm(bquit,COLOR_PAIR(VGOOD)); else wattroncm(bquit,COLOR_PAIR(GOOD)); mvwprintw(bquit,0,0, " Quit "); wrefresh(bquit);}void init_screen(void){ int i,x,y; if(ncur.color==TRUE) { init_pair(NORMAL,COLOR_WHITE, COLOR_BLACK); /* definicion de atributos */ init_pair(VGOOD, COLOR_BLUE, COLOR_CYAN); init_pair(GOOD, COLOR_CYAN, COLOR_BLUE); init_pair(WHITE, COLOR_WHITE, COLOR_BLUE); init_pair(GREEN, COLOR_GREEN, COLOR_WHITE); init_pair(RED, COLOR_RED, COLOR_WHITE); init_pair(BLACK, COLOR_BLACK, COLOR_WHITE); init_pair(BLUE, COLOR_WHITE, COLOR_BLUE); init_pair(BLUCKY,COLOR_BLACK, COLOR_CYAN); init_pair(LUCKY, COLOR_CYAN, COLOR_BLACK); } myname=newwin(1,24,5,5); miboard=newwin(12,24,6,5); /* creacion de ventanas */ yourname=newwin(1,24,5,54); tuboard=newwin(12,24,6,54); title=newwin(4,80,0,0); bconnect=newwin(1,10,5,35); bstart=newwin(1,10,7,35); bstatus=newwin(1,10,9,35); bmsg=newwin(1,10,11,35); bconfig=newwin(1,10,13,35); babout=newwin(1,10,15,35); bquit=newwin(1,10,17,35); wright=newwin(1,40,23,40); wleft=newwin(1,40,23,0); msgboard=newwin(5,0,18,0); about=newwin(5,80,18,0); sendmesg=newwin(3,80,20,0); config=newwin(4,80,19,0); /* todo -1 */ wattroncm(myname,COLOR_PAIR(BLUCKY)); wattroncm(yourname,COLOR_PAIR(BLUCKY)); wattroncm(msgboard,COLOR_PAIR(LUCKY)); wattroncm(title,COLOR_PAIR(VGOOD)); wattroncm(wleft,COLOR_PAIR(BLUCKY)); wattroncm(wright,COLOR_PAIR(BLUCKY)); scrollok(msgboard, TRUE); /* scrollok(myname, FALSE); scrollok(yourname, FALSE); */ scrollok(wleft, FALSE); /* can these help me ?*/ scrollok(wright, FALSE); wattroncm(tuboard,COLOR_PAIR(VGOOD)); wattroncm(miboard,COLOR_PAIR(VGOOD)); mvwprintw(miboard,0,0," 0 1 2 3 4 5 6 7 8 9 "); mvwprintw(tuboard,0,0," 0 1 2 3 4 5 6 7 8 9 "); for(y=1;y<11;y++) { wattroncm(tuboard,COLOR_PAIR(VGOOD)); wattroncm(miboard,COLOR_PAIR(VGOOD)); mvwaddch(miboard,y,0,'A'+y-1); mvwaddch(tuboard,y,0,'A'+y-1); wattroncm(tuboard,COLOR_PAIR(GOOD)); wattroncm(miboard,COLOR_PAIR(GOOD)); for(x=1;x<21;) { mvwaddch(miboard,y,x,' '); mvwaddch(tuboard,y,x,' '); x++; mvwaddch(miboard,y,x,ACS_VLINE); mvwaddch(tuboard,y,x,ACS_VLINE); x++; } } for(i=0;i<80*5;i++) waddch(msgboard,' '); wmove(msgboard,0,0); wprintw(msgboard,"\nBatalla Naval ncurses client v"BN_NVER". by Ricardo Quesada (c) 1996,97,98"); wprintw(msgboard,"\nUse TAB,CURSOR LEFT & CURSOR RIGHT to change between windows"); for(i=0;i<80*4;i++) waddch(title,' '); mvwprintw(title,1,25,"Batalla Naval ncurses client v"BN_NVER); mvwprintw(title,2,26,"by Ricardo Quesada (c)1996,97,98"); strcpy(ncur.minombre,"Your board"); mname(); strcpy(ncur.tunombre,"Enemy board"); yname(); foot_left("Batalla Naval"); foot_right("(c) 1995,96,97,98 by Riq"); rconnect(); rstart(); rstatus(); rmsg(); rconfig(); rabout(); rquit(); wattroncm(about,COLOR_PAIR(VGOOD)); mvwprintw(about,0,0," Batalla Naval (c) 1995,96,97,98 by Ricardo Quesada (rquesada@dc.uba.ar) "); wattroncm(about,COLOR_PAIR(GOOD)); mvwprintw(about,1,0,"Dedicado a: Sebastian Cativa Tolosa que algun dia me dijo que iba a hacer este "); mvwprintw(about,2,0," cliente. Espero que la proxima termines lo que empezas :) "); mvwprintw(about,3,0,"Agradezco a: Horacio Pe馻 (horape@century.com.ar) por varios bugs resueltos "); mvwprintw(about,4,0," y por su cliente Win16. - "); wattroncm(sendmesg,COLOR_PAIR(VGOOD)); mvwprintw(sendmesg,0,0," Send Message v0.03 "); wattroncm(sendmesg,COLOR_PAIR(GOOD)); mvwprintw(sendmesg,1,0,"Message: "); mvwprintw(sendmesg,2,0,"To player number (0=broadcast):0 Local Echo:[ ] "); wattroncm(config,COLOR_PAIR(VGOOD)); mvwprintw(config,0,0," The Config v0.01 "); wattroncm(config,COLOR_PAIR(GOOD)); mvwprintw(config,1,0,"Host name: "); mvwprintw(config,2,0,"Host port: "); mvwprintw(config,3,0,"Your name: "); fillenemyt(Bboard,0); fillenemyt(Nboard,1); wrefresh(miboard); wrefresh(tuboard); wrefresh(msgboard); wrefresh(title); wrefresh(bconnect); wrefresh(bstatus); wrefresh(bmsg); wrefresh(bstart); wrefresh(bconfig); wrefresh(babout); wrefresh(bquit);}/* * Proceso() */void proceso( void ) { struct protocolo proto; char outbuf[MSGMAXLEN]; char outbufi[MSGMAXLEN]; int i; FD_SET(sock,&readfds); if(select(FD_SETSIZE,&readfds,NULL,NULL,&tout)) { if( read(sock,&proto,MAXLEN) >0) { ncur.flag1=1; switch(proto.bnptip0) { case BNTST: break; case BNWRI: if(proto.bnpmsg[0]==1) { usuario.play=2; rstart(); wprintw(msgboard,"\nThe board is OK. Press START to start the game."); } else if(proto.bnpmsg[0]==0) { usuario.play=1; wprintw(msgboard,"\nCheck board: bnserver reports an error."); } wrefresh(msgboard); break; case BNJUG: usuario.numjug=proto.bnptip1; sprintf(outbuf,"Connected to bnserver:%s",proto.bnpmsg); foot_right(outbuf); sprintf(outbuf,"Player %i",usuario.numjug); foot_left(outbuf); wprintw(msgboard,"\nConnected to bnserver:%s. You are player number:%i.",proto.bnpmsg,usuario.numjug); wrefresh(msgboard); usuario.play = 1; rconnect(); strcpy(ncur.tunombre,"Enemy board"); yname(); bnwrite(sock,outbuf,BNVER,usuario.usrfrom,bnsup,usuario.numjug); break; case BNVER: wprintw(msgboard,"\nbnserver version:%i.%i",proto.bnpmsg[0],proto.bnpmsg[1]); wrefresh(msgboard); break; case BNMSG: wprintw(msgboard,"\n%s",proto.bnpmsg); wrefresh(msgboard); break; case BNALL: wprintw(msgboard,"\nbnserver is full. Using all the connections.Try later"); wrefresh(msgboard); close(sock); break; case BNWAI: wprintw(msgboard,"\nYou cant start a game while people are playing.Try later"); wrefresh(msgboard); break; case BNHIT: if(usuario.play>=3) { mitabla[(int)proto.bnpmsg[0]][(int)proto.bnpmsg[1]]=(int)proto.bnpmsg[2]; /* new on v0.44 */ if(usuario.hide==FALSE) repaintmi(); } bnwrite(sock,outbuf,BNREA,usuario.usrfrom,bnsup,usuario.numjug); break; case BNREA: if(usuario.play>=3) { repainttu(proto.bnpmsg); inteliclient(proto.bnpmsg); } break; case BNSTR: foot_right("Please,wait your turn"); wprintw(msgboard,"\nUse PAGE UP or 'p' & PAGE DOWN or 'n' to switch between enemy boards"); wprintw(msgboard,"\nAt the bottom-right corner you can see if its your turn."); sprintf(outbuf,"Available players:"); for(i=0;i<MAXPLAYER;i++) { if(proto.bnpmsg[i]>=2) { sprintf(outbufi," %i",i+1); strcat(outbuf,outbufi); strcpy(usuario.names[i],&proto.bnpmsg[5+i*MAXNAMELEN]); } } wprintw(msgboard,"\n%s",outbuf); wrefresh(msgboard); foot_left(usuario.names[usuario.numjug-1]); if(usuario.play==2) { usuario.play=3; } for(i=0;i<MAXPLAYER;i++) { if(usuario.numjug-1!=i) { if(strcmp(usuario.names[i],"")!=0) { usuario.usrfrom=i+1; strcpy(ncur.tunombre,usuario.names[i]); /* new on v0.41 */ yname(); } } } break; case BNSTS: /* new on v0.43 */ for(i=0;i<MAXPLAYER;i++) { wprintw(msgboard,"\nPlayer %i (%s), name='%s'",i+1,usuario.status[(int)proto.bnpmsg[i]],&proto.bnpmsg[5+i*MAXNAMELEN]); } wrefresh(msgboard); break; case BNCON:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -