Advertisement
Geralt1001

Untitled

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