the winning total should be read from the keyboard before the game starts and be
ID: 3552605 • Letter: T
Question
the winning total should be read from the keyboard before the game starts and before any fork(for example 50 or 500).
Your program should ignore CTR-C and CTR-Z signals
The referee should be the one to check the scores and to declare the winner. A com- mon file should be used between processes for communicating their current score to the referee.
Explanation / Answer
#include #include #include #include #include #include #include // defaults #define PROB_TREE 0.55 #define PROB_F 0.00001 #define PROB_P 0.001 #define TIMERFREQ 100 #ifndef WIDTH # define WIDTH 640 #endif #ifndef HEIGHT # define HEIGHT 480 #endif #ifndef BPP # define BPP 32 #endif #if BPP != 32 #warning This program could not work with BPP different from 32 #endif uint8_t *field[2], swapu; double prob_f = PROB_F, prob_p = PROB_P, prob_tree = PROB_TREE; enum cell_state { VOID, TREE, BURNING }; // simplistic random func to give [0, 1) double prand() { return (double)rand() / (RAND_MAX + 1.0); } // initialize the field void init_field(void) { int i, j; swapu = 0; for(i = 0; i pixels; uint32_t color; SDL_PixelFormat *f = s->format; pthread_mutex_lock(&synclock); for(i = 0; i >3)) = color; } } pthread_mutex_unlock(&synclock); } int main(int argc, char **argv) { SDL_Surface *scr = NULL; SDL_Event event[1]; bool quit = false, running = false; SDL_TimerID tid; // add variability to the simulation srand(time(NULL)); // we can change prob_f and prob_p // prob_f prob of spontaneous ignition // prob_p prob of birth of a tree double *p; for(argv++, argc--; argc > 0; argc--, argv++) { if ( strcmp(*argv, "prob_f") == 0 && argc > 1 ) { p = &prob_f; } else if ( strcmp(*argv, "prob_p") == 0 && argc > 1 ) { p = &prob_p; } else if ( strcmp(*argv, "prob_tree") == 0 && argc > 1 ) { p = &prob_tree; } else continue; argv++; argc--; char *s = NULL; double t = strtod(*argv, &s); if (s != *argv) *p = t; } printf("prob_f %lf prob_p %lf ratio %lf prob_tree %lf ", prob_f, prob_p, prob_p/prob_f, prob_tree); if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0 ) return EXIT_FAILURE; atexit(SDL_Quit); field[0] = malloc(WIDTH*HEIGHT); if (field[0] == NULL) exit(EXIT_FAILURE); field[1] = malloc(WIDTH*HEIGHT); if (field[1] == NULL) { free(field[0]); exit(EXIT_FAILURE); } scr = SDL_SetVideoMode(WIDTH, HEIGHT, BPP, SDL_HWSURFACE|SDL_DOUBLEBUF); if (scr == NULL) { fprintf(stderr, "SDL_SetVideoMode: %s ", SDL_GetError()); free(field[0]); free(field[1]); exit(EXIT_FAILURE); } init_field(); tid = SDL_AddTimer(TIMERFREQ, simulate, NULL); // suppose success running = true; event->type = SDL_VIDEOEXPOSE; SDL_PushEvent(event); while(SDL_WaitEvent(event) && !quit) { switch(event->type) { case SDL_VIDEOEXPOSE: while(SDL_LockSurface(scr) != 0) SDL_Delay(1); show(scr); SDL_UnlockSurface(scr); SDL_Flip(scr); event->type = SDL_VIDEOEXPOSE; SDL_PushEvent(event); break; case SDL_KEYDOWN: switch(event->key.keysym.sym) { case SDLK_q: quit = true; break; case SDLK_p: if (running) { running = false; pthread_mutex_lock(&synclock); SDL_RemoveTimer(tid); // ignore failure... pthread_mutex_unlock(&synclock); } else { running = true; tid = SDL_AddTimer(TIMERFREQ, simulate, NULL); // suppose success... } break; } case SDL_QUIT: quit = true; break; } } if (running) { pthread_mutex_lock(&synclock); SDL_RemoveTimer(tid); pthread_mutex_unlock(&synclock); } free(field[0]); free(field[1]); exit(EXIT_SUCCESS); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.