This is for C program: I am trying to create the following program with the foll
ID: 3882263 • Letter: T
Question
This is for C program: I am trying to create the following program with the following functions. Already created a main.c and have the following functions that need to run in my new program as the same as my main. Please help. I am new to this.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fp;
fp = fopen("game.txt", "r");
char **puzzles;
int count = 0;
int num = 0;
int numWords = 0;
int win = 0;
int randomPuzzle;
int i = 0;
char letter;
if ( fp != NULL )
{
char line[30];
while ( fgets ( line, sizeof line, fp ) != NULL )
{
size_t len = strlen(line);
if (len > 0 && line[len-1] == ' ') {
line[--len] = '';
}
num++;
if ( num == 1 )
{
numWords = atoi(line);
puzzles = malloc(sizeof (char) * (numWords));
num++;
}
else
{
puzzles[count] = malloc(sizeof (char) * 30);
strcpy(puzzles[count], line);
count++;
}
}
}
time_t t;
char *hangman = "HANGMAN";
srand((unsigned) time(&t));
randomPuzzle = rand() % numWords;
char *originalPuzzle = malloc(sizeof(char)*30);
char *missedGuess = malloc(sizeof(char) *8);
int curPuzLength = 0;
int found = 0;
int missedWord = 0;
strcpy(originalPuzzle, puzzles[randomPuzzle]);
curPuzLength = strlen(originalPuzzle);
char *currentPuzzle = malloc(sizeof(char) *curPuzLength);
printf("puzzle chosen => [%s] ", originalPuzzle);
printf("Current Puzzle:");
for (i = 0; i < curPuzLength; i++)
{
strcat(currentPuzzle, "_");
}
printf(currentPuzzle);
printf(" Missed Guesses: ");
for (i = 0; i < 7; i++)
{
strcat(missedGuess," ");
}
int missedCounter = 0;
printf("Please guess a letter:");
scanf("%c",&letter);
while (1)
{
found = 0;
printf("Current Puzzle:");
for (i = 0; i < curPuzLength; i++)
{
if (letter == originalPuzzle[i])
{
currentPuzzle[i] = letter;
found = 1;
}
else if (currentPuzzle[i] == '_')
{
currentPuzzle[i] = '_';
}
}
printf("%s ",currentPuzzle);
printf("Missed Guesses:");
if (found)
{
printf("%s ", missedGuess);
}
else
{
missedGuess[missedCounter] = hangman[missedWord];
missedWord++;
missedCounter++;
printf("%s ", missedGuess);
}
if (missedWord > 6)
{
printf("You LOST! ");
printf("Exiting program... ");
exit(0);
}
if ( strcmp(currentPuzzle, originalPuzzle) == 0 )
{
printf("You WON! ");
printf("Exiting program... ");
exit(0);
}
printf("Please guess a letter:");
scanf("%1s",&letter);
}
return 0;
}
My functions:
#include <stdbool.h>
typdef struct Hangman
{
char **puzzles;
int numPuzzles;
char *currentPuzzleWord;
char *guessedWord;
bool gameOver;
bool gameWon;
int missedGuesses;
*Hangman;
}
Hangman createHangmanGame(char *puzzleFile)
void newHangmanPuzzle(Hangman currentHangmanGame)
void loadPuzzleFile(Hangman currentHangmanGame, char *puzzleFile)
bool isPuzzleOver(Hangman currentHangmanGame)
bool isPuzzleSolved(Hangman currentHangmanGame)
char* getGuessedWord(Hangman currentHangmanGame)
bool guessLetter(Hangman currentHangmanGame, char letterToGuess)
char* getStateOfHangman(Hangman currentHangmanGame)
void freeHangmanGame(Hangman currentHangmanGame)
My new main:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typdef struct Hangman
int main(int argc, char** argv)
{
int i;
system("cls");
printf(" Welcome to the Hangman Game! ");
printf(" You will have 7 chances to guess the right word. ")
getchar();
printf(" Hit >> ENTER<<");
system("cls");
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef struct hangman
{
char **puzzles;
int numPuzzles;
char *currentPuzzleWord;
char *guessedWord;
bool gameOver;
bool gameWon;
int missedGuesses;
}Hangman;
Hangman createHangmanGame(char *puzzleFile)
{
FILE *fp = fopen(puzzleFile, "w");
Hangman *game = calloc(1, sizeof(Hangman));
if(fp == NULL)
{
printf("Error cannot create puzzle file. ");
return *game;
}
int puzzleCount = 0;
char word[30] = {0};
printf("Enter how many words you need in the puzzle : ");
scanf("%d", &puzzleCount);
fprintf(fp, "%d", puzzleCount);
while ((getchar()) != ' '); // flushes the standard input (clears the input buffer)
while(puzzleCount--)
{
memset(word, '', sizeof(word));
fgets(word, 30, stdin);
fputs(word, fp);
}
fclose(fp);
return *game;
}
char* getGuessedWord(Hangman currentHangmanGame)
{
return (currentHangmanGame.guessedWord);
}
bool isPuzzleSolved(Hangman currentHangmanGame)
{
return currentHangmanGame.gameWon;
}
void newHangmanPuzzle(Hangman currentHangmanGame)
{
int win = 0;
int randomPuzzle;
int i = 0;
char letter;
time_t t;
randomPuzzle = rand() % currentHangmanGame.numPuzzles;
currentHangmanGame.currentPuzzleWord = malloc(sizeof(char)*30);
currentHangmanGame.missedGuesses = 0;
int curPuzLength = 0;
int found = 0;
int missedWord = 0;
strcpy(currentHangmanGame.currentPuzzleWord, currentHangmanGame.puzzles[randomPuzzle]);
curPuzLength = strlen(currentHangmanGame.currentPuzzleWord);
currentHangmanGame.guessedWord = malloc(sizeof(char) *curPuzLength);
printf("puzzle chosen => [%s] ", currentHangmanGame.currentPuzzleWord); /* Comment this line as it shows the player what word he is playing. */
printf("Current Puzzle:");
for (i = 0; i < curPuzLength; i++)
{
strcat(currentHangmanGame.guessedWord, "_");
}
printf("%s ", getGuessedWord(currentHangmanGame));
int missedCounter = 0;
printf("Please guess a letter:");
scanf("%c",&letter);
while (1)
{
found = 0;
printf("Current Puzzle:");
for (i = 0; i < curPuzLength; i++)
{
if (letter == currentHangmanGame.currentPuzzleWord[i])
{
currentHangmanGame.guessedWord[i] = letter;
found = 1;
}
else if (currentHangmanGame.guessedWord[i] == '_')
{
currentHangmanGame.guessedWord[i] = '_';
}
}
printf("%s ", getGuessedWord(currentHangmanGame));
printf("Missed Guesses:");
if (found)
{
printf("%d ", currentHangmanGame.missedGuesses);
}
else
{
missedWord++;
missedCounter++;
printf("%d ", currentHangmanGame.missedGuesses);
}
if (missedWord > 6)
{
currentHangmanGame.gameOver = true;
currentHangmanGame.gameWon = false;
exit(0);
}
if ( strcmp(currentHangmanGame.guessedWord, currentHangmanGame.currentPuzzleWord) == 0 )
{
currentHangmanGame.gameOver = true;
currentHangmanGame.gameWon = true;
exit(0);
}
printf("Please guess a letter:");
scanf("%1s",&letter);
}
}
void loadPuzzleFile(Hangman *currentHangmanGame, char *puzzleFile)
{
FILE *fp;
fp = fopen("game.txt", "r");
int count = 0;
int num = 0;
if ( fp != NULL )
{
char line[30];
while ( fgets ( line, sizeof line, fp ) != NULL )
{
size_t len = strlen(line);
if (len > 0 && line[len-1] == ' ') {
line[--len] = '';
}
num++;
if ( num == 1 )
{
currentHangmanGame->numPuzzles = atoi(line);
currentHangmanGame->puzzles = malloc(sizeof (char) * (currentHangmanGame->numPuzzles));
num++;
}
else
{
currentHangmanGame->puzzles[count] = malloc(sizeof (char) * 30);
strcpy(currentHangmanGame->puzzles[count], line);
count++;
}
}
}
}
bool isPuzzleOver(Hangman currentHangmanGame)
{
return currentHangmanGame.gameOver;
}
bool guessLetter(Hangman currentHangmanGame, char letterToGuess)
{
return true; /* Explain What this function does, what is the input/ouput, then i will implement. */
}
char* getStateOfHangman(Hangman currentHangmanGame)
{
return NULL; /* Explain what this function does. I couldn't get this from its name. */
}
void freeHangmanGame(Hangman currentHangmanGame)
{
free(currentHangmanGame.puzzles);
free(currentHangmanGame.currentPuzzleWord);
free(currentHangmanGame.guessedWord);
return;
}
int main(int argc, char** argv)
{
int i;
time_t t;
// system("cls");
printf(" Welcome to the Hangman Game! ");
printf(" You will have 7 chances to guess the right word. ");
printf(" Hit >> ENTER<<");
getchar();
// system("cls");
srand((unsigned) time(&t));
Hangman game = createHangmanGame("game.txt");
loadPuzzleFile(&game, "game.txt");
newHangmanPuzzle(game);
if(true == isPuzzleOver(game))
{
if(true == isPuzzleSolved(game))
printf("You WON! ");
else
printf("You LOST! ");
}
printf("Exiting program... ");
}
/***** Note : I used linux environment for compiling and executing the program. If you are facing any problem, please revert back to me. I will try to short it out. And please give me explanation for the above two functions(which are not implemented), so that I can implement it. I have not added more comments as it was your code only. Wherever needed, I have added comments, which you can refer before executing program. Thanks. **************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.