HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Improving Hangman game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gameimprovinghangman

Problem

I'd like to know how this program can be improved. Any comments or critiques are most welcome.

```
#include
#include
#include
#include
#include
#define MAX 10000
#define FOUND 1
#define NOT_FOUND 0
#define DEAD 0
#define LINUX

void starMaker(char string[] , char starContainer[]); /creats a string of stars /
int wordCalculator();/calculates how many words there is in the dictionary file/
int queryFinder(char letter , char string[] , char starWord[]);/checks if the character entered is in the mystery word/
int starFinder(char starWord[]);/checks if there is anymore stars in the star string if there isn't any it means the player has won/
void nRemover(char string[]);/removes the \n /
void score(char name[] , int score);/organizes the scores and writes them into the score file/
void showScores();/shows the scores at the end of the game/

main(void)
{
/---------------------------------DECLARATION OF VARIABLES---------------------------------/
FILE* wordDic = NULL;
char entered_char;
char starWord[MAX] = "";
char winPath[20] = ".\\wordDIC";
char mysteryWord[MAX] = "";
char ans = 0;
char name[MAX] = "";
int i = 0;
int ran = 0; /ran holds a random number that represents the number of lines/
int lifes = 10;/initial number of lifes ... the numbers of shots to go/
int check = 0; /checks if there is any more stars/
int Max = 0; /max lines/
long scor = 0;
const int Min = 0;
time_t secA = 0 , secB = 0;
/------------------------------------------------------------------------------------------/
do
{
#ifdef LINUX /linux version/
wordDic = fopen("wordDIC" , "r");
if(wordDic == NULL)
{
perror("wordDIC");
return -1;
}
#endif
#ifdef WINDOWS /windows version/
wordDic = fopen(winPath , "r");
if(wordDic == NULL)
{
perror("wordDIC");
return -

Solution

Don't use macros to conditionally change code.

Use the macros to define appropriate macros that can be used in code:

#ifdef LINUX /*linux version*/
        wordDic = fopen("wordDIC" , "r");
        if(wordDic == NULL)
        {
            perror("wordDIC");
            return -1;
        }
#endif
#ifdef WINDOWS /*windows version*/
        wordDic = fopen(winPath , "r");
        if(wordDic == NULL)
                {
                        perror("wordDIC");
                        return -1;
                }

#endif


This should be re-written as:

wordDic = fopen(WORD_DICTIONARY , "r");
        if(wordDic == NULL)
        {
            perror("wordDIC");
            return -1;
        }


Then in the header you can define:

#if defined(LINUX)
#define WORD_DICTIONARY      "wordDIC"
#elif definded(WINDOWS)
#define WORD_DICTIONARY      ".\\wordDIC"
#else
#error "Unsupported platform"
#endif

Code Snippets

#ifdef LINUX /*linux version*/
        wordDic = fopen("wordDIC" , "r");
        if(wordDic == NULL)
        {
            perror("wordDIC");
            return -1;
        }
#endif
#ifdef WINDOWS /*windows version*/
        wordDic = fopen(winPath , "r");
        if(wordDic == NULL)
                {
                        perror("wordDIC");
                        return -1;
                }

#endif
wordDic = fopen(WORD_DICTIONARY , "r");
        if(wordDic == NULL)
        {
            perror("wordDIC");
            return -1;
        }
#if defined(LINUX)
#define WORD_DICTIONARY      "wordDIC"
#elif definded(WINDOWS)
#define WORD_DICTIONARY      ".\\wordDIC"
#else
#error "Unsupported platform"
#endif

Context

StackExchange Code Review Q#28671, answer score: 5

Revisions (0)

No revisions yet.