patterncMinor
My HangMan Game
Viewed 0 times
gamehangmanstackoverflow
Problem
I have been trying to learn C by making a Hangman game. It's not perfect, but with C, I worry that my coding practices will not be very good. I'm also new to allocating memory myself, so I feel I may have done something wrong there. I would very much appreciate reviews of my code.
The game takes in a text file with the first line of the file being a category and the rest of the file being words that can be used in the game: a random line is selected. The file address is in the arguments for the program.
```
/* File: hangman.c
*
*
* This program implements the game, Hangman.
*
* Comments
*/
#include
#include
#include
#include
#include
#include
#include
#include
int contains(char[], char, int);
char *hideWord(char []);
void changeWord(char [], char *, char);
void InitializeArray(char [], int);
void printHangMan(int, int);
void Engine(char [], char *);
void Clear();
void printSpace(int);
char stringTrimmer(char );
void printOneSpace(int);
char Randomline(FILE , char *);
char Catergory(FILE , char *);
void printTop();
void printWin(int, int);
void printLost(int, int);
void printRest(int, int, char [], char [], char []);
char **fileArray(char []);
int stringCont(char , char );
int menu();
void files();
#define Size 60
#define wordLength 30
int main(int argc, char *argv[])
{
FILE *ptrFile;
char input[5] = " ";
char *randomCat;
char *randomLine;
while (strcmp(input, "quit") != 0)
{
char address[Size]="0";
int menuChoice = menu();
if(menuChoice== 1)
{
char *i=argv[1];
randomLine = Randomline(ptrFile,i);
randomCat = Catergory(ptrFile,i);
}
else if(menuChoice == 2)
{
files(address);
randomLine = Randomline(ptrFile, address);
randomCat = Catergory(ptrFile, address);
system("clear");
}
Engine(p, r);
printf("\ntype menu to return to Menu type quit to quit\n");
scanf("%s", input);
system("clear");
}
retur
The game takes in a text file with the first line of the file being a category and the rest of the file being words that can be used in the game: a random line is selected. The file address is in the arguments for the program.
```
/* File: hangman.c
*
*
* This program implements the game, Hangman.
*
* Comments
*/
#include
#include
#include
#include
#include
#include
#include
#include
int contains(char[], char, int);
char *hideWord(char []);
void changeWord(char [], char *, char);
void InitializeArray(char [], int);
void printHangMan(int, int);
void Engine(char [], char *);
void Clear();
void printSpace(int);
char stringTrimmer(char );
void printOneSpace(int);
char Randomline(FILE , char *);
char Catergory(FILE , char *);
void printTop();
void printWin(int, int);
void printLost(int, int);
void printRest(int, int, char [], char [], char []);
char **fileArray(char []);
int stringCont(char , char );
int menu();
void files();
#define Size 60
#define wordLength 30
int main(int argc, char *argv[])
{
FILE *ptrFile;
char input[5] = " ";
char *randomCat;
char *randomLine;
while (strcmp(input, "quit") != 0)
{
char address[Size]="0";
int menuChoice = menu();
if(menuChoice== 1)
{
char *i=argv[1];
randomLine = Randomline(ptrFile,i);
randomCat = Catergory(ptrFile,i);
}
else if(menuChoice == 2)
{
files(address);
randomLine = Randomline(ptrFile, address);
randomCat = Catergory(ptrFile, address);
system("clear");
}
Engine(p, r);
printf("\ntype menu to return to Menu type quit to quit\n");
scanf("%s", input);
system("clear");
}
retur
Solution
There's a lot of code here (the poor indentation in making it harder to read), but I'll skim through and recommend several things I've found.
-
Some of your indentation is off. Remember to have all code within curly braces indented, preferably by four spaces. More importantly, keep this consistent throughout the program.
-
You're mixing lowercase and uppercase naming with your functions. Choose only one and keep it consistent. Lowercase is common for functions in C, but it's mostly personal preference.
-
You can eliminate all of those function prototypes by defining
-
Your large hard-coded displays clutter up your code and are not necessary for a console program. Just a simple menu for the user will work.
-
-
Prefer to call
-
I don't think your space-printing functions are necessary, or at least
-
For printing an unformatted line that should also end with a newline, use
-
Some of your indentation is off. Remember to have all code within curly braces indented, preferably by four spaces. More importantly, keep this consistent throughout the program.
-
You're mixing lowercase and uppercase naming with your functions. Choose only one and keep it consistent. Lowercase is common for functions in C, but it's mostly personal preference.
-
You can eliminate all of those function prototypes by defining
main() below the others. This works because main() will already be aware of their definitions when calling them.-
Your large hard-coded displays clutter up your code and are not necessary for a console program. Just a simple menu for the user will work.
-
printHangMan() is really long and should be simplified if you still want such an output.-
Prefer to call
srand() at the top of main(). This will make it easy to maintain and will ensure that it's called only once. If called multiple times, it reset the seed each time, resulting in the "same" random values when calling rand().-
I don't think your space-printing functions are necessary, or at least
printOneSpace() (you could just output this inline). You shouldn't output a lot of spaces if it'll clutter up your code and output.-
For printing an unformatted line that should also end with a newline, use
puts() instead of printf(). For similar lines without a newline, use fputs(). More info about that here.Context
StackExchange Code Review Q#46453, answer score: 5
Revisions (0)
No revisions yet.