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:
and
to
and
Right now your only assigning the first character of
dictionary[ran]to the character at position oflengthin secretWord.(If you’re goint to print
gameWordyou’ll also have to allocate space and set a null terminator for it).Then change
to:
as you right now are passing a
charto a function expecting achar*. You’ll also need to change the signature offindCharsto:There’s a lot of other things to object to, but this should get you started. Things to look at:
while(findChars...into a loopstrncpy(gameWord[i],secretWord[i],1);to assign one char from one string to anotherprintfformattingfor (i=0; i<length; i++){-loop infindCharsreturns in the first iteration