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
/*
 * Play one game of Hangperson.  The secret word is passed as a
 * parameter.  The function should return true if the player won,
 * and false otherwise.
 */
bool one_game(const char *word) {
    
    int num_correct = 0;
    char* guess;
    int num_missed = 0;
    int word_len = strlen(word);
    int already_guessed_count = 0;
    char already_guessed[7 + word_len]; //7 is the number of misses possible before game over + the number of individual letters that could be in the word (i.e. word_len)
    memset(already_guessed, '\0', ((7 + word_len)*sizeof(char)));

    char displayed_word[(word_len*2)+1]; //creates an array of nicely spaced blanks (_) 
    for(int i = 0; i < word_len*2; i+=2){
        displayed_word[i] = '_';
        displayed_word[i+1] = ' ';}
    displayed_word[word_len*2] = '\0';

    printf("%d\n", word_len);

    while (true){     //game loop, run this until you lose... or win
        printf("Number missed: %d\n", num_missed);
        print_gallows(num_missed);
        printf("%s\n", displayed_word);

        if(already_guessed[0] == '\0'){
            printf("Already guessed: (none)\n");
        } else{
            printf("Already guessed: %s\n", already_guessed);
            free(guess);
        }
        guess = user_guess(already_guessed); //Working on getting user input. Tryed to make a new function to make it cleaner, but couldn't get it to work...everything above this line in one_game works though. I created a array of the letters that have been guessed (already_guessed) as well as an array w/ blank spaces(displayed word) where the letters will if they are correct! 
        already_guessed[already_guessed_count] = guess[0];
        already_guessed[already_guessed_count+1] = '\0';
        already_guessed_count++;
        
        if (strcasestr(word, &(guess[0]))){ //guess is correct
            num_correct += update_correct(displayed_word, guess, word); //function to update displayed_word
            //printf("Number of correctly guessed letters: %d\n", num_correct);
        }
        else{
            num_missed++;
        }
        printf("----------------------------------------\n\n\n");
        if (num_missed == 7){
            printf("Better luck next time, chump.\n");
            return true;
        }
        else if (num_correct == strlen(word)){
            printf("Alright wiseass, you win this round.\n");
            return true;
        }
    }
    return true; 
}
Esempio n. 3
0
/*
 * This function prints out at the initial start of the game and after
 * each guess made by the user so they can see the gallows,
 * the letters they have already guessed, how many misses they
 * have, and how close they are to guessing the correct word.
 */
void print_state(int num_missed, char secret[], char guessed[]) {
    printf("Missed: %d\n", num_missed);
    print_gallows(num_missed);
    for (int i = 0; i < strlen(secret); i++) {
        printf("%c ", secret[i]);
    }
    printf("\n");
    if (strlen(guessed) == 0) {
        printf("Already guessed: (none)\n");
    } else {
        printf("Already guessed: %s\n", guessed);
    }
    printf("What is your guess? ");
    return;
}
Esempio n. 4
0
/*
 * Play one game of Hangperson.  The secret word is passed as a
 * parameter.  The function should return true if the player won,
 * and false otherwise.
 */
bool one_game(const char *word) {
    char wordLength = get_length(word);
    char num_missed = 0;
    bool game_over = false;
    char input[1024];


    // Create a string containing the secret word
    char secretWord[wordLength+1];
    strcpy(&secretWord, word);
    printf("The secret word is: %s\n",secretWord);
    printf("Length of word is: %d\n", wordLength);

    // Create a string representing the guessing state
    char currWord[wordLength+1];
    for (char i = 0; i < wordLength; i++) {
        currWord[i] = '_';
    }
    currWord[wordLength] = '\0';  
    
    // Create an array to track guessed character
    char guessedChar[27];
    guessedChar[0] = '\0';

    while (!game_over) {
        bool good_input = false;
        print_gallows(num_missed);
        printf("%s\n", currWord);
        char guess = 'a';
        while (!good_input) { // Loop until a single alphabet char is typed in
            print_prompt(guessedChar);
            fgets(input, 1024, stdin);
            if (feof(stdin)) {
                clearerr(stdin);
                print_tryagain();
                continue;
            } else if (input[0] == '\0' || input[1] != '\n') {
                print_tryagain();
                continue;
            } else if (!isalpha(input[0])) {
                print_tryagain();
                continue;
            } else {
                guess = toupper(input[0]);
                if (in_str(guess, &guessedChar)) {
                    printf("You already guessed %c\n", guess);
                    continue;
                }
                good_input = true;
            }  
        } 

       
        append(guess, &guessedChar);
        if (in_str(guess, &secretWord)) {
            printf("Good guess.\n");
            update_curr(guess, &secretWord, &currWord);
            if (is_guessed(&currWord)){
                game_over = true;
                break;
            }
        } else {
            printf("Bad guess.\n");
            num_missed += 1;
            if (num_missed >= 7) {
                game_over = true;
                print_gallows(num_missed);
                break;                        
            }
        }
        
        printf("Missed: %d\n",num_missed);              
        good_input = true; 


    }
    
    if (num_missed >= 7) {
        printf("You lost.\n");
    } else {
        printf("You won.\n");
    }
    
    printf("The word is: %s\n", secretWord);
}
Esempio n. 5
0
//////////////////////////Vòng lặp chính của trò chơi///////////////////////////////////
int play_loop(int player)
{
    char a[100],*word_guess,res;
    int ligne, ligne_random, l_word=0, end=0,select,i=0;
    if (player == 1) // 1 player
    {
        ligne = count_ligne(a,word_guess);
        srand(time(NULL));
        ligne_random = rand()%ligne+0;  //Chọn 1 hàng ngẫu nhiên trong file
        l_word = get_word_guess(a,word_guess,ligne_random,word,l_word);
    }
    else // 2 player
    {
        system("cls");
        do {
            printf("\n\n\n\t\tPlayer1 enter a word ! (more than 2 letters) : \n");
            printf("\t\t");scanf("%s",&a);

            for (i=0;a[i]!= '\0' ; i++)
                l_word++;
            if (l_word < 2) printf("\n\t\tWrong type ! Enter again");
        } while (l_word < 2);
        for (i=0;a[i]!= '\0' ; i++)
        {
            word[i] = a[i];
            flip[i] = 0;
        }
    }
    do
    {
        system("cls");
        print_gallows(end);
        printf("\n\t");print_guess(l_word);
        printf("\n\n\tMisses: "); print_misses(i);
        if (check_correct(l_word) == 1) break; //Kết thúc game
        if (end < 7)
        {
            menu_play();
            scanf("%d",&select);
            switch(select)
            {
                case 1:
                    printf("\n\n\tGuess a letter! : ");
                    scanf("%s",&res);
                    if(check_lettre(l_word,res) == 0)
                    {
                        end++;
                        miss[i] = res;
                        i++;
                    }
                    else if (check_lettre(l_word,res) == 2)
                    	end++;
                    break;
                case 2:
                    end = 7;
                    break;
                case 3:
                    play_loop(player);
                    return 0;
                    break;
                case 4:
                    return 0;
            }
        }
        else break;
    } while(end < 8);
    // Hiện kết quả
    system("cls");
    print_gallows(end);
    if (check_correct(l_word)==1)
    {
        printf("\n\n\n\t\tYou WIN ! \n");
        solution(l_word);
    }
    else
    {
        printf("\n\n\n\t\tYou LOSE ! \n");
        solution(l_word);
    }
    printf("\n\t\t1.New \t2.Quit \t3.Menu\n");
    printf("\t\tSelect : ");
    scanf("%d",&select);
    switch(select)
    {
        case 1:
            play_loop(player);
            return 0;
            break;
        case 2:
            break;
        case 3:
            return 0;
    }
    return 1;
}