I need the C Program Pseudo code description of HOW my code works. #include \"st
ID: 3770555 • Letter: I
Question
I need the C Program Pseudo code description of HOW my code works.
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#define MAXNUMWORDS 512
#define MAXWORDLENGTH 16
int main() {
srand(time(NULL));
char guessingWords[MAXNUMWORDS][MAXWORDLENGTH];
int WordsReadIn = 0;
FILE *pToFile = fopen("Hangman Words.txt","r");
if(pToFile == NULL) {
printf("Unable To Read File ");
return 0;
}
char input[64];
while(fgets(input, 63, pToFile)) {
sscanf(input, "%s", guessingWords[WordsReadIn]);
WordsReadIn++;
}
fclose(pToFile);
int randomIndex = rand() % WordsReadIn;
int numLives = 5;
int numCorrect = 0;
int oldCorrect = 0;
int lengthOfWord = strlen(guessingWords[randomIndex]);
int letterGuessed[10] = { 0,0,0,0,0,0,0,0,0,0 };
int loopIndex = 0;
int reguessed = 0;
char guess[16];
char letterEntered;
while ( numCorrect < lengthOfWord ) {
printf(" New Turn.... Hangman Word:");
for( loopIndex = 0; loopIndex < lengthOfWord; loopIndex++) {
if(letterGuessed[loopIndex] == 1) {
printf("%c",guessingWords[randomIndex][loopIndex]);
} else {
printf("-");
}
}
printf(" ");
printf("Enter a guess letter:");
fgets(guess, 16, stdin);
letterEntered = guess[0];
reguessed = 0;
printf("letterEntered:%c ",letterEntered);
oldCorrect = numCorrect;
for( loopIndex = 0; loopIndex < lengthOfWord; loopIndex++) {
if(letterGuessed[loopIndex] == 1) {
if(guessingWords[randomIndex][loopIndex] == letterEntered) {
reguessed = 1;
break;
}
continue;
}
if( letterEntered == guessingWords[randomIndex][loopIndex] ) {
letterGuessed[loopIndex] = 1;
numCorrect++;
}
}
if( oldCorrect == numCorrect && reguessed == 0) {
numLives--;
printf("Sorry, wrong guess ");
if (numLives == 0) {
break;
}
} else if( reguessed == 1) {
printf("Already Guessed!! ");
} else {
printf("Correct guess :) ");
}
}
if (numLives == 0) {
printf(" Oops you lose, the word was: %s ",
guessingWords[randomIndex]);
} else {
printf(" CONGRATS, YOU GOT THE WORD!!! :) ");
}
return 0;
}
Explanation / Answer
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#define MAXNUMWORDS 512
#define MAXWORDLENGTH 16
int main() {
srand(time(NULL));
char guessingWords[MAXNUMWORDS][MAXWORDLENGTH];/* guessingWords[512][16] its a character array*/
int WordsReadIn = 0;/*initailized wordsReadIn as 0*/
FILE *pToFile = fopen("Hangman Words.txt","r");/* tried to open a file in read mode*/
if(pToFile == NULL) {/* if fopen() returns NULL then file cannot be opened*/
printf("Unable To Read File ");
return 0;
}
char input[64];/* character array is taken input[64]*/
while(fgets(input, 63, pToFile)) {/*obtaining string from pToFile of length 63*/
sscanf(input, "%s", guessingWords[WordsReadIn]);/* String from source array input is placedin guessingWords array
WordsReadIn++;
}/*loop continues until we reach endof file*/
fclose(pToFile);/*pToFile is closed*/
int randomIndex = rand() % WordsReadIn; /* randomIndex is calculated by generating a random number using rand() function and obtaining remainder of random number to WordsReadIn.*/
int numLives = 5;
int numCorrect = 0;
int oldCorrect = 0;
int lengthOfWord = strlen(guessingWords[randomIndex]);
int letterGuessed[10] = { 0,0,0,0,0,0,0,0,0,0 };
int loopIndex = 0;
int reguessed = 0;
char guess[16];
char letterEntered;
while ( numCorrect < lengthOfWord ) {/* initailly 0 is less than a string of length at given index*/
printf(" New Turn.... Hangman Word:");
for( loopIndex = 0; loopIndex < lengthOfWord; loopIndex++) {/* loop runs from index 0 to lenght of word-1*/
if(letterGuessed[loopIndex] == 1) {
printf("%c",guessingWords[randomIndex][loopIndex]);
} else {
printf("-");
}
}/* this loop prints '-' for 10 times and thereafter it prints character in array guesingWords*/
printf(" ");
printf("Enter a guess letter:");
fgets(guess, 16, stdin);/*reads string from standard input and stores in guess[] array max of 16 character including '' character */
letterEntered = guess[0];
reguessed = 0;
printf("letterEntered:%c ",letterEntered);
oldCorrect = numCorrect;/* numCorrect is zero*/
for( loopIndex = 0; loopIndex < lengthOfWord; loopIndex++) {
if(letterGuessed[loopIndex] == 1) {
if(guessingWords[randomIndex][loopIndex] == letterEntered) {
reguessed = 1;
break;
}
continue;
}/*reads a character from 0th location and if it contains a character then reguessed is set to 1*/
if( letterEntered == guessingWords[randomIndex][loopIndex] ) {
letterGuessed[loopIndex] = 1;
numCorrect++;
}
}/* if entered letter is at random index in guessing words then letterguessed is true*/
if( oldCorrect == numCorrect && reguessed == 0) {
numLives--;
printf("Sorry, wrong guess ");
if (numLives == 0) {
break;
}
} else if( reguessed == 1) {
printf("Already Guessed!! ");
} else {
printf("Correct guess :) ");
}
}/* this else-if ladder whether character guessed correctly or not*/
if (numLives == 0) {
printf(" Oops you lose, the word was: %s ",
guessingWords[randomIndex]);
} else {
printf(" CONGRATS, YOU GOT THE WORD!!! :) ");
}/* this if else checks whether we have chosed the word at random index or not*/
return 0;
}