phrase guess game in C

I'm trying to create a code with these prospects:

Program that simulates a simple word-guessing game, similar to Hangman (without the pictures) or television’s Wheel of Fortune (without the money and prizes). The idea is that the program will “think” of a secret phrase, and the user will try to discover the phrase by guessing one letter at a time.
The program will use a single secret phrase that it uses each time the program runs. It can be any phrase, as long as it contains at least 15 characters, including at least one space (i.e., multiple letters).
• After each guess, the program should show the current status of the user’s guesses, using dashes to indicates unguessed letters. For example, if the secret phrase is 'magical mystery tour' and the user has already guessed the letters b, y, and t, then the program should display:
------- -y-t--y t---
Any spaces in the phrase should automatically be included (i.e., the user doesn’t have to guess them).
• Keep track of the number of guesses the user makes. (If a user re-guesses a letter previously guessed, the new guess is still counted.)
• If, after the 20th guess, the user still hasn’t completely discovered the phrase, the program should display a consolation message and end the game. The consolation message should include the secret phrase.
• If the user discovers the secret phrase by the 20th guess, display a congratulatory message, along with the number of guesses the user took.

I am not sure how to cover the last bullet and how to end the game when the player guessed right before 20 times. any help would be greatly appreciated! Thank you!

my code so far:
#include "stdafx.h"


int main(void)
{
char guess;
char phrase[22] = { 'i',' ', 'l','o','v','e',' ','f','r','i','e','d',' ','c','h','i','c','k','e','n','\0' }; // Initialization of variables
char dashedArray[22] = { '-',' ','-','-','-','-',' ','-','-','-','-','-',' ','-','-','-','-','-','-','-','\0' };
int totalTries = 20;
int currentTries = 0;
int i, j;
int count = 0;

printf("\nLet's play a game! The objective of the game is to guess my phrase. You will have twenty chances to guess.\n");
printf("\nLet's get started!\n");

for (i = 0; i < totalTries; i++)
{
printf("\nPlease enter a letter: ");
scanf_s(" %c\n", &guess);
for (j = 0; j < 22; j++)
{
if (guess == phrase[j])
dashedArray[j] = guess;
count++;
}

printf("Result: %s", dashedArray);
}

printf("\n");
printf("You lose. The secret phrase is i love fried chicken.\n");

return 0;
}

Last edited on
you need a way out of the loop if they win.
so you need to do some more work to get that last bullet..
- detect if they have won. This may simply be checking dashed array to see if it has any dashes left. There are other ways, that one is simple.

-if they won, exit the loop. this can be as simple as adding another condition to your loop.

a small example of that:

char won = 0;
for(x = 0; x < numtries && !won; x++)
{
if(stuff)
won = 1; //this will kill the loop.
}

check your loop variable after they win or lose. If they win, that will either be the # of tries or off by one, I am having a moment here... if it is off by one, fix it when you print, if it is right, just print it, but regardless, that can be used to get the answer.

Last edited on
Topic archived. No new replies allowed.