Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <curses.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define PASSO 1 /* entita dello spostamento del ladro */
- #define SU 65 /* Freccia su */
- #define GIU 66 /* Freccia giu */
- #define SINISTRA 68 /* Freccia sinsitra */
- #define DESTRA 67 /* Freccia destra */
- #define MAXX 80 /* Numero di colonne dello schermo */
- #define MAXY 24 /* Numero di righe dello schermo */
- /* Struttura per la comunicazione tra figli e padre */
- struct pos {
- char c; /* soggetto che invia il dato: ladro o guardia */
- int x; /* coordinata x */
- int y; /* coordinata y */
- };
- pthread_mutex_t muta=PTHREAD_MUTEX_INITIALIZER;
- void* ladro(void* arg) {
- struct pos pos_ladro;
- struct pos prev_pos_ladro;
- long int r;
- int dx,dy;
- pos_ladro.x=1;
- pos_ladro.y=1;
- pos_ladro.c='$';
- //mvprintw(0,1,"%c",pos_ladro.c);
- while(1) {
- r=random();
- if(r<RAND_MAX/2)
- dx=PASSO;
- else
- dx=-PASSO;
- if(pos_ladro.x+dx<1 || pos_ladro.x+dx>=MAXX)
- dx=-dx;
- pos_ladro.x+=dx;
- r=random();
- if(r<RAND_MAX/2)
- dy=PASSO;
- else
- dy=-PASSO;
- if(pos_ladro.y+dy<1 || pos_ladro.y+dy>=MAXY)
- dy=-dy;
- pos_ladro.y+=dy;
- pthread_mutex_lock(&muta);
- mvprintw(prev_pos_ladro.y,prev_pos_ladro.x," ");
- mvprintw(pos_ladro.y,pos_ladro.x,"%c",pos_ladro.c);
- refresh();
- prev_pos_ladro.y = pos_ladro.y;
- prev_pos_ladro.x = pos_ladro.x;
- pthread_mutex_unlock(&muta);
- usleep(500000);
- }
- }
- void* avvocato(void* arg){
- struct pos pos_avvocato;
- struct pos prev_pos_avvocato;
- long int r;
- int dx,dy;
- pos_avvocato.x=1;
- pos_avvocato.y=10;
- pos_avvocato.c='A';
- while(1) {
- r=random();
- if(r<RAND_MAX/2)
- dx=PASSO;
- else
- dx=-PASSO;
- if(pos_avvocato.x+dx<1 || pos_avvocato.x+dx>=MAXX)
- dx=-dx;
- pos_avvocato.x+=dx;
- r=random();
- if(r<RAND_MAX/2)
- dy=PASSO;
- else
- dy=-PASSO;
- if(pos_avvocato.y+dy<1 || pos_avvocato.y+dy>=MAXY)
- dy=-dy;
- pos_avvocato.y+=dy;
- pthread_mutex_lock(&muta);
- mvprintw(prev_pos_avvocato.y,prev_pos_avvocato.x," ");
- mvprintw(pos_avvocato.y,pos_avvocato.x,"%c",pos_avvocato.c);
- refresh();
- prev_pos_avvocato.y = pos_avvocato.y;
- prev_pos_avvocato.x = pos_avvocato.x;
- pthread_mutex_unlock(&muta);
- usleep(500000);
- }
- }
- void guardia(int pipeout) {
- struct pos pos_guardia;
- pos_guardia.c='#';
- pos_guardia.x=MAXX-1;
- pos_guardia.y=MAXY-1;
- write(pipeout,&pos_guardia,sizeof(pos_guardia));
- while(1) {
- char c;
- switch(c=getch()) {
- case SU:
- if(pos_guardia.y>0)
- pos_guardia.y-=1;
- break;
- case GIU:
- if(pos_guardia.y<MAXY-1)
- pos_guardia.y+=1;
- break;
- case SINISTRA:
- if(pos_guardia.x>0)
- pos_guardia.x-=1;
- break;
- case DESTRA:
- if(pos_guardia.x<MAXX-1)
- pos_guardia.x+=1;
- break;
- }
- write(pipeout,&pos_guardia,sizeof(pos_guardia));
- }
- }
- void* guardia(void* arg);
- void controllo(void);
- int main () {
- initscr(); /* inizializzazione dello schermo */
- noecho(); /* i caratteri corrispondenti ai tasti premuti non saranno
- * visualizzati sullo schermo del terminale */
- srand((int)time(0)); /*inizializziamo il generatore di numeri random*/
- curs_set(0); /* nasconde il cursore */
- thread_mutex_init(&mtx, NULL);
- pthread_t ladro_thread;
- pthread_t avvocato_thread;
- //pthread_t guardia_thread;
- //pthread_t controllo_thread;
- pthread_create (&ladro_thread, NULL, &ladro, NULL);
- pthread_create (&avvocato_thread, NULL, &avvocato, NULL);
- //pthread_create (&guardia_thread, NULL, &ladro, NULL);
- //pthread_create (&controllo_thread, NULL, &ladro, NULL);
- pthread_join(ladro_thread, NULL);
- pthread_join(avvocato_thread, NULL);
- //pthread_join(guardia_thread, NULL);
- //pthread_join(controllo_thread, NULL);
- endwin();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement