Advertisement
Geralt1001

Untitled

Feb 6th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <time.h>
  10. #include <arpa/inet.h>
  11. #include <sys/wait.h>
  12. #include <signal.h>
  13. #include <fcntl.h>
  14. #include <sys/poll.h>
  15.  
  16. #define MYPORT 6666
  17.  
  18. #define BACKLOG 5
  19.  
  20. //int size_lab=10;
  21. //int xMax = 24;
  22. //int yMax = 24;
  23. //int x,y,ret;
  24. //int minions[5][2];
  25. //char true_labyrinth [24][24];         //z racji tego, ze operuje na zmiennych globalnych, chcac zwiekszyc rozmiar labiryntu nie wystarczy zwiekszyc size_lab,
  26. //char invisible_lab[24][24];           //tylko trzeba recznie tutaj podac rozmiar tablic
  27. //int steps=xMax*2;
  28.  
  29.  
  30. void deadChildHandler(int s);
  31. void showTrueLabyrinth(char** lab,int n);
  32. void showInvisibleDungeon(char** lab, int n);
  33. int generatePosition(int n);
  34. void move(int sockfd,char** true_labyrinth,int minions[][2],char c,int number,int steps,int size);
  35. char** generateInvisibleLabyrinth(int n);
  36. char** createLab(int n,int clients);
  37.  
  38. int main(int argc,char* argv[])
  39. {
  40.    
  41.     printf("\n\tDUNGEON and cakes and mushroms v.1.4\n\n");
  42.     char *end,*end1;
  43.    
  44.     if(argc!=3)
  45.     {
  46.         printf("usage: ilosc_klientow rozmiar_lab\n\n");
  47.         exit(1);
  48.     }
  49.     else if(strtol(argv[1],&end,10)<=0)
  50.     {
  51.         printf("Niepoprawny 1 parametr - ilosc klientow!\nLiczba ma byc nieujemna i wieksza od 0\n");
  52.         exit(1);
  53.     }
  54.     else if(strtol(argv[2],&end1,10)<=0)
  55.     {
  56.         printf("Niepoprawny 2 parametr - rozmiar labiryntu!\nLiczba ma byc nieujemna i wieksza od 0\n");
  57.         exit(1);
  58.     }
  59.    
  60.     int clients_count,size_lab,size;
  61.    
  62.  
  63.     size_lab= strtol(argv[2],&end1,10);
  64.     printf("size_lab = %d\n",size_lab);
  65.     clients_count = strtol(argv[1],&end,10);
  66.     printf("clients_count = %d\n",clients_count);
  67.     int minions[clients_count][2];
  68.    
  69.     int steps=size_lab*4;
  70.    
  71.     srand(time(NULL));
  72.     char **true_labyrinth=NULL;
  73.     //char **invisible_lab=NULL;
  74.    
  75.     size=2*size_lab+1+(size_lab/3);
  76.    
  77.     true_labyrinth=createLab(size_lab,clients_count);
  78.     showTrueLabyrinth(true_labyrinth,size);
  79.     printf("inv1\n\n");
  80.     //invisible_lab=generateInvisibleLabyrinth(size);
  81.     //printf("inv2\n\n");
  82.     //showTrueLabyrinth(invisible_lab,size);
  83.     //showInvisibleDungeon(invisible_lab,size);
  84.    
  85.    
  86.     char minionsChar[5] = {'-','^','V','!','+'};
  87.     int xx,yy;
  88.     printf("Generuje pozycje pionkow:");
  89.     for(int i=0;i<clients_count;i++)
  90.     {
  91.         xx=0;
  92.         yy=0;
  93.         while(true_labyrinth[xx][yy]!='0')
  94.         {
  95.             xx=generatePosition(size);
  96.             yy=generatePosition(size);
  97.            
  98.         }
  99.         true_labyrinth[xx][yy]=minionsChar[i];
  100.         minions[i][0] = xx;
  101.         minions[i][1] = yy;
  102.     }
  103.    
  104.     printf("\nWygenerowalem:\n");
  105.     showTrueLabyrinth(true_labyrinth,size);
  106.     showInvisibleDungeon(true_labyrinth,size);
  107.    
  108.    
  109.     int sockfd,sin_size;
  110.     int tru = 1;
  111.    
  112.    
  113.     struct sockaddr_in my_addr;
  114.     struct sockaddr_in their_addr;
  115.    
  116.     struct sigaction sa;
  117.    
  118.     if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
  119.     {
  120.         perror("Error - socket");
  121.         exit(1);
  122.     }
  123.    
  124.     if(fcntl(sockfd,F_SETFL,O_NONBLOCK)==-1)
  125.     {
  126.         perror( "Error - fcntl - nie nastawilem nieblokowania" );
  127.         exit(1);
  128.     }
  129.    
  130.     if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&tru,sizeof(int))==-1)
  131.     {
  132.         perror("Error - setsockopt");
  133.         exit(1);
  134.     }
  135.    
  136.     my_addr.sin_family = AF_INET;
  137.     my_addr.sin_port = htons(MYPORT);
  138.     my_addr.sin_addr.s_addr = INADDR_ANY;
  139.     memset(&(my_addr.sin_zero), '\0', 8 );
  140.    
  141.    
  142.     if(bind(sockfd,(struct sockaddr *) &my_addr,sizeof(struct sockaddr))==-1)
  143.     {
  144.         perror("Error - bind\n");
  145.         exit(1);
  146.     }
  147.    
  148.     if(listen(sockfd,BACKLOG)==-1)
  149.     {
  150.         perror("Error - listen\n");
  151.         exit(1);
  152.     }
  153.    
  154.     sa.sa_handler = deadChildHandler;
  155.     sigemptyset( & sa.sa_mask );
  156.     sa.sa_flags = SA_RESTART;
  157.    
  158.     if(sigaction(SIGCHLD,& sa,NULL)==-1)
  159.     {
  160.         perror("Error - sigaction");
  161.         exit(1);
  162.     }
  163.    
  164.     //showTrueLabyrinth();
  165.     //generateInvisibleLabyrinth(clients_count);
  166.     //showInvisibleDungeon();    
  167.  
  168.    
  169.     int fdTab[10];
  170.     int m = 0,i;
  171.    
  172.     sin_size = sizeof( struct sockaddr_in );
  173.     int rc, socket_number = 0, max_socket = 5;
  174.     fd_set fds, readfds;
  175.     FD_ZERO(&fds);
  176.     FD_SET(sockfd, &fds);
  177.    
  178.  
  179.    
  180.     while(1)
  181.     {
  182.         readfds = fds;
  183.         rc = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
  184.         if (rc == -1)
  185.         {
  186.                 perror("Error - select\n");
  187.                 break;
  188.         }
  189.         //printf("wszedł do while\n\n");
  190.         int i=0;
  191.  
  192.         while(i<FD_SETSIZE)
  193.         {
  194.             if (FD_ISSET(i, &readfds))
  195.             {
  196.                 if (i == sockfd)
  197.                 {
  198.                     if (socket_number < max_socket)
  199.                     {
  200.                         if((fdTab[socket_number] = accept(sockfd,( struct sockaddr *) &their_addr,(socklen_t *) &sin_size))==-1)
  201.                         {
  202.                             //printf( "Error - accept" );                               //wylaczona notyfikacja, poniewaz polaczenia nie sa blokujace i bylby niezly span na ekranie
  203.                             continue;
  204.                         }
  205.                         printf("And we have a client from:  %s\n",inet_ntoa(their_addr.sin_addr));
  206.                         //x = minions[socket_number][0];
  207.                         //y = minions[socket_number][1];
  208.  
  209.                         printf("_____%d  %d______\n",i,socket_number);
  210.                         move(fdTab[socket_number],true_labyrinth,minions,minionsChar[socket_number],socket_number,steps,size);
  211.                         FD_SET(fdTab[socket_number], &fds);
  212.                         socket_number++;
  213.                     }
  214.                 }
  215.             }
  216.             i++;
  217.         }
  218.         shutdown( fdTab[m], SHUT_RDWR );
  219.     }
  220.     close(sockfd);
  221.     return 0;
  222. }
  223.  
  224. void deadChildHandler(int s)
  225. {
  226.     while( wait( NULL ) > 0 );
  227. }
  228.  
  229. int generatePosition(int n)
  230. {
  231.     int x;
  232.     srand(time(NULL));
  233.     x = rand()%(n-2)+1;
  234.     return x;
  235. }
  236.  
  237. void showTrueLabyrinth(char** lab,int n)
  238. {
  239.     for(int i=0;i<n;i++)
  240.     {
  241.         for(int j=0;j<n;j++)
  242.         {
  243.             printf("%c",lab[j][i]);
  244.         }
  245.         printf("\n");
  246.     }
  247. }
  248.  
  249. void showInvisibleDungeon(char** lab, int n)
  250. {
  251.     for(int i=0;i<n;i++)
  252.     {
  253.         for(int j=0;j<n;j++)
  254.         {
  255.             if (lab[j][i]=='0')
  256.                 printf(" ");
  257.             else if (lab[j][i]=='1')
  258.                 printf("X");
  259.             else
  260.                 printf("%c",lab[j][i]);
  261.         }
  262.         printf("\n");
  263.     }
  264. }
  265.  
  266. /*char** generateInvisibleLabyrinth(int n)
  267. {
  268.     char ** invisible_lab = NULL;
  269.    
  270.     invisible_lab=(char**)calloc(2*n+2, sizeof(char*));
  271.    
  272.     for(int i=0;i<2*n+2;i++)
  273.     {
  274.         invisible_lab[i]=(char*)calloc(2*n+2,sizeof(char));
  275.     }
  276.    
  277.     for(int i=0;i<n;i++)
  278.     {
  279.         for(int j=0;j<n;j++)
  280.         {
  281.             invisible_lab[i][j] = ' ';
  282.             invisible_lab[0][j] = '1';
  283.             invisible_lab[n-1][j] = '1';
  284.         }
  285.         invisible_lab[i][0] = '1';
  286.         invisible_lab[i][n-1] = '1';
  287.     }
  288.     return invisible_lab;
  289. }*/
  290.  
  291. char** createLab(int n,int clients)
  292. {
  293.     struct room
  294.     {
  295.         bool left_wall;
  296.         bool top_wall;
  297.         bool visited;
  298.     };
  299.     int size_lab=n;
  300.     bool rand_start=0;
  301.    
  302.    
  303.     room labyrinth[size_lab][size_lab];
  304.  
  305.     int i,j;
  306.    
  307.     for(i=0;i<size_lab;i++)
  308.     {
  309.         for(j=0;j<size_lab;j++)
  310.         {
  311.             labyrinth[i][j].left_wall=1;
  312.             labyrinth[i][j].top_wall=1;
  313.             labyrinth[i][j].visited=0;
  314.         }
  315.     }
  316.    
  317.     int x_hall,y_hall,hall_size,max_hall_size;
  318.  
  319.     int x_start=0;
  320.     int y_start=0;
  321.     if (!rand_start)
  322.     {
  323.         x_start=(rand() % size_lab)+0;
  324.         y_start=(rand() % size_lab)+0;
  325.         rand_start=1;
  326.     }
  327.     int direction=0;
  328.     bool koniec=1;
  329.     while(koniec==1)
  330.     {
  331.         if(labyrinth[x_start][y_start].visited==0)
  332.             labyrinth[x_start][y_start].visited=1;
  333.        
  334.         direction=(rand()%4)+0;
  335.         if (direction==0)
  336.         {
  337.             if (y_start<size_lab-1){
  338.                 if (labyrinth[x_start][y_start+1].visited==0)
  339.                     labyrinth[x_start][y_start+1].top_wall=0;
  340.                 y_start++;
  341.             }  
  342.         }
  343.         else if (direction==1)
  344.         {
  345.             if (y_start>0)
  346.             {
  347.                 if (labyrinth[x_start][y_start-1].visited==0)
  348.                     labyrinth[x_start][y_start].top_wall=0;
  349.                 y_start--;
  350.             }
  351.         }
  352.         else if (direction==2)
  353.         {
  354.             if (x_start>0)
  355.             {
  356.                 if (labyrinth[x_start-1][y_start].visited==0)
  357.                     labyrinth[x_start][y_start].left_wall=0;
  358.                 x_start--;
  359.             }
  360.         }
  361.         else if(direction==3)
  362.         {
  363.             if (x_start<size_lab-1)
  364.             {
  365.                 if (labyrinth[x_start+1][y_start].visited==0)
  366.                     labyrinth[x_start+1][y_start].left_wall=0;
  367.                 x_start++;
  368.             }
  369.         }
  370.    
  371.         for(int i=0;i<size_lab;i++)
  372.         {
  373.             for(int j=0;j<size_lab;j++)
  374.             {
  375.                 if (labyrinth[i][j].visited==0)
  376.                 {
  377.                     koniec=1;
  378.                     break;
  379.                 }
  380.                 else
  381.                     koniec=0;
  382.             }
  383.             if(koniec==1)
  384.                 break;
  385.         }
  386.     }
  387.    
  388.     char perfect [2*size_lab+1][2*size_lab+1];
  389.    
  390.     for(int i=0;i<2*size_lab+1;i++)
  391.     {
  392.         for(int j=0;j<2*size_lab+1;j++)
  393.         {
  394.             if ((i==2*size_lab))
  395.                 perfect[j][i]='1';
  396.             else if ((j==2*size_lab))
  397.                 perfect[j][i]='1';
  398.             else if ((i%2==1)&&(j%2==1))
  399.                 perfect[j][i]='0';
  400.             else if ((i%2==0)&&(j%2==1))
  401.             {
  402.                 if (labyrinth[j/2][i/2].top_wall==0)
  403.                     perfect[j][i]='0';
  404.                 else
  405.                     perfect[j][i]='1';
  406.             }
  407.             else if ((i%2==1)&&(j%2==0))
  408.             {
  409.                 if (labyrinth[j/2][i/2].left_wall==0)
  410.                     perfect[j][i]='0';
  411.                 else
  412.                     perfect[j][i]='1';
  413.             }
  414.             else if ((i%2==0)&&(j%2==0))
  415.                 perfect[j][i]='1';
  416.         }
  417.     }
  418.    
  419.     max_hall_size=size_lab/3;
  420.     int copies_count=size_lab/3;
  421.     for (int k=0;k<copies_count;k++)
  422.     {
  423.         x_hall=(rand() % (2*size_lab-max_hall_size)) + (max_hall_size/2+1);
  424.         y_hall=(rand() % (2*size_lab-max_hall_size)) + (max_hall_size/2+1);
  425.         hall_size=(rand() % (max_hall_size-1)) + 2;
  426.  
  427.         for( i=(x_hall-(hall_size/2));i<(x_hall+hall_size/2+1);i++)
  428.         {
  429.             for(j=(y_hall-(hall_size/2));j<(y_hall+hall_size/2+1);j++)
  430.                 perfect[i][j]='0';
  431.         }
  432.     }
  433.                
  434.     int row[copies_count];
  435.     int column[copies_count];
  436.     for(int k=0;k<copies_count;k++)
  437.     {
  438.         row[k]=(rand() % (2*size_lab-1)) + 1;
  439.         column[k]=(rand() % (2*size_lab-1)) + 1;
  440.     }
  441.     char perfect_with_halls [2*size_lab+1][2*size_lab+1+copies_count];
  442.     int displacement=0;
  443.     int copies;
  444.     for(int j=0;j<2*size_lab+1;j++)
  445.     {
  446.         copies=1;
  447.         for(int k=0;k<copies_count;k++)
  448.             if (column[k]==j)
  449.                 copies++;
  450.            
  451.         for(int l=0;l<copies;l++)
  452.         {
  453.             if (l>0)
  454.                 displacement++;
  455.             for(int i=0;i<2*size_lab+1;i++)
  456.                 perfect_with_halls[i][j+displacement]=perfect[i][j];
  457.         }
  458.     }  
  459.                    
  460.     displacement=0;
  461.     copies=1;
  462.     char ** true_labyrinth = NULL;
  463.    
  464.     true_labyrinth=(char**)calloc(2*size_lab+1+(size_lab/3), sizeof(char*));
  465.    
  466.     for(int i=0;i<2*size_lab+2;i++)
  467.     {
  468.         true_labyrinth[i]=(char*)calloc(2*size_lab+1+(size_lab/3),sizeof(char));
  469.     }
  470.  
  471.    
  472.     for(int i=0;i<2*size_lab+1+copies_count;i++)
  473.     {
  474.         copies=1;
  475.             for(int k=0;k<copies_count;k++)
  476.             {
  477.                 if (row[k]==i)
  478.                     copies++;
  479.             }
  480.             for(int l=0;l<copies;l++)
  481.             {
  482.                 if (l>0)
  483.                     displacement++;
  484.                
  485.                 for(int j=0;j<2*size_lab+1+copies_count;j++)
  486.                     true_labyrinth[i+displacement][j]=perfect_with_halls[i][j];
  487.  
  488.             }
  489.     }
  490.                
  491.     for(int i=0;i<2*size_lab+1+copies_count;i++)
  492.     {
  493.         for(int j=0;j<2*size_lab+1+copies_count;j++)
  494.         {
  495.             if (true_labyrinth[j][i]=='0')
  496.                 printf(" ");
  497.             if (true_labyrinth[j][i]=='1')
  498.                 printf("X");
  499.         }
  500.         printf("\n");
  501.     }
  502.     printf("\n");
  503.    
  504.     return true_labyrinth;
  505. }
  506.  
  507. void move(int sockfd,char** true_labyrinth,int minions[][2],char c,int number,int steps,int size)
  508. {
  509.     char znak;
  510.     int xn = minions[number][0];
  511.     int yn = minions[number][1];
  512.     int i;
  513.    
  514.     for(i=0;i<steps;i++)
  515.     {
  516.         char buf[1];
  517.        
  518.         if((recv(sockfd,buf,sizeof(buf),0))==-1)
  519.         {
  520.             sleep(1);
  521.         }
  522.  
  523.         znak = buf[0];
  524.        
  525.         if(znak=='w' || znak=='s' || znak=='a' || znak=='d')
  526.         {
  527.             printf("%c\n",znak);
  528.  
  529.             switch(znak)
  530.             {
  531.                 case 'w':
  532.                     //yn--;
  533.  
  534.                     switch(true_labyrinth[xn][yn-1])
  535.                     {
  536.                         case '1':
  537.                             if(send( sockfd,"Wall",14,MSG_DONTWAIT)==-1)
  538.                                 printf("Error - send\n");
  539.  
  540.                             //invisible_lab[xn][yn] = '1';
  541.                             //yn++;
  542.                             break;
  543.                         case '0':
  544.                             if(send( sockfd,"Hall",14,MSG_DONTWAIT)==-1)
  545.                                 printf("Error - send\n");
  546.                            
  547.                             //invisible_lab[xn][yn+1] = '0';
  548.                             //invisible_lab[xn][yn] = c;
  549.                             true_labyrinth[xn][yn-1]=true_labyrinth[xn][yn];
  550.                             true_labyrinth[xn][yn]='0';
  551.                             minions[number][1]=yn-1;                           
  552.                             break;
  553.                     }
  554.                     showInvisibleDungeon(true_labyrinth,size);
  555.                     break;
  556.  
  557.                 case 's':
  558.                     //yn++;
  559.  
  560.                     switch(true_labyrinth[xn][yn+1])
  561.                     {
  562.                         case '1':
  563.                             if(send( sockfd,"Wall",14,MSG_DONTWAIT)==-1)
  564.                                 printf("Error - send\n");
  565.  
  566.                             //invisible_lab[xn][yn] = '1';
  567.                             //yn--;
  568.                             break;
  569.                         case '0':
  570.                             if(send( sockfd,"Hall",14,MSG_DONTWAIT)==-1)
  571.                                 printf("Error - send\n");
  572.                             //invisible_lab[xn][yn-1] = '0';
  573.                             //invisible_lab[xn][yn] = c;
  574.                             true_labyrinth[xn][yn+1]=true_labyrinth[xn][yn];
  575.                             true_labyrinth[xn][yn]='0';
  576.                             minions[number][1]=yn+1;
  577.                             break;
  578.                     }
  579.                     showInvisibleDungeon(true_labyrinth,size);
  580.                     break;
  581.  
  582.                 case 'a':
  583.                     //xn--;
  584.  
  585.                     switch(true_labyrinth[xn-1][yn])
  586.                     {
  587.                         case '1':
  588.                             if(send( sockfd,"Wall",14,MSG_DONTWAIT)==-1)
  589.                                 printf("Error - send\n");
  590.  
  591.                             //invisible_lab[xn][yn] = '1';
  592.                             //xn++;
  593.                             break;
  594.                         case '0':
  595.                             if(send( sockfd,"Hall",14,MSG_DONTWAIT)==-1)
  596.                                 printf("Error - send\n");
  597.                             //invisible_lab[xn+1][yn] = '0';
  598.                             //invisible_lab[xn][yn] = c;
  599.                             true_labyrinth[xn-1][yn]=true_labyrinth[xn][yn];
  600.                             true_labyrinth[xn][yn]='0';
  601.                             minions[number][0]=xn-1;
  602.                             break;
  603.                     }
  604.                     showInvisibleDungeon(true_labyrinth,size);
  605.                     break;
  606.  
  607.                 case 'd':
  608.                     //xn++;
  609.  
  610.                     switch(true_labyrinth[xn+1][yn])
  611.                     {
  612.                         case '1':
  613.                             if(send( sockfd,"Wall",14,MSG_DONTWAIT)==-1)
  614.                                 printf("Error - send\n");
  615.  
  616.                             //invisible_lab[xn][yn] = '1';
  617.                             //xn--;
  618.                             break;
  619.                         case '0':
  620.                             if(send( sockfd,"Hall",14,MSG_DONTWAIT)==-1)
  621.                                 printf("Error - send\n");
  622.                             //invisible_lab[xn-1][yn] = '0';
  623.                             //invisible_lab[xn][yn] = c;
  624.                             true_labyrinth[xn+1][yn]=true_labyrinth[xn][yn];
  625.                             true_labyrinth[xn][yn]='0';
  626.                             minions[number][0]=xn+1;
  627.                             break;
  628.                     }
  629.                     showInvisibleDungeon(true_labyrinth,size);
  630.                     break;
  631.             }
  632.         }
  633.     }
  634. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement