📄 xbnclient.c
字号:
y = event_y(event) / LARGO; mitabla[x][y]=BARCO; pmicell(x,y,GREEN); break; } /* BOTON DERECHO */ case ACTION_MENU: case MS_RIGHT: if(!event_is_down(event)) { if( (usuario.firsthit==0) && (usuario.play<2)) { for(x=0;x<10;x++) { for(y=0;y<10;y++) mitabla[x][y]=NOBARCO; } iwtable(temptable); fillboard(temptable,0); fillboard(temptable,1); usuario.firsthit=1; } if( usuario.play >=3 ) { ttyfill("You cant modify the board while playing!\n\r"); break; } if( usuario.play ==2 ) { ttyfill("The board is OK, so you cant modify it\n\r"); break; } x = event_x(event) / ANCHO; y = event_y(event) / LARGO; mitabla[x][y]=NOBARCO; pmicell(x,y,WHITE); break; } default: break; }}/* Tu tabla -eventos */void event_tub( Xv_Window window, Event *event ){ int x; int y; char outbuf[MSGMAXLEN]; char temptable[100]; switch( event_action(event) ) { case ACTION_SELECT: case MS_LEFT: if(!event_is_down(event)) { x = event_x(event) / ANCHO; y = event_y(event) / LARGO; if( (usuario.firsthit==0) && (usuario.play<2)) { for(x=0;x<10;x++) { for(y=0;y<10;y++) mitabla[x][y]=NOBARCO; } iwtable(temptable); fillboard(temptable,0); fillboard(temptable,1); usuario.firsthit=1; } if(usuario.play<3) { ttyfill("You cant HIT now. First start a game\n\r"); break; } if(usuario.play==3) { ttyfill("It is not your turn!\n\r"); break; } usuario.play=3; outbuf[0]=x; outbuf[1]=y; bnwrite(sock,outbuf,BNHIT,0,0,usuario.numjug); bnwrite(sock,outbuf,BNREA,usuario.usrfrom,bnsup,usuario.numjug); } default: break; }}/* * FUNCIONES DE COMUNICACIONES */static Notify_value proceso( ){ struct protocolo proto; char outbuf[MSGMAXLEN]; char outbufi[MSGMAXLEN]; char temptable[100]; /* v0.50 */ int i; FD_SET(sock,&readfds); if(select(FD_SETSIZE,&readfds,NULL,NULL,&timeout)) { /* == MAXLEN modified in 0.50 - Big bug ?*/ if( read(sock,&proto,MAXLEN) == MAXLEN) { switch(proto.bnptip0) { case BNTST: break; case BNWRI: if(proto.bnpmsg[0]==1) { xv_set(panelcit2, PANEL_LABEL_STRING," Start ", NULL); ttyfill("The board is OK. Press START to start the game.\n\r"); usuario.play=2; } else if(proto.bnpmsg[0]==0) { ttyfill("Check board: Bnserver reports an error.\n\r"); usuario.play=1; } 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); sprintf(outbuf,"Connected to bnserver:%s. You are player number:%i.\n\r",proto.bnpmsg,usuario.numjug); ttyfill(outbuf); usuario.play = 1; xv_set(panelcito, PANEL_LABEL_STRING,"Disconnect", NULL); xv_set(panelcit2, PANEL_BUSY,FALSE, NULL); xv_set(panelcit4, PANEL_BUSY,FALSE, NULL); xv_set(panelcit5, /* status */ PANEL_BUSY,FALSE, NULL); bnwrite(sock,outbuf,BNVER,usuario.usrfrom,bnsup,usuario.numjug); break; case BNVER: sprintf(outbuf,"bnserver version:%i.%i\n\r",proto.bnpmsg[0],proto.bnpmsg[1]); ttyfill(outbuf); break; case BNMSG: strcpy(outbuf,proto.bnpmsg); strcat(outbuf,"\n\r"); ttyfill(outbuf); break; case BNALL: ttyfill("bnserver is full. Using all the connections.Try later\n\r"); close(sock); break; case BNWAI: ttyfill("You cant start a game while people are playing.Try later\n\r"); 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) { iwtable(temptable); fillboard(temptable,0); /* v0.50 */ } } bnwrite(sock,outbuf,BNREA,usuario.usrfrom,bnsup,usuario.numjug); break; case BNREA: if(usuario.play>=3) { fillboard(proto.bnpmsg,1); /* v0.50 */ inteliclient(proto.bnpmsg); } break; case BNSTR: foot_right("Please,wait your turn"); ttyfill("The game starts now! Please,wait your turn.\n\r"); /* new on v0.32 */ ttyfill("At the bottom-right corner you can see if its your turn.\n\r"); 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]); xv_set(mi[i],MENU_STRING,usuario.names[i],NULL); } } strcat(outbuf,".\n\r"); ttyfill(outbuf); strcpy(outbuf,"Your board:"); strcat(outbuf,usuario.names[usuario.numjug-1]); dname(outbuf,0); foot_left(usuario.names[usuario.numjug-1]); /* new on v0.36 */ if(usuario.play==2) { usuario.play=3; xv_set(panelcit2, PANEL_BUSY,TRUE, NULL); xv_set(panelcit3, PANEL_BUSY,FALSE, NULL); } for(i=0;i<MAXPLAYER;i++) { /* new on v0.36 */ if(usuario.numjug-1!=i) { if(strcmp(usuario.names[i],"")!=0) { usuario.usrfrom=i+1; sprintf(outbuf,"%s's board",usuario.names[i]); dname(outbuf,1); } } } break; case BNCON: sprintf(outbuf,"Player %i is ready to play\n\r",proto.bnptip1); ttyfill(outbuf); break; case BNWHO: /* new on v0.32 */ sprintf(outbuf,"It's %s turn.",usuario.names[proto.bnptip1-1]); foot_right(outbuf); break; case BNTRN: usuario.play=4; /* oh, it is my turn */ foot_right("It's my turn."); break; case BNEXT: /* new on v0.36 */ i=proto.bnptip1-1; usuario.names[i][0]=0; xv_set(mi[i],MENU_STRING,usuario.names[i]); strcpy(outbuf,proto.bnpmsg); strcat(outbuf,"\n\r"); ttyfill(outbuf); for(i=0;i<MAXPLAYER;i++) { /* new on v0.36 */ if(usuario.numjug-1!=i) { if(strcmp(usuario.names[i],"")!=0) { usuario.usrfrom=i+1; sprintf(outbuf,"%s's board",usuario.names[i]); } } } if(usuario.play>=3) { bnwrite(sock,outbuf,BNREA,usuario.usrfrom,bnsup,usuario.numjug); } break; case BNLOS: usuario.play=1; /* oh, i lost the game */ for(i=0;i<MAXPLAYER;i++) usuario.names[i][0]=0; ttyfill("You were killed. To start again wait until the game is over.\n\r"); fillboard(looseboard,1); xv_set(panelcit2, PANEL_LABEL_STRING,"Send Board", PANEL_BUSY,FALSE, NULL); xv_set(panelcit3, PANEL_BUSY,TRUE, NULL); usuario.firsthit=0; /* new on v0.41 */ strcpy(outbuf,"My board"); dname(outbuf,0); strcpy(outbuf,"Enemy's board"); dname(outbuf,1); strcpy(outbuf,"Batalla Naval"); foot_right(outbuf); strcpy(outbuf,"Game Over"); foot_left(outbuf); break; case BNWIN: usuario.play=1; /* oh, i lost the game */ for(i=0;i<MAXPLAYER;i++) usuario.names[i][0]=0; ttyfill("CONGRATULATIONS: You are the winner.\n\r"); fillboard(winboard,1); xv_set(panelcit2, PANEL_LABEL_STRING,"Send Board", PANEL_BUSY,FALSE, NULL); xv_set(panelcit3, PANEL_BUSY,TRUE, NULL); usuario.firsthit=0; /* new on v0.41 */ strcpy(outbuf,"My board"); dname(outbuf,0); strcpy(outbuf,"Enemy's board"); dname(outbuf,1); strcpy(outbuf,"Batalla Naval"); foot_right(outbuf); strcpy(outbuf,"Game Over"); foot_left(outbuf); break; case BNSOL: ttyfill("You are the only one who sent the board.Wait another player.\n\r"); break; case BNSTS: /* new on v0.43 */ for(i=0;i<MAXPLAYER;i++) { sprintf(outbuf,"Player %i (%s), name='%s'\n\r" ,i+1,usuario.status[(int)proto.bnpmsg[i]],&proto.bnpmsg[5+i*MAXNAMELEN]); ttyfill(outbuf); } break; default: sprintf(outbuf,"BNSERVER sending code %X. Not available in this xbnclient.\n\r",proto.bnptip0); ttyfill(outbuf); break; } } } return NOTIFY_DONE; /* new on v0.30 */}int init_cliente( void ) { char outbuf[MSGMAXLEN]; int i; if(usuario.play==0) { /* estado de un boton */ /* Create socket */ sock=socket(AF_INET,SOCK_STREAM,0); if(sock <0) { ttyfill("ERROR: creating stream socket\n\r"); return(1); } host_info = gethostbyname(usuario.server_name); if(host_info==NULL) { close(sock); /* new on v0.38 */ ttyfill("ERROR: Unknown host.Please press 'Config'\n\r"); return(2); } server.sin_family=host_info->h_addrtype; memcpy( (char*) &server.sin_addr, host_info->h_addr,host_info->h_length); server.sin_port=htons(usuario.port); if(connect(sock,(struct sockaddr *)&server,sizeof(server))< 0) { close(sock); /* new on v0.38 */ ttyfill("ERROR:Cant establish connection with server .Press 'Config'.\n\r"); return(2); } timer.it_value.tv_usec = RAPID * 1000; timer.it_interval.tv_usec = RAPID * 1000; notify_set_itimer_func(frame, proceso,ITIMER_REAL, &timer, NULL); strncpy(outbuf,usuario.nombre,MAXNAMELEN); bnwrite(sock,outbuf,BNJUG,XCLI,BN_XVERH,BN_XVERL); /* WHO AM I */ return(0); } else if(usuario.play!=0) { foot_right("(c) Ricardo Quesada"); foot_left("Batalla Naval"); xv_set(panelcito, PANEL_LABEL_STRING," Connect ", NULL); xv_set(panelcit2, PANEL_LABEL_STRING,"Send Board", PANEL_BUSY,TRUE, NULL); xv_set(panelcit3, PANEL_BUSY,TRUE, NULL); xv_set(panelcit4, PANEL_BUSY,TRUE, NULL); xv_set(panelcit5, PANEL_BUSY,TRUE, NULL); usuario.firsthit=0; /* new on v0.41 */ for(i=0;i<MAXPLAYER;i++) usuario.names[i][0]=0; usuario.play=0; bnwrite(sock,outbuf,BNEXT,0,0,usuario.numjug); close(sock); } return(0);}/* * FUNCIONES X y otras... *//* void nada( void ) - no hace nada */void menuses( Menu menu,Menu_item menu_item) { char outbuf[MSGMAXLEN]; int i; for(i=0;i<MAXPLAYER;i++) { if(!strcmp((char *)xv_get(menu_item,MENU_STRING),usuario.names[i])) { if(strcmp(usuario.names[i],"")!=0) { sprintf(fijo01,"%s's board",usuario.names[i]); usuario.usrfrom=i+1; dname(fijo01,1); } } } if(usuario.play>=3) { bnwrite(sock,outbuf,BNREA,usuario.usrfrom,bnsup,usuario.numjug); }}void about( Frame item, Event *event ) { xv_set( subframe1, XV_SHOW, TRUE, NULL );}void send_msg( Frame item, Event *event ) { xv_set( subframe3, XV_SHOW, TRUE, NULL);}void status() { char tempbuf[100]; if(usuario.play>=1) { bnwrite(sock,tempbuf,BNSTS,0,0,usuario.numjug); }}void about_proc( Canvas c, Xv_window pw, Display *display, Window xid, Xv_xrectlist *xrects ) { int w,h; w = xv_get(pw,XV_WIDTH); h = xv_get(pw,XV_HEIGHT); XSetForeground(display,gc,colors[LBLUE]); XFillRectangle(display, xid, gc, 0, 0, w, h/3 ); XSetForeground(display,gc,colors[WHITE]); XFillRectangle(display, xid, gc, 0, h/3, w, h/3*2 ); XSetForeground(display,gc,colors[LBLUE]); XFillRectangle(display, xid, gc, 0, h/3*2, w,h ); XSetForeground(display,gc,colors[YELLOW]); XFillArc(display, xid, gc, w/3 + w/3 * .15, h/3 + h/3 *.15, h/3 * .7, h/3 * .7, 0, 360*64 ); XSetForeground(display,gc,colors[BLACK]); XDrawString(display,xid,gc,90, 40,"Batalla Naval v"BN_XVER,19); XDrawString(display,xid,gc,126, 80,"X client",8); XDrawString(display,xid,gc,30,100 ,"by Ricardo Quesada (rquesada@dc.uba.ar)",39); XDrawString(display,xid,gc,124,140,"bnserver",8); XDrawString(display,xid,gc,90,160,"by Ricardo Quesada",18); XDrawString(display,xid,gc,107,204,"ncurses client",14); XDrawString(display,xid,gc,90,224,"by Ricardo Quesada",18); XDrawString(display,xid,gc,50,260,"Hecho en Buenos Aires, Argentina",32);}void info( Frame item, Event *event ) { xv_set( subframe2, XV_SHOW, TRUE, NULL );}int server_port( Panel_item item, Event *event ) { usuario.port=(int)xv_get(item,PANEL_VALUE); return PANEL_NEXT;}int server_name( Panel_item item, Event *event ) { strcpy(usuario.server_name,(char *)xv_get(item,PANEL_VALUE)); return PANEL_NEXT;}int your_name( Panel_item item, Event *event ) { strncpy(usuario.nombre,(char *)xv_get(item,PANEL_VALUE),MAXNAMELEN); return PANEL_NEXT;}int localecho( Panel_item item,unsigned value, Event *event ) { usuario.lecho=value; return XV_OK;}int to_player( Panel_item item, Event *event ) { usuario.towho=(int)xv_get(item,PANEL_VALUE); return PANEL_NEXT;}int to_message( Panel_item item, Event *event ) { strcpy(usuario.tomsg,(char *)xv_get(item,PANEL_VALUE)); return PANEL_NEXT;}void real_message( void ) { /* new on v0.34 - this send the message */ if(usuario.play>=1) bnwrite(sock,usuario.tomsg,BNMSG,usuario.towho,usuario.lecho,usuario.numjug); else ttyfill("First establish a connection with 'Connect'.\n\r"); return;}/* 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 ) return TRUE; if( mitabla[x][y] == 0 ) return FALSE; ttyfill("ABNORMAL ERROR in haybarco(): Please contact the author.\n\r"); return FALSE;}/* return TRUE if board is OK .else return FALSE */int algoritmo( void ) { int i,xx,yy,barcos,subarco; char temporal[100]; 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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -