This problem is about writing a puzzle solver c code for Microsoft visual studio
ID: 651847 • Letter: T
Question
This problem is about writing a puzzle solver c code for Microsoft visual studio. It can be found by following links(indluding three images):
https://drive.google.com/file/d/0B7WUYmW3LyL6M3RiekpmU0ZObWc/view?usp=sharing
https://drive.google.com/file/d/0B7WUYmW3LyL6REV5aWlnSUxEMkk/view?usp=sharing
https://drive.google.com/file/d/0B7WUYmW3LyL6aDRIWnN2UlRZMjQ/view?usp=sharing
the input file can be found at this link:
https://drive.google.com/file/d/0B7WUYmW3LyL6bXRhQVBvNVlCb2M/view?usp=sharing
In my last homework, I am confused about typedef which may be used in this problem.
Please give specific steps .
Explanation / Answer
//fortune_codes.txt contains the given file else it wont work !!!! Add that file to the solution
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int bool;
enum {
false, true
};
//read fortune codes from file and store in the string 2D array
char *fortune_codes1[300];
char *fortune_codes2[300];
//count of number of codes
int size = 0;
void readFromText() {
//count of number of codes
int count = 0;
char *line;
size_t len = 0;
//read the file: fortune_codes.txt
FILE *fp;
fp = fopen("fortune_codes.txt", "r");
if (fp != NULL) {
//read each line and corresponding code into the array: fortune_codes
while (!feof(fp)) {
//get the first clue in text
getline(&fortune_codes1[count], &len, fp);
getline(&fortune_codes2[count], &len, fp);
//increase count
++count;
}
size = count;
} else {
printf("Sorry.. Couldnot open the file !! ");
}
}
bool fillGuessedWord(char *word, char *guess, char *guessedLetters, int len, char letter) {
bool isGameOver = true; //game over is default to true
int contain = 0; //this checks the number of times the letter exists int the word
//below we add the guessed letter to end of the array and also add delimiter
int i = 0;
while (guessedLetters[i] != '') {
i++;
}
guessedLetters[i] = letter;
guessedLetters[i + 1] = '';
for (int i = 0; i < len; i++) {
//checks whether it exists by converting all the input guessed letter to upper form
if (word[i] == (char) toupper(letter)) {
guess[i] = word[i];
contain++; //increases contain by 1
}
if (guess[i] == '_')//if _ is found then game is not over
isGameOver = false;
}
//now based on contain value, the strings are displayed
if (contain)
printf("Letter %c was found %d times in the secret puzzle", letter, contain);
else
printf("Letter %c is not a part of the secret puzzle", letter);
//then the guessed word is displayed
printf(" %s ", guess);
//then the already guessed words are displayed
printf("Letter already guessed: ");
i = 0;
while (guessedLetters[i] != '') {
printf("%c ", guessedLetters[i++]);
}
printf(" ");
//return isgameOver flag here
return isGameOver;
}
void playGame() {
//get random codes from the data in file, stored in fortune_codes variable 2D array
int index = (rand() / size);
char letter;
//randomly select the clue and guess word
char *clue = (char *) malloc(strlen(fortune_codes1[index]));
strcpy(clue, fortune_codes1[index]);
char *word = (char *) malloc(strlen(fortune_codes2[index]));
strcpy(word, fortune_codes2[index]);
int len = strlen(fortune_codes2[index]); //word size
char *guess = (char *) malloc(strlen(word)); //the word guessed with _ in the string
char guessedLetters[100]; // all the guessed letters
guessedLetters[0] = ''; // acts as a delimiter
//initialization of guess word by adding _ where the letters are to be guessed
for (int i = 0; i <= len; i++) {
if (word[i] >= 'A' && word[i] <= 'Z')
guess[i] = '_';
else
guess[i] = word[i];
}
//start of game
printf("Hi, let's play hangman. The clue is: %s ", clue);
printf("The secret puzzle is: ");
printf("%s ", guess);
bool isGameOver = false; //flag to get whether game is over or not
while (!isGameOver) {
//guess a letter
printf("Guess a letter: ");
letter = fgetc(stdin);
while (fgetc(stdin) != ' ');
//call this function to check the letter and also check whether the game is over or not
isGameOver = fillGuessedWord(word, guess, guessedLetters, len, letter);
}
//print when game is over
printf(" Congratulations! You solved the puzzle: %s ", word);
}
int main() {
readFromText();
playGame();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.