Removed the malloc calls to prevent memory leak at the end of the program
This commit is contained in:
59
hangman.c
59
hangman.c
@@ -27,23 +27,18 @@ int main(int argc, char **argv) {
|
|||||||
};
|
};
|
||||||
int c, startscr = 1; /* Show startscreen by default */
|
int c, startscr = 1; /* Show startscreen by default */
|
||||||
char filename[255];
|
char filename[255];
|
||||||
|
game_state gs = {0};
|
||||||
|
hitfeed hf = {{{0}}}; // 3 braces are needed to not get a warning
|
||||||
|
|
||||||
filename[0] = '\0';
|
filename[0] = '\0';
|
||||||
|
|
||||||
/* Initialization */
|
/* Initialization */
|
||||||
initscr();
|
initscr();
|
||||||
atexit(quitProgram);
|
atexit(quitProgram);
|
||||||
game_state *gs;
|
|
||||||
hitfeed *hf;
|
|
||||||
|
|
||||||
gs = malloc(sizeof(game_state));
|
initCoordinates(&gs);
|
||||||
hf = malloc(sizeof(hitfeed)); //zeroes the whole struct
|
gs.allowedMoves = DEFAULTTRIES;
|
||||||
initCoordinates(gs);
|
|
||||||
gs->allowedMoves = DEFAULTTRIES;
|
|
||||||
gs->moves = 0;
|
|
||||||
gs->guesses = 0;
|
|
||||||
gs->trollEnabled = 0;
|
|
||||||
hf->besthit = 0;
|
|
||||||
hf->impstreakcounter = 0;
|
|
||||||
keypad(stdscr, FALSE);
|
keypad(stdscr, FALSE);
|
||||||
nonl(); //No new line. Prevents the new line when hitting enter
|
nonl(); //No new line. Prevents the new line when hitting enter
|
||||||
|
|
||||||
@@ -51,12 +46,12 @@ int main(int argc, char **argv) {
|
|||||||
while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 ) {
|
while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 ) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'w':
|
case 'w':
|
||||||
sprintf(gs->guessWord, "%s", optarg);
|
sprintf(gs.guessWord, "%s", optarg);
|
||||||
startscr = 0;
|
startscr = 0;
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
showHelp(gs);
|
showHelp(&gs);
|
||||||
exit(0);
|
return 0;
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
/* Set filename */
|
/* Set filename */
|
||||||
@@ -68,34 +63,32 @@ int main(int argc, char **argv) {
|
|||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
/* troll option */
|
/* troll option */
|
||||||
gs->trollEnabled = 1;
|
gs.trollEnabled = 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
mvprintw(gs->centery, gs->centerx - 9, "Invalid arguments.");
|
mvprintw(gs.centery, gs.centerx - 9, "Invalid arguments.");
|
||||||
getch();
|
getch();
|
||||||
showHelp(gs);
|
showHelp(&gs);
|
||||||
exit(0);
|
return 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
InitializePRNG(time(NULL)); /* Initialize random number generator */
|
InitializePRNG(time(NULL)); /* Initialize random number generator */
|
||||||
|
|
||||||
if (startscr) {
|
if (startscr) {
|
||||||
showStartScreen(gs);
|
showStartScreen(&gs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* start game */
|
/* start game */
|
||||||
initGuessWord(gs, filename);
|
initGuessWord(&gs, filename);
|
||||||
initAlphabet(gs);
|
initAlphabet(&gs);
|
||||||
drawAlphabet(gs, 0);
|
drawAlphabet(&gs, 0);
|
||||||
drawFigure(gs, 0);
|
drawFigure(&gs, 0);
|
||||||
startGame(gs, hf);
|
startGame(&gs, &hf);
|
||||||
|
|
||||||
nl(); //enable newline again
|
nl(); //enable newline again
|
||||||
while (getch() != 10);
|
while (getch() != 10);
|
||||||
clear();
|
clear();
|
||||||
free(gs);
|
|
||||||
free(hf);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,19 +161,13 @@ void initGuessWord(game_state *gs, char *filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void initAlphabet(game_state *gs) {
|
void initAlphabet(game_state *gs) {
|
||||||
int hasNumber = 0;
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < MAXWORDLENGTH; i++) {
|
|
||||||
if (isdigit(gs->guessWord[i]) > 0) {
|
|
||||||
hasNumber = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
gs->alphabet = malloc(sizeof(char) * ((hasNumber ? ALPHABET : ALPHABET_NUM) + 1));
|
|
||||||
strcpy(gs->alphabet, "abcdefghijklmnopqrstuvwxyz");
|
strcpy(gs->alphabet, "abcdefghijklmnopqrstuvwxyz");
|
||||||
|
|
||||||
if (hasNumber) {
|
for (unsigned int i = 0; i < strlen(gs->guessWord); i++) {
|
||||||
|
if (isdigit(gs->guessWord[i]) > 0) {
|
||||||
strcat(gs->alphabet, "1234567890");
|
strcat(gs->alphabet, "1234567890");
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -26,7 +26,7 @@ typedef struct {
|
|||||||
char currentWord[MAXWORDLENGTH];
|
char currentWord[MAXWORDLENGTH];
|
||||||
char wrongCharacters[DEFAULTTRIES];
|
char wrongCharacters[DEFAULTTRIES];
|
||||||
int trollEnabled;
|
int trollEnabled;
|
||||||
char *alphabet;
|
char alphabet[ALPHABET_NUM + 1];
|
||||||
time_t startTime;
|
time_t startTime;
|
||||||
time_t endTime;
|
time_t endTime;
|
||||||
int wordRows;
|
int wordRows;
|
||||||
|
Reference in New Issue
Block a user