Reputation: 23
Beginner in C programming here, i'm currently working on a standard Hangman game. I've encountered a problem which i can't figure out a way to tackle due to my limited knowledge. The game doesn't seem to restart with the initial values that i set at the start of them game when the user gets asked if they want to play a new game. Instead of counting from 5 and down (like when i run the program) it instead counts down from 0 and down (-1,-2,-3 etc) without a limit. Also a new random word doesn't get spawned whenever the user accepts a new game. It just continues on with the same word from the previous game, any suggestions on how i can solve this? Thank you in advance.
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main(){
srand(time(NULL));
char guessWords[][10] = {
"chocolate",
"tea",
"cream",
"cats",
"pizza",
"phone"
};
int randomIndex = rand() % 6;
int numGuess = 5;
int numCorrect = 0;
int oldCorrect = 0;
int lengthofWord = strlen(guessWords[randomIndex]);
int loopIndex = 0;
int letterGuessed[10] = {0,0,0,0,0,0,0,0,0};
char guess[10];
int letterEnter;
char play;
int quit = 0;
printf("guessWords:%s randomIndex:%d lengthofWord:%d\n",
guessWords[randomIndex],
randomIndex,
lengthofWord);
do{
while(numCorrect < lengthofWord){
printf("\n\n New round...\nHangman word: ");
for(loopIndex = 0; loopIndex < lengthofWord; loopIndex++){
if(letterGuessed[loopIndex] == 1){
printf("%c",guessWords[randomIndex][loopIndex]);
}
else{
printf("-");
}
}
printf("\n");
printf("Numbers Correct:%d\n", numCorrect);
printf("Guess a letter: ");
fgets(guess, 10, stdin);
if(strncmp(guess, "quit", 4)== 0){
quit = 1;
break;
}
letterEnter = guess[0];
printf("Letter Entered: %c",letterEnter);
oldCorrect = numCorrect;
for(loopIndex = 0; loopIndex < lengthofWord; loopIndex++){
if(letterGuessed[loopIndex] == 1){
continue;
}
if(letterEnter == guessWords[randomIndex][loopIndex]){
letterGuessed[loopIndex] = 1;
numCorrect++;
}
}
if(oldCorrect == numCorrect){
numGuess--;
printf("\nSorry, wrong guess!\n");
printf("You have %d more guesses to go.\n",numGuess);
if(numGuess == 0){
break;
}
}
else{
printf("\nCorrect Guess!\n");
printf("You have %d more guesses to go.\n",numGuess);
}
}
if(quit == 1){
printf("The user quit.\n");
}
else if(numGuess == 0){
printf(" ______\n");
printf(" | |\n");
printf(" (_) |\n");
printf(" /|\\ |\n");
printf(" | |\n");
printf(" / \\ |\n");
printf(" _____|");
printf("\nYou are hanged! The correct word is: %s",
guessWords[randomIndex]);
}
else{
printf("You Win!\n");
}
printf("\nDo you wanna play again?(y/n): \n");
scanf("%c", &play);
}while(play == 'y');
return 0;
} ```
Upvotes: 1
Views: 61
Reputation: 71626
int main()
{
int numCorrect = 0;
do //each new game starts here
{
while(numCorrect < lengthofWord)
{
}
} while(play == 'y');
return 0;
}
This is quite easy to figure out yourself, you don't need us. As Paul indicated in comments. Show for example where within your do loop you re-initialize numCorrect to zero for each new game?
If you don't set the variables for a new game then numCorrect will always match lengthofWord after the first (winning) game, yes? And keep kicking you out to do you want to play again?
There are multiple ways to solve this:
int main()
{
int numCorrect;
do //each new game starts here
{
numCorrect = 0;
while(numCorrect < lengthofWord)
{
}
} while(play == 'y');
return 0;
}
or see other answer (Majkl) or other solutions.
Upvotes: 0
Reputation: 153
Instead of reinitializing every value again, I would recommend putting your game in a function, you could write something like this:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int fun()
{
srand(time(NULL));
char guessWords[][10] = {
"chocolate",
"tea",
"cream",
"cats",
"pizza",
"phone"
};
int randomIndex = rand() % 6;
int numGuess = 5;
int numCorrect = 0;
int oldCorrect = 0;
int lengthofWord = strlen(guessWords[randomIndex]);
int loopIndex = 0;
int letterGuessed[10] = {0,0,0,0,0,0,0,0,0};
char guess[10];
int letterEnter;
char play;
int quit = 0;
printf("guessWords:%s randomIndex:%d lengthofWord:%d\n",
guessWords[randomIndex],
randomIndex,
lengthofWord);
while(numCorrect < lengthofWord){
printf("\n\n New round...\nHangman word: ");
for(loopIndex = 0; loopIndex < lengthofWord; loopIndex++){
if(letterGuessed[loopIndex] == 1){
printf("%c",guessWords[randomIndex][loopIndex]);
}
else{
printf("-");
}
}
printf("\n");
printf("Numbers Correct:%d\n", numCorrect);
printf("Guess a letter: ");
fgets(guess, 10, stdin);
if(strncmp(guess, "quit", 4)== 0){
quit = 1;
break;
}
letterEnter = guess[0];
printf("Letter Entered: %c",letterEnter);
oldCorrect = numCorrect;
for(loopIndex = 0; loopIndex < lengthofWord; loopIndex++){
if(letterGuessed[loopIndex] == 1){
continue;
}
if(letterEnter == guessWords[randomIndex][loopIndex]){
letterGuessed[loopIndex] = 1;
numCorrect++;
}
}
if(oldCorrect == numCorrect){
numGuess--;
printf("\nSorry, wrong guess!\n");
printf("You have %d more guesses to go.\n",numGuess);
if(numGuess == 0){
break;
}
}
else{
printf("\nCorrect Guess!\n");
printf("You have %d more guesses to go.\n",numGuess);
}
}
if(quit == 1){
printf("The user quit.\n");
}
else if(numGuess == 0){
printf(" ______\n");
printf(" | |\n");
printf(" (_) |\n");
printf(" /|\\ |\n");
printf(" | |\n");
printf(" / \\ |\n");
printf(" _____|");
printf("\nYou are hanged! The correct word is: %s",
guessWords[randomIndex]);
}
else{
printf("You Win!\n");
}
printf("\nDo you wanna play again?(y/n): \n");
scanf("%c", &play);
if(play == 'y') return 1;
return 0;
}
int main()
{
int flag = 1;
while(flag)
{
flag = fun();
}
return 0;
}
You should always focus on the use of functions, they can really help you in many ways (spare your time, prevent unnecessary code duplication, make your code more readable...).
Upvotes: 3