• Log in or sign up for an account here: https://www.sythe.org/login

Need help with a function in C. [Paid]

NathanW

Newcomer
Joined
May 6, 2015
Sythe Gold
0
Messages
22
Need help with a function in C. [Paid]
Hi,
I have been doing a hangman project that i have been trying to do in C. i have completed 90% of it but i am stuck on how to do the last two functions. I have tried to so many different ways but i cant seem to work it out.
Any help is greatly appreciated.

Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
 
 
 
 
int main(int argc, char *argv[]){
 
 
    char word[500];
    int len;
    int guessedLetters[26] = {
      1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0
    };
 
 
    len = init(word);
    printf("\nWord = %s", word);
    printf("\nGuessed:  ");
    displayWord(word, guessedLetters, len);
    return 0;
}
 
 
 
 
//picks word for hangman game
int init(char* word)
{
   srand(time(NULL));
   int randomIndex = rand() % 50;
 
 
   const char* words[50] = {
      "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"
   };
 
 
    strcpy(word, words[randomIndex]);
    int lengthOfWord = strlen(words[randomIndex]);
 
 
    return lengthOfWord;
 
 
 
 
 
 
}
 
 
 
 
//Displays word with dashes over un-guessed letters
void displayWord(char *word, int *guessedLetters, int length)
{
    int i;
 
 
    for (i = 0; i < length; i++ ){
 
 
        if(guessedLetters[word[i] - 97] == 1){
            printf("%c", word[i]);
        }else{
            printf("-");
        }
    }
    printf("\n\n");
}
 
 
 
 
 
 
 
 
/****************************************************************************
* Function guessLetter() prompts the user to enter one letter. The function
* maintains an array of guessed letters. The function returns GOOD_GUESS
* or BAD_GUESS depending on whether or not the letter is in the word.
****************************************************************************/
int guessLetter(char* word, int* guessedLetters)
{
}


/****************************************************************************
* Function isGameOver() is the final step in the program. The game is over
* if either all letters in the word have been guessed, or the player has run
* out of guesses. The player is congratulated if he/she wins. The word is
* displayed to the player if he/she loses. This function returns either
* GAME_OVER or GAME_CONTINUE.
****************************************************************************/
int isGameOver(char* word, int* guessedLetters, unsigned wrongGuesses)
{
}


I have experience in Java but i cant seem to wrap my head around C. Any help with the last two functions (isGameOver() and guessLetter()) would be very much appreciated. Will send some $$ over as well.
 
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
Need help with a function in C. [Paid]
I mean I'm not going to write the functions for you, but I'll give you a detailed outline on how to get them done.

guessLetter:
- Prompt use to enter a letter, store it as a temp char
- Check entered char against array of chars already guessed so that the user can't duplicate guesses
- A C-String is just an array of chars, so loop through the correct word like it's an array and check if the guessed char matches the currently selected char in the C-String
- If the loop comes up empty, return BAD_GUESS (let's say an int value of 0), or 1 for GOOD_GUESS
- Add 1 to a global "good guesses" variable, so we can keep track of how many good guesses we have. Make a "bad guesses variable" to keep track of wrong guesses too.

isGameOver:
- Run isGameOver every turn to check if the game is over yet or not
- If our "good guesses" variable is equal to the length of the word we're playing with, the game is over
- If our "bad guesses" variable is equal to the length of the word we're playing with, the game is over

Feel free to PM with further questions
 
Need help with a function in C. [Paid]
Thanks!
I just needed to be pointed in the right direction. I will use your advice tonight and let you know how i go
 
Runescape Gamblingrunescape gambling
Back
Top