I have this source code of my program here that is kind of like the game hangman
ID: 3831537 • Letter: I
Question
I have this source code of my program here that is kind of like the game hangman. The program runs, however, it does not track the number of guess that I've made already. I think the biggest problem is with my "checkguess()" function. I've bolded the parts the parts that would contain error and would need change. The non-bolded areas should be correct.
/*
* gameFunctions.c
* Contains code used to begin and run a game of 'horse'.
* The idea of the game is to guess letters in a word until
* the number tries to guess the word is exceeded or the word
* is found.
*
*/
#include "gameFunctions.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
//Begins game execution, manages user I/O
void startGame(char word[25])
{
int won = 0; //Flag to see if the user has won yet
int numBadGuesses = 0; //Counter to end the game on a lose condition
int possibleBadGuesses; //Total number of bad guesses allowed
int charRevealed; //Flag to see if the user guessed a good letter
char guess; //The user's guess
char revealedLetters[25]; //What the user has revealed so far
//Initializes the guessing array to all underscores
initializeBlankString(strlen(word), revealedLetters);
clearScreen();
//Gets the total number of chances
printf("Please enter the total number of incorrect guesses you would like to be able to make: ");
scanf("%d", &possibleBadGuesses);
printWithSpaces(revealedLetters);
//Runs the game loop until the number of tries are exhausted or the word is found
while (numBadGuesses <= possibleBadGuesses && !won)
{
printf("Enter a letter to guess: ");
scanf(" %c", &guess);
//Updates the revealed letters and checks to see if the user won
charRevealed = revealGuessedLetter(word, revealedLetters, guess);
won = checkGuess(word, revealedLetters);
//Increments bad guesses if the last guess was a miss
if (!charRevealed)
{
numBadGuesses++;
}
//Outputs game information to the user
drawHorse(numBadGuesses, possibleBadGuesses);
printWithSpaces(revealedLetters);
}
if (won)
printf("Congratulations! You correctly guessed the word %s ", word);
else
printf("You've run out of guesses. The correct word was %s ", word);
}
/*IMPLEMENT YOUR FUNCTIONS HERE*/
void initializeBlankString(int numLetter, char revealedLetters[25]) {
int i;
for(i = 0; i < numLetter; i++) {
revealedLetters[i] = '_';
}
revealedLetters[numLetter] = '';
}
void printWithSpaces(char revealedLetters[25]) {
int i;
for(i = 0; i < (strlen(revealedLetters)); i++) {
printf("%c ", revealedLetters[i]);
}
printf(" ");
}
int revealGuessedLetter(char *str, char *fname, char c) {
int i=0;
while (str[i] != '') { //looping till end of fname
if(str[i] == c)
fname[i] = c;
i++;
}
return 1;
}
int checkGuess(const char *a, const char *b){
if(a = b) {
return 0;
} else {
return 1;
}
}
//Draws part of the horse pending on how many guesses have been made so far
//Horse grabbed from: http://www.virtualhorses.com/graphics/asciiart.htm
//And no, I don't know why there's an entire site dedicated to virtualhorses =/
void drawHorse(int guessedSoFar, int allowedGuesses)
{
//The horse! Duh!
char horsey[14][29] = {
{" (\(\_\"},
{" _.o o`\\\\"},
{"('_ ))))"},
{" '---' (((( .=,"},
{" ) )))___.-''-./=;\\\"},
{" / ((( \ ))))"},
{" ; | |//"},
{" /\ | | (("},
{" ( \ /__.-'\ / )"},
{" / /` | / \ \ |"},
{"/ / \ ; \ (\ ("},
{"\\_ || || ||"},
{" \_] || || ||"},
{" /_< /_</_< "}};
clearScreen();
//Determines how much of the horse to print and prints it
double ratio = (double)guessedSoFar / (double)allowedGuesses;
int linesToDraw = floor(ratio * 13);
linesToDraw = linesToDraw <= 14 ? linesToDraw : 14;
int i;
for (i = 14 - linesToDraw; i < 14; i++)
{
printf("%s ", horsey[i]);
}
}
//Clears the terminal screen
void clearScreen()
{
//Some UNIX hackary to clear the terminal. Makes this not portable to some systems,
//but should work fine on CSE
printf("[2J");
printf("[0;0f");
}
Explanation / Answer
The error in your code for it not working is the revealGuessedLetter() function.
The variable charRevealed is a flag to check if the user entered a correct letter or not, but due to the revealGuessedLetter() function, it is always getting the value 1 i.e. according to the function, the user always enters a correct letter even when he actually might have not entered one.
int revealGuessedLetter(char *str, char *fname, char c) {
int i=0;
while (str[i] != '') { //looping till end of fname
if(str[i] == c)
fname[i] = c;
i++;
}
return 1; //Due to this line, each time the charRevealed variable gets value 1
}
And due to charRevealed being one, the statement which increments numBadGuesses fails.
if (!charRevealed)
{
numBadGuesses++;
}
So, the best way to correct this would be to declare a flag variable in the revealGuessedLetter() function, which takes the value 1 only if the user enters a correct letter and return flag itself.
int revealGuessedLetter(char *str, char *fname, char c)
{
int i=0, flag=0; //Declaration of the flag variable
while (str[i] != '') //looping till end of fname
{
if(str[i] == c)
{
fname[i] = c;
flag=1; //Flag is changed to 1 in case of a correct letter.
}
i++;
}
return flag;
//Return the variable flag itself, so it returns 1 if a correct letter is entered and else returns 0.
}
This will solve the problem of numBadGuesses being incremented even in case of a wrong letter entered.