Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <time.h>
- #define LEADER 0
- bool been_in_room[100];
- bool light = false;
- inline int pick(){
- int n = (int)random() % 100;
- printf("Prisoner %d is picked.\n",n);
- return n;
- }
- int main(){
- int i,p;
- srandom(time(NULL)); //set seed to current unixtime
- //main program loop
- for(i=0;i<100;i++){
- start:
- p = (int)random() % 100;
- if(p == LEADER){
- printf("Prisoner %02d (LEADER) is picked.\n",p);
- if(light == true){
- printf("The leader turns the light off.\n");
- light = false;
- continue;
- } else {
- goto start;
- }
- } else {
- printf("Prisoner %02d is picked.\n",p);
- }
- if(been_in_room[p] == true){
- //he's been here before, walk out.
- printf(" Prisoner %d has been in the room.\n",p);
- goto start;
- } else {
- //he hasn't been here before, light on?
- if(light == true){
- printf(" Prisoner %02d has not been in the room, but the light is on.\n",p);
- goto start;
- } else {
- light = true;
- printf(" Prisoner %02d turns the light on.\n",p);
- goto start;
- }
- }
- }
- printf("Prisoner %02d declares that all prisoners have been in the room.\n",LEADER);
- for(i=0;i<100;i++){
- if(been_in_room == false){
- printf("they are all killed. Prisoner %d has not been in the room.\n",i);
- exit(1);
- }
- }
- puts("Good job!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement