Sorry to bump, but im really running short on time and I'm SO close to finishing! If anyone can help me out soon, that would be amazing.
Code so far:
Code:
#include <stdio.h>#include <string.h>
int check_guess(int length, char guess, char word[])
{
int i;
for(i=0; i<length; i=i+1)
{
if(word[i] == guess)
{
return 1;
break;
}
else
{
return 0;
break;
}
}
}
void draw_guess(int length, char guess, char word[]) //the function draws the unguessed letters (dashes) and updates it when guessed correctly
{
//Draws the dashes
int i;
static int startup = 0;
static char display[7];
//Updates the dashes with the properly guessed letters
if(startup == 0)//only runs the first time
{
for(i=0; i<length; i=i+1)
{
display[i] = '-';
}
printf("%s\n", display);
}
else//after the first time this starts printing with correctly guessed letters
{
for(i=0; i<length; i=i+1)
{ if(word[i] == guess)
{
display[i] = guess;
}
}
printf("%s\n", display);
}
startup = 1; //changes startup so that it can skip the first loop
}
int main(void)
{
int length, wrong, correct, position;
char guess;
char word[7], prevguess[17];
wrong = 0;
correct = 0;
printf("Welcome to HANGMAN!\n");
printf("Please pick a word (3 to 6 letters): ");
scanf("%s", word);
length = strlen(word); //just takes the length of the word entered
while(length > 6 || length < 3)
{
printf("Your word cannot be less than 3 or more than 6 letters\n");
printf("Please pick a word (3 to 6 letters): ");
scanf("%s", word);
}
printf("You are guessing a word that is %d letters long.\n", length);
draw_guess(length, guess, word);//draws initial dashes
while(wrong != 10) //keeps going for 10 incorrect guesses
{
if(correct == length)
break;
if(check_guess(length, guess, word) == 1)
{
draw_guess(length, guess, word); //SHOULD draw the initial dashes once, than afterwards begin printing the updated ones
correct = correct + 1;
}
else wrong = wrong + 1;
printf("Make a guess (1 letter): ");
scanf(" %c", &guess);
position = strlen(prevguess); //this just sets up the string containing guesses
prevguess[position] = guess;//adds the guess into the string
wrong = wrong + 1; //adds to the incorrect guess count
printf("Your guesses: %s\n", prevguess);//shows the previous guesses
printf("Incorrect guesses: %d\n", wrong);//tells user how many times they have guessed wrong
}
return 0;
}
Now in the first "check_guess" function, I am aware that the return 0 is inside the for loop and therefore will most certainly return a zero (unless the player guesses the last letter). I'm having difficulty figuring out how to run the whole for loop and then return a zero if the guess was not in the string at all.
Also in the main function I need help figuring out why my "previous guesses" string won't work. I copied this from a TA but they were hasty and I probably forgot something, but I can't figure out what.
If I could just get help with these spots it would be great,
Thanks!
(p.s. I promise this is the last assignment/headache I subject you all to!)