Advertisement
nigu

difesa aerea pipes

Dec 14th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <curses.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <time.h>
  6.  
  7. #define SOPRA 65
  8. #define SOTTO 66
  9. #define SINISTRA 68
  10. #define DESTRA 67
  11.  
  12. #define MAXX 80
  13. #define MAXY 24
  14.  
  15. #define DELAY 50000
  16.  
  17. struct position {
  18.     int x;
  19.     int y;
  20.     char c;
  21. };
  22.  
  23. void Area (int pipein);
  24. void Nemico1 (int pipeout);
  25. void Nemico2 (int pipeout);
  26. void Amico (int pipeout);
  27.  
  28. int main()
  29. {
  30.     int scudo = 3;
  31.    
  32.     int p[2];
  33.     int pidN1;
  34.     int pidN2;
  35.     int pidA;
  36.    
  37.     initscr();
  38.     noecho();
  39.     curs_set(0);
  40.    
  41.     srand(time(NULL));
  42.        
  43.     pipe(p);
  44.  
  45.     pidN1 = fork();
  46.    
  47.     if (pidN1==0){
  48.         /*sono nel processo figlio N1*/
  49.         Nemico1(p[1]); 
  50.     }
  51.     else {
  52.         pidN2=fork();
  53.         if (pidN2==0) {
  54.             /*sono nel processo figlio N2*/
  55.             Nemico2(p[1]);
  56.         }
  57.         else {
  58.             pidA=fork();
  59.             if (pidA==0){
  60.                 /*sono nel processo figlio A*/
  61.                
  62.                 mvaddch(MAXY/2,MAXX/2,'#');
  63.                 Amico(p[1]);
  64.             }
  65.             else {
  66.                 /* sono nel padre Area*/
  67.                 Area(p[0]);    
  68.             }
  69.         }
  70.     }
  71.    
  72.     kill(pidN1);
  73.     kill(pidN2);
  74.     kill(pidA);
  75.    
  76.     endwin();
  77.    
  78.     printf("GAME OVER");
  79.    
  80.     return 0;
  81. }
  82.  
  83.  
  84. void Amico (pipeout){
  85.     struct position amico;
  86.     int key;
  87.    
  88.     amico.c = '#';
  89.     amico.x=MAXX/2;
  90.     amico.y=MAXY/2;
  91.    
  92.     write(pipeout,&amico,sizeof(amico));
  93.        
  94.     while (1){
  95.         key = getch();
  96.         switch (key) {
  97.             case SOPRA:
  98.                 if (amico.y>0)
  99.                     amico.y--;
  100.                 break;
  101.             case SOTTO:
  102.                 if (amico.y<MAXY-1)
  103.                     amico.y++;
  104.                 break;
  105.             case DESTRA:
  106.                 if (amico.x<MAXX-1)
  107.                     amico.x++;
  108.                 break;
  109.             case SINISTRA:
  110.                 if (amico.x>0)
  111.                     amico.x--;
  112.                 break;
  113.         }
  114.         write(pipeout,&amico,sizeof(amico));
  115.     }
  116. }
  117.  
  118. void Nemico1(pipeout){
  119.     struct position nemico1;
  120.     int i=0;
  121.     nemico1.x = 1;
  122.     nemico1.y = 2;
  123.     nemico1.c = '+';
  124.        
  125.     while (1){
  126.         nemico1.y = 2 + random()%MAXY-2;
  127.         for (i=1;i<MAXX;i++){
  128.             nemico1.x=i;
  129.             write(pipeout,&nemico1,sizeof(nemico1));
  130.             usleep(DELAY);
  131.         }          
  132.     }
  133. }
  134.  
  135. void Nemico2(pipeout){
  136.     struct position nemico2;
  137.     int i=0;
  138.     nemico2.y = 2;
  139.     nemico2.c = 'X';
  140.    
  141.     while (1){
  142.         nemico2.x = random()%MAXX;
  143.         for (i=2;i<MAXY;i++){
  144.             nemico2.y=i;
  145.             write(pipeout,&nemico2,sizeof(nemico2));
  146.             usleep(DELAY);
  147.         }          
  148.     }
  149. }
  150.  
  151. void Area(pipein){
  152.    
  153.     int collision=0;
  154.     int scudi=3;
  155.     int i;
  156.    
  157.     struct position elemento;
  158.     struct position nemico1;
  159.     struct position nemico2;
  160.     struct position amico;
  161.     struct position balloon;
  162.    
  163.    
  164.    
  165.     while (!collision){
  166.         mvprintw(0,1,"SCUDI %d", scudi);
  167.         refresh();
  168.         read(pipein,&elemento,sizeof(elemento));
  169.         switch (elemento.c){
  170.             case '+':
  171.                 mvaddch(nemico1.y,nemico1.x,' ');
  172.                 nemico1.x=elemento.x;
  173.                 nemico1.y=elemento.y;
  174.                 mvaddch(nemico1.y,nemico1.x,'+');
  175.                 break;
  176.             case 'X':
  177.                 mvaddch(nemico2.y,nemico2.x,' ');
  178.                 nemico2.x=elemento.x;
  179.                 nemico2.y=elemento.y;
  180.                 mvaddch(nemico2.y,nemico2.x,'X');
  181.                 break;
  182.             case '#':
  183.                 mvaddch(amico.y,amico.x,' ');
  184.                 amico.x=elemento.x;
  185.                 amico.y=elemento.y;
  186.                 mvaddch(amico.y,amico.x,'#');
  187.                 break;
  188.             default:
  189.                 break;
  190.         }
  191.        
  192.         if (!(i++%100)){
  193.             mvaddch(balloon.y,balloon.x,' ');
  194.             balloon.x=random()%MAXX;
  195.             balloon.y=random()%MAXY;
  196.             mvaddch(balloon.y,balloon.x,'O');
  197.         }
  198.        
  199.         if ((amico.x == nemico1.x && amico.y == nemico1.y)||
  200.             (amico.x == nemico2.x && amico.y == nemico2.y)){
  201.             if (scudi>0){
  202.                 scudi--;
  203.             }
  204.             else {
  205.                 collision = 1;
  206.             }
  207.                
  208.         }
  209.        
  210.         if (amico.x == balloon.x && amico.y == balloon.y){
  211.             if (scudi<6){
  212.                 scudi++;
  213.             }
  214.        
  215.         }
  216.            
  217.        
  218.     }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement