I am trying to make a hangman gave using C Functions. I was able to make the game without using functions but now when i try to use functions i am running into a few problems.
The main problem i am having is in my displayWord() function. I need this function to print out the word to screen with all letters that have not been correctly guessed blanked out with an underscore.
~Header FiIles
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Constants. */
#define NUM_WORDS 50
#define ALPHABET_SIZE 26
#define GOOD_GUESS 0
#define BAD_GUESS 1
#define GAME_OVER 1
#define GAME_CONTINUE 0
#define MAX_GUESS 10
#define MAX_WORD_LEN 10
/* Function prototypes. */
int init(char* word);
void displayWord(char* word, int* guessedLetters, int* length);
int guessLetter(char* word, int* guessedLetters);
void displayHangman(unsigned wrongGuesses);
int isGameOver(char* word, int* guessedLetters, unsigned wrongGuesses);
void readRestOfLine();
~Main Code
#include "hangman.h"
/****************************************************************************
* Function main() is the entry point for the program.
****************************************************************************/
int main(void)
{
char word[MAX_WORD_LEN + 1];
unsigned wrongGuesses = 0;
int guessedLetters[ALPHABET_SIZE] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
int length = 0;
int numLives = 10;
int numCorrect = 0;
int oldCorrect = 0;
int lengthOfWord = 0;
length = init(word);
printf("length: %d\n", length);
displayWord(word, guessedLetters, length);
return EXIT_SUCCESS;
}
int init(char* word)
{
srand(time(NULL));
int randomIndex = rand() % 50;
const char* words[NUM_WORDS] = {
"array", "auto", "break", "case", "cast",
"character", "comment", "compiler", "constant", "continue",
"default", "double", "dynamic", "else", "enum",
"expression", "extern", "file", "float", "function",
"goto", "heap", "identifier", "library", "linker",
"long", "macro", "operand", "operator", "pointer",
"prototype", "recursion", "register", "return", "short",
"signed", "sizeof", "stack", "statement", "static",
"string", "struct", "switch", "typedef", "union",
"unsigned", "variable", "void", "volatile", "while"
};
word = words[randomIndex];
int lengthOfWord = strlen(words[randomIndex]);
printf("Word: %s\n", words[randomIndex]);
return lengthOfWord;
}
void displayWord(char* word, int* guessedLetters, int* length)
{
int loopIndex;
char gameWord[][1] = word;
for ( loopIndex = 0; loopIndex < length; loopIndex++ ) {
if(letterGuessed[loopIndex] == 1) {
printf("%c",gameWord[0][loopIndex]);
} else {
printf("-");
}
}
}
I have no idea how to scan through my random word to print out the letters that have been guessed. I have tried a few different way from reading around the web and watching videos but i am really not sure. Please help.
Problem Code
void displayWord(char* word, int* guessedLetters, int* length)
{
int loopIndex;
char gameWord[][1] = word;
for ( loopIndex = 0; loopIndex < length; loopIndex++ ) {
if(letterGuessed[loopIndex] == 1) {
printf("%c",gameWord[0][loopIndex]);
} else {
printf("-");
}
}
}
Aucun commentaire:
Enregistrer un commentaire