Could Someone Please Explain This Code
Hi I got a code that I would like someone to explain to me. Its a code for a word guessing game. If possible could you please point out the technical bits behind the code and also comment it out in the code itself. Many thanks. Here is the code:
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
int func (void);
void wrong (void);
void hello (void);
#define MAX_WORDS 1024
struct _WORDS
{
char* m_Array [MAX_WORDS]; // Every entry is a pointer to string
int m_iCount; // How many filled in 'm_Array'
};
typedef struct _WORDS WORDS;
void
CreateWords (WORDS* pWords, char* pstrFileName)
{
FILE* pFile;
char strLine [32]; // Assume words are no longer than 31 chars
int iLastCharPos;
pFile = fopen (pstrFileName, "rt");
if (!pFile) {
fprintf (stderr, " Error: Couldn't open file `%s'!\n", pstrFileName);
}
else {
pWords->m_iCount = 0;
while (fgets (strLine, 31, pFile)) {
// Remove '\n', at the end, if any:
iLastCharPos = strlen (strLine) - 1;
if (strLine [iLastCharPos] == '\n')
strLine [iLastCharPos] = '\0';
pWords->m_Array [pWords->m_iCount] = strdup (strLine);
(pWords->m_iCount)++;
if (pWords->m_iCount == MAX_WORDS)
break; // Array is full
}
fclose (pFile);
}
}
char*
GetRandomWord (WORDS* pWords)
{
return pWords->m_Array [rand () % pWords->m_iCount];
}
void
DestroyWords (WORDS* pWords)
{
int i;
for (i=0; i < pWords->m_iCount; i++)
free (pWords->m_Array [i]);
}
int
main (int argc, char *argv[])
{
/*
char words[10][10] = {"mythology", "astrology", "interface",
"programme", "schedules", "faithless",
"sandstorm", "bulgarian", "boulevard", "salvation"};*/
char *temp;
WORDS words;
int count;
int random;
int flag;
char *current;
char *inserted; // That's where the already inserted letters are
// stored
char *solution; // Give user a whole try -> premature end
char ch;
int i, t, q; // Why don't reduce them to one?
char finished;
// count = 50; // Hell, how many letters do we have? Around 26 ...!?
count = 10; // It would be more logic, if we put down tries to 10
// Better: 7/8
finished = 0;
flag = 2;
temp = (char*) malloc (sizeof (char) * 32);
current = (char*) malloc (sizeof (char) * 32);
inserted = (char*) malloc (sizeof (char) * count);
solution = (char*) malloc (sizeof (char) * 32);
CreateWords (&words, "words.dat");
srand (time (0));
while (!finished) {
current = GetRandomWord (&words);
memset (temp, '_', strlen (current));
temp[strlen (current)] = '\0';
memset (inserted, 0, 32);
memset (solution, 0, 32);
/*
random = func();
for (q=0; q<strlen (words[random]); q++)
current[q] = words[random][q];
*/
while (1)
{
loop_begin:
clrscr ();
printf ("\n --- Hangman ---\n");
printf (" Tries left: %d\n\n", count);
printf (" Word: %s \n"
" Wrong Guessed:\n"
" --------------\n"
" %c %c %c %c %c\n"
" %c %c %c %c %c\n",
temp, inserted[0], inserted[1], inserted[2], inserted[3],
inserted[4], inserted[5], inserted[6], inserted[7],
inserted[8], inserted[9], inserted[9]);
if (strcmp (temp, current) == 0) {
printf("\nYou did it. \nPress any key to continue ...");
break;
}
ch = tolower (getch ());
if (ch == '?')
goto end;
else if (ch == '!') {
printf ("\n Solution: ");
scanf ("%s", solution);
if (strcmp (solution, current) == 0)
printf (" That's right!\n");
else {
printf (" That's wrong!\n");
printf (" Right answer was: %s\n", current);
}
break;
}
if (!isalpha (ch))
goto loop_begin;
/ if already inserted, skip the following
for (t=0; t<strlen (inserted); t++)
if (ch == inserted[t])
goto loop_begin;
flag = 0;
for (t=0; t<strlen (current); t++)
if (ch == tolower (current[t])) {
temp[t] = current[t];
flag = 2;
}
if (flag < 2) {
count--;
inserted[strlen (inserted)] = ch;
}
if (count <= 1) {
wrong ();
return 0;
}
}
ch = getche();
}
end:
free (temp);
free (current);
free (inserted);
free (solution);
DestroyWords (&words);
return 0;
}
int
func (void)
{
return (int) rand() % 10;
}
void
wrong (void)
{
printf ("\n\nSorry, But You Have Run Out Of Your Tries. Please Retry The Program");
}