Esempio n. 1
0
bool one_game(const char *word) {
    int secret_len = strlen(word);
    int num_missed = 0;
    char secret[secret_len];
    for (int i = 0; i < secret_len; i++) {
        secret[i] = 95;
    }
    secret[secret_len] = '\0';
    char guessed[27] = "";
    while (num_missed < 7) {
        print_state(num_missed, secret, guessed);
        char guess[128];
        fgets(guess, 128, stdin);
        guess[strlen(guess) - 1] = '\0';
        int resultguess = check_guess(guess, guessed, word);
        if (resultguess == 2) {
            guess[0] = toupper(guess[0]);
            strcat(guessed,guess);
            good_turn(guess,word,secret);
        }
        else if (resultguess == 1) {
            num_missed++;
            guess[0] = toupper(guess[0]);
            strcat(guessed, guess);
        }
        if (game_outcome(secret,word)) {
            return true;
        }
    }
    print_gallows(7);
    printf("Sorry, you've lost...The word was %s.\n", word);
    return false;
}
Esempio n. 2
0
/**
 * @brief Program entry point.
 * @param argc The argument count
 * @param argv The argument vector
 * @return EXIT_SUCCESS, EXIT_PARITY_ERROR, EXIT_MULTIPLE_ERRORS
**/
int main(int argc, char *argv[])  
{
    uint8_t client_count = 0;
    int shmfd;
    
    
    /* setup signal handlers */
    const int signals[] = {SIGINT, SIGTERM};
    struct sigaction s;

    s.sa_handler = signal_handler;
    s.sa_flags   = 0;
    if(sigfillset(&s.sa_mask) < 0) {
        bail_out(EXIT_FAILURE, "sigfillset");
    }
    for(int i = 0; i < COUNT_OF(signals); i++) {
        if (sigaction(signals[i], &s, NULL) < 0) {
            bail_out(EXIT_FAILURE, "sigaction");
        }
    }
    
    /* Handle arguments */
    if (argc > 0) {
       progname = argv[0]; 
    }
    
    if (argc > 2) {
        fprintf(stderr, "USAGE: %s [input_file]", progname);
        exit(EXIT_FAILURE);
    }

    switch (argc) {
    /* Handle dict input from stdin */
    case 1: 
        printf("Please input your dictionary: \n");
        in_stream = stdin;
        read_dict();
    break;
    /* Handle dict input from file */
    case 2: 
		if ((in_stream = fopen(argv[1], "r")) == NULL)
		{
            bail_out(EXIT_FAILURE, "Invalid File");
		}
        read_dict();
    break;
    default:
        fprintf(stderr, "USAGE: %s [input_file]", progname);
        exit(EXIT_FAILURE);
        break; 
    }

    /* Create a new Shared Memory Segment (shm_open) */
    shmfd = shm_open(SHM_NAME, O_RDWR | O_CREAT | O_EXCL, PERMISSION);

    if (shmfd == (-1)) {
        bail_out(EXIT_FAILURE, "shm_open failed");
    }
    
    /*  Truncate a file to a specified length 
        extend (set size) */ 
    if (ftruncate(shmfd, sizeof *shared) == -1) {
        (void) close(shmfd);
        bail_out(EXIT_FAILURE, "ftruncate failed");
    }

    /* Map shared memory object */
    shared = mmap(NULL, sizeof *shared,
                    PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);

    if (shared == MAP_FAILED) {
        (void) close(shmfd);
        bail_out(EXIT_FAILURE, "MMAP failed");
    }

    if (close(shmfd) == -1) {
        bail_out(EXIT_FAILURE, "close(shmfd) failed");
    }
    
    shared->sc_terminate = -1;

    /* Create new named semaphores */
    s_server = sem_open(S_SERVER, O_CREAT | O_EXCL, PERMISSION, 0);
    s_client = sem_open(S_CLIENT, O_CREAT | O_EXCL, PERMISSION, 1);
    s_return = sem_open(S_RETURN, O_CREAT | O_EXCL, PERMISSION, 0);
    
    if(s_server == SEM_FAILED || 
        s_client == SEM_FAILED || 
        s_return == SEM_FAILED) {
        bail_out(EXIT_FAILURE, "sem_open(3) failed"); 
    }

    sem_set = 1;

    struct ClientList *el_pre = NULL;
    struct ClientList *el_cur = NULL;
    
    /* Keep server open until it gets killed. */
    while (!want_quit) {
        /* Critical section entry. */
        /* Wait until server is allowed to access SHM. */
        if (sem_wait(s_server) == -1) {
            if(errno == EINTR) continue;
            bail_out(EXIT_FAILURE, "sem_wait(3) failed");
        }
        
        /* Setup data for existing client. */
        if (shared->s_id >= 0) {
            el_cur = client_list;
            while (el_cur != NULL && el_cur->server_id != shared->s_id) {
                printf("elpre\n");
                el_pre = el_cur;
                printf("elcur\n");
                el_cur = el_cur->next;
            }

            if (el_cur == NULL) {
                bail_out(EXIT_FAILURE, "Client does not exist");
            }

            el_cur->guess = shared->c_guess;
        }
        
        /* Setup new client to list. */
        if (shared->s_id == -1) {
            /* Allocate element and set to zero. */
            el_cur = 
                 (struct ClientList *) calloc (1, sizeof(struct ClientList)); 
            if (el_cur == NULL) {
                bail_out(EXIT_FAILURE, "calloc(3) failed");
            }

            /* Assign unique ID based on client count. */
            el_cur->server_id = client_count++;
            el_cur->game_count = 0;
            
            /* Add the current element to our list. */
            el_cur->next = client_list;
            client_list = el_cur;
        }
        
        /* Check if client has terminated. */
        if (shared->sc_terminate >= 1) {
            DEBUG("Terminating client...\n");
            /* Remove client from list. */
            if (client_list == el_cur) {
                client_list = el_cur->next;
            } else {
                el_pre->next = el_cur->next;
            }

            free(el_cur);
            /* Free allocated resources. */
            /* Reset sc_terminate. */
            shared->sc_terminate = -1;
            if (sem_post(s_client) == -1) {
                bail_out(EXIT_FAILURE, "sem_post(3) failed");
            }
            /* Skip the rest of the game as we have nothing to do here */
            continue;
        }

        /* Check game status of client. */
        if (shared->status_id == CreateGame) {
            /* Start a new game. */
            DEBUG("Setting up game for client %d\n", shared->s_id);
            create_game(el_cur);
            
        }

        if (shared->status_id == Running) {
            /* Check guess. */
            DEBUG("Checking guess of client %d\n", shared->s_id);
            check_guess(el_cur);
        }
        
        /* Write server answer back into SHM. */
        shared->s_id = el_cur->server_id;
        shared->status_id = el_cur->status_id;
        shared->s_errors = el_cur->errors;
        strncpy(shared->s_word, el_cur->client_word, MAX_DATA);
        
        /* Let the client know that there is an answer. */
		if (sem_post(s_return) == -1) { 
            bail_out(EXIT_FAILURE, "sem_post(3) failed");
        }

        /* critical section end. */

    }
    /* Free stuff */
    free(strings);
}