| ||
Ecco, quando e se hai voglia...son ben accette critiche (anche pesanti ) e consigli
CODICE #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/wait.h> #include <ncurses.h> #include <fcntl.h> #define ROW 24 #define COL 80 #define WRITE 1 #define READ 0 #define DX 261 #define SX 260 #define DOWN 258 #define UP 259 //***************************************** // uso 2 array per le direzioni in modo da risparmiare 4 if int dir_up_down[4]={0,0,-1,1}; // array per direzioni (variabili globali) int dir_lat[4]={1,-1,0,0}; // prototipi void prova_pipe_g(int pipe_out,int pipe2); void prova_pipe_r(int pipe_in); void tasti (int pipe2); //**************************************************************************** //**************************************************************************** // routine per gestione tasti: impedisce movimenti in retro // 261 sarebbe un offset 261-tasto es 258 (261-258)=3 // vuol dire che mando 3 con la pipe che in array int dir_up_down[4]={0,0,-1,1}; // corrisponde ad giu void tasti (int pipe2) { int ch=0; int buff[1]; int tmp_dir=0; while (1) { ch=getch(); if (ch==DX && tmp_dir!=1) //se vai a dx,negato sx { buff[0]=261-ch;tmp_dir=buff[0]; write(pipe2, buff, sizeof(int)); } if (ch==SX && tmp_dir!=0) { buff[0]=261-ch;tmp_dir=buff[0]; write(pipe2, buff, sizeof(int));//scrivo } if (ch==UP && tmp_dir!=3) { buff[0]=261-ch;tmp_dir=buff[0]; write(pipe2, buff, sizeof(int));//scrivo } if (ch==DOWN && tmp_dir!=2) { buff[0]=261-ch;tmp_dir=buff[0]; write(pipe2, buff, sizeof(int));//scrivo } } } //**************************************************************************** // processo che inizializza il bruco e gestisce i movimenti // il bruco parte da dx a sx, se riceve una direzione valida // la direzione cambia // per muovere il bruco scalo le posizioni dell'array, poi aggiorno la testa //**************************************************************************** void prova_pipe_g(int pipe_out,int pipe2) //guardia { struct { int x[100]; int y[100]; int rnd; int tmp_coda_x; int tmp_coda_y; int corpo; }snake; snake.corpo=20; int j,buff[1],x,tmp_x,tmp_y; for (j=0; j<snake.corpo-1; j++) { snake.x[j]=10; // creo array bruco snake.y[j]=31-j; } for (j=0; j<snake.corpo-1; j++) // stampo bruco { move(snake.x[j], snake.y[j]); addch('@'); } move(snake.x[0], snake.y[0]); addch('X'); refresh(); while(1) { x=fcntl(pipe2, F_SETFL, O_NDELAY); // attivo flag pipe non bloccante if (x!=-1) // se buffer non vuoto--->leggi read(pipe2,buff,1*sizeof(int)); snake.tmp_coda_x=snake.x[snake.corpo-1]; // memorizzo la coda che il padre metterà come spazio snake.tmp_coda_y=snake.y[snake.corpo-1]; for (j=0; j<snake.corpo-1; j++) { tmp_x=snake.x[j+1]; // scalo posizioni per muovere tmp_y=snake.y[j+1]; snake.x[j+1]=snake.x[0]; snake.y[j+1]=snake.y[0]; snake.x[0]=tmp_x; snake.y[0]=tmp_y; } snake.x[0]=snake.x[1]+dir_up_down[buff[0]]; //aggiorno dir testa in base al tasto arrivato snake.y[0]=snake.y[1]+dir_lat[buff[0]]; write(pipe_out, &snake, sizeof(snake)); // scrivo al padre usleep(90000); } } //**************************************************************************** // il padre legge e stampa i dati //**************************************************************************** void prova_pipe_r(int pipe_in) // pipe padre(legge) { struct { int x[100]; int y[100]; int rnd; int tmp_coda_x; int tmp_coda_y; int corpo; }st_padre; int i; while(1) { read(pipe_in,&st_padre,sizeof(st_padre)); // Leggo l'evento move (st_padre.tmp_coda_x, st_padre.tmp_coda_y); addch(' '); for (i=0; i<st_padre.corpo-1; i++) { move (st_padre.x[i], st_padre.y[i]); addch('@'); } move (st_padre.x[0], st_padre.y[0]); addch('X');refresh(); } } int main(void) { int pid[2]; int my_pipe[2]; int my_pipe_2[2]; initscr(); // init ncurses cbreak(); noecho(); curs_set(0); intrflush(stdscr, FALSE); keypad(stdscr, TRUE); if (pipe(my_pipe)==-1) { puts("erroer"); return -1; } if (pipe(my_pipe_2)==-1) { puts("erroer"); return -1; } //******************************************** // figlio 1 //******************************************** pid[0] = fork(); if ( pid[0] == -1 ) { printf ("Impossibile creare un nuovo processo\n"); exit(1); } if ( pid[0] == 0 ) { close (my_pipe[READ]); prova_pipe_g(my_pipe[WRITE],my_pipe_2[READ]); //passo descrittore pipe scrittura } //******************************************** // figlio 2 //******************************************** pid[1] = fork(); if ( pid[1] == -1 ) { printf ("Impossibile creare un nuovo processo\n"); exit(1); } if ( pid[1] == 0 ) { tasti(my_pipe_2[WRITE]); //passo descrittore pipe scrittura } else // entro nel padre (main) { prova_pipe_r(my_pipe[READ]); } endwin(); return 0; } |