简体   繁体   中英

Hangman Program Help (Intro to C Programming)

I have spent many hours working on this program that is supposed to play a game of hangman.

The assignment is as follows: In this modified game of Hangman, the computer chooses a secret word and the player must guess letters in the word. The secret word is displayed as a series of *'s (the number of *'s displayed indicates the number of letters in the word). Each time the player guesses a letter in the word, the corresponding *'s are replaced with the letters correctly guessed so far. The game ends when the player correctly guesses the entire word (player wins) or when the player uses up all of his turns (player loses). The player will be allowed a maximum of 7 incorrect guesses.

I have come pretty far with it, but feel like I am messing up in several elementary places. I am trying to debug it, but cannot get past the part where I get an error in the main function every time I pass the function 'findChars' saying that it "makes pointer from integer without a cast in argument 2."

I apologize for all the reading, but any help would be great. Thank you.

<

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <time.h> /* contains prototype for function time */
#define MAX 10


int findChars(char* gameWord[], char secretWord[], int length);

int main (void) {
const int numberOfWords = 20;
int length;
srand((unsigned)time(NULL)); //generate a random seed based on time so it's different every time
int ran = rand()% numberOfWords; //Generate a random number between 0 to numberOfWords - 1
char* dictionary[] = {"who", "lives", "in","a", "pineapple", "under","the", "sea", "absorbant",
            "and", "yellow", "porous","is", "he", "sponge","bob", "square","pants","crabby","patties"}; //array of word strings

printf("%s\n", dictionary[ran]);

printf("Welcome to HANGMAN.\n\n You will be asked to guess the computer's secret word. The word will be displayed as a number of *'s.\n Every time you guess a letter correctly, that letter will be shown in its correct position in the word.  \nIf you guess incorrectly, the number of tries you have left will be decremented.  \nYou will be given a maximum of 7 incorrect guesses.\n");

length=strlen(dictionary[ran]);
printf("%d\n", length);
char secretWord[MAX];
secretWord[length]=*dictionary[ran];

char* gameWord[length];

int i;
for (i=0; i<length; i++){
    gameWord[i]= "*";
}

for (i=0; i<length; i++){
    printf("%s", gameWord[i]);
}
printf("\n");
printf("7 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("6 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("5 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("4 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("3 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("2 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("1 turns left \nEnter a letter:    \n");

while(findChars(&gameWord[length], secretWord[length], length)!=0) {
    (findChars(&gameWord[length], secretWord[length], length));
}
    printf("Sorry, no more turns left. The secret word was ???");

return 0;
}

//PRE: findChar inputs the character we are looking for, the string we are looking in.
//POST: the function outputs the number of occurances of the said character in the said array

int findChars(char* gameWord[],char secretWord[], int length) {
int i;
char character[MAX];
    while((getchar()) != '\n'){
        character[0]=getchar();
        for (i=0; i<length; i++){
            if (secretWord[i]==character[0]){
                strncpy(gameWord[i],secretWord[i],1);
                for (i=0; i<length; i++){
                    printf("%s", gameWord[i]);
                    return 1;
                }
            }
            else
                return 0;
        }
    return 0;
    }
return 0;
}

>

Try changing:

char secretWord[MAX];
secretWord[length]=*dictionary[ran];

and

char* gameWord[length];

to

char* secretWord = dictionary[ran];

and

char gameWord[length];

Right now your only assigning the first character of dictionary[ran] to the character at position of length in secretWord.

(If you're goint to print gameWord you'll also have to allocate space and set a null terminator for it).

Then change

findChars(&gameWord[length], secretWord[length], length)

to:

findChars(gameWord, secretWord, length)

as you right now are passing a char to a function expecting a char* . You'll also need to change the signature of findChars to:

int findChars(char* gameWord, char* secretWord, int length)

There's a lot of other things to object to, but this should get you started. Things to look at:

  • Putting the seven while(findChars... into a loop
  • Don't use strncpy(gameWord[i],secretWord[i],1); to assign one char from one string to another
  • Incorrect printf formatting
  • The for (i=0; i<length; i++){ -loop in findChars returns in the first iteration

I think there's a problem in how you specify the parameters for findChars .

Basically, where you have calls like findChars(&gameWord[length], secretWord[length], length) , I think you need calls like findChars(gameWord, secretWord, length) .

The prototype would be...

int findChars(char gameWord[], char secretWord[], int length);

or...

int findChars(char* gameWord, char* secretWord, int length);

rather than...

int findChars(char* gameWord[], char secretWord[], int length);

That is, both gameWord and secretWord should either be passed as arrays or as pointers, but not as arrays of pointers.

Similarly, when you declare gameWord in main , it should be an array of characters - not an array of pointers to characters...

char gameWord [length+1];

I'm a little concerned about that because length is a variable, but I think it's OK - my C is a bit dated, and my automatic thing is to declare array sizes using compile-time constant expressions (the largest size you might need).

BTW - note the +1 . AC string has an extra character - a null terminator - which isn't counted by strlen - you have to allow for that when declaring the char-array variable, though.

Try to compare how the function is declared to how the function is being called, in order to understand the error messages.

The findChars() function is being declared like this:

int findChars(char* gameWord[], char secretWord[], int length);

and being called from main() like this:

findChars(&gameWord[length], secretWord[length], length)

Note in particular that the second argument is being declared as a char array (a string), while you are passing into it a single character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM