Cleanup
This commit is contained in:
114
hangman.c
114
hangman.c
@@ -1,9 +1,9 @@
|
||||
#ifdef _WIN32 //For 32 and 64 bits
|
||||
/* pdcurses include */
|
||||
#include <curses.h>
|
||||
/* pdcurses include */
|
||||
#include <curses.h>
|
||||
#else
|
||||
/* Linux ncurses include */
|
||||
#include <ncurses.h>
|
||||
/* Linux ncurses include */
|
||||
#include <ncurses.h>
|
||||
#endif
|
||||
#include "hangman.h" //includes time.h
|
||||
#include <stdlib.h> //atexit
|
||||
@@ -15,9 +15,8 @@
|
||||
#include "figures.h"
|
||||
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
|
||||
const char *short_options = "w:hf:ct";
|
||||
struct option long_options[] = {
|
||||
{"word", required_argument, NULL, 'w'},
|
||||
@@ -25,17 +24,17 @@ int main(int argc, char **argv) {
|
||||
{"file", required_argument, NULL, 'f'},
|
||||
{"credits", no_argument, NULL, 'c'},
|
||||
{"troll", no_argument, NULL, 't'}
|
||||
};
|
||||
};
|
||||
int c, startscr = 1; /* Show startscreen by default */
|
||||
char filename[255];
|
||||
filename[0] = '\0';
|
||||
|
||||
|
||||
/* Initialization */
|
||||
initscr();
|
||||
atexit(quitProgram);
|
||||
game_state *gs;
|
||||
hitfeed *hf;
|
||||
|
||||
|
||||
gs = malloc(sizeof(game_state));
|
||||
hf = malloc(sizeof(hitfeed)); //zeroes the whole struct
|
||||
initCoordinates(gs);
|
||||
@@ -47,8 +46,8 @@ int main(int argc, char **argv) {
|
||||
hf->impstreakcounter = 0;
|
||||
keypad(stdscr, FALSE);
|
||||
nonl(); //No new line. Prevents the new line when hitting enter
|
||||
|
||||
curs_set(0);
|
||||
|
||||
curs_set(0);
|
||||
while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 ) {
|
||||
switch (c) {
|
||||
case 'w':
|
||||
@@ -80,7 +79,7 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
}
|
||||
InitializePRNG(time(NULL)); /* Initialize random number generator */
|
||||
|
||||
|
||||
if (startscr) {
|
||||
showStartScreen(gs);
|
||||
}
|
||||
@@ -178,8 +177,8 @@ void initAlphabet(game_state *gs) {
|
||||
}
|
||||
}
|
||||
gs->alphabet = malloc(sizeof(char) * hasNumber ? ALPHABET : ALPHABET_NUM);
|
||||
strcpy(gs->alphabet, "abcdefghijklmnopqrstuvwxyz");
|
||||
|
||||
strcpy(gs->alphabet, "abcdefghijklmnopqrstuvwxyz");
|
||||
|
||||
if (hasNumber) {
|
||||
strcat(gs->alphabet, "1234567890");
|
||||
}
|
||||
@@ -187,16 +186,16 @@ void initAlphabet(game_state *gs) {
|
||||
|
||||
void drawAlphabet(game_state *gs, char usedchar) {
|
||||
int start = centerDiff(gs->centerx, gs->alphabet);
|
||||
int i;
|
||||
if (usedchar != 0) { /* 0 is used for initialization */
|
||||
for (i = 0; i < (int)strlen(gs->alphabet); i++) {
|
||||
if (gs->alphabet[i] == usedchar) {
|
||||
gs->alphabet[i] = '_';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
mvprintw(1, start, "%s", gs->alphabet);
|
||||
int i;
|
||||
if (usedchar != 0) { /* 0 is used for initialization */
|
||||
for (i = 0; i < (int)strlen(gs->alphabet); i++) {
|
||||
if (gs->alphabet[i] == usedchar) {
|
||||
gs->alphabet[i] = '_';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
mvprintw(1, start, "%s", gs->alphabet);
|
||||
}
|
||||
|
||||
void toLowerCase(char *str) {
|
||||
@@ -215,7 +214,7 @@ void drawGuessWord(game_state *gs) {
|
||||
int ycounter = 0; /* counter of the y position on sceen (row) */
|
||||
int tempstartpos = startpos; /* the calculated start position to draw the characters */
|
||||
int rows = 1; /* number of rows */
|
||||
|
||||
|
||||
if (gs->wordLength * 2 > gs->maxx - LINEBREAK) {
|
||||
/* The word will be longer then the max. linesize with offset. */
|
||||
rows = 2;
|
||||
@@ -287,13 +286,13 @@ int fillCurrentWord(game_state *gs, char validchar) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!alreadyUsed) {
|
||||
if (!alreadyUsed) {
|
||||
for (i = 0; i < gs->wordLength; i++) {
|
||||
if (gs->guessWord[i] == validchar) {
|
||||
gs->currentWord[i] = validchar;
|
||||
}
|
||||
}
|
||||
drawAlphabet(gs, validchar);
|
||||
drawAlphabet(gs, validchar);
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
@@ -307,7 +306,7 @@ int stackWrongCharacter(game_state *gs, char wrongchar) {
|
||||
alreadyUsed = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!alreadyUsed) {
|
||||
gs->wrongCharacters[strlen(gs->wrongCharacters)] = wrongchar;
|
||||
drawAlphabet(gs, wrongchar);
|
||||
@@ -360,16 +359,16 @@ void printGameStats(game_state *gs) {
|
||||
result = (float)((gs->guesses - gs->moves) / gs->moves);
|
||||
} else {
|
||||
result = (float)((gs->guesses - gs->moves) / 1);
|
||||
}
|
||||
|
||||
sprintf(message, "Wrong guesses: %i, right/wrong ratio: %.2f, time: %.2fsec", gs->moves, result, diff);
|
||||
}
|
||||
|
||||
sprintf(message, "Wrong guesses: %i, right/wrong ratio: %.2f, time: %.2fsec", gs->moves, result, diff);
|
||||
mvprintw(gs->centery + 1, gs->centerx - (strlen(message) / 2), message);
|
||||
mvprintw(gs->centery + 2, gs->centerx - (gs->wordLength / 2), gs->guessWord);
|
||||
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
|
||||
void showHelp(game_state *gs) {
|
||||
char *wordstring = "-w or --word: Enter the word or sentence as an argument";
|
||||
char *helpstring = "-h or --help: Show this page";
|
||||
@@ -390,7 +389,7 @@ void readRandomLine(char *file, char *result) {
|
||||
srand(time(NULL));
|
||||
while (fgets(line, MAXWORDLENGTH, fp) != NULL) {
|
||||
count++;
|
||||
if ((rand() / (float)RAND_MAX) <= (1.0 / count)) {
|
||||
if ((rand() / (float)RAND_MAX) <= (1.0 / count)) {
|
||||
strcpy(result, line);
|
||||
}
|
||||
}
|
||||
@@ -403,8 +402,8 @@ void readRandomLine(char *file, char *result) {
|
||||
void trollHitScreen(game_state *gs, hitfeed *hf, int hits) {
|
||||
if (gs->trollEnabled) {
|
||||
char *strings[] = {"Double Hit", "Triple Hit", "Multi Hit", \
|
||||
"Ultra Hit", "Monster Hit", "Rampage", \
|
||||
"Unstoppable", "Wicked sick", "Godlike"};
|
||||
"Ultra Hit", "Monster Hit", "Rampage", \
|
||||
"Unstoppable", "Wicked sick", "Godlike"};
|
||||
int selection[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
int i, found = 0;
|
||||
for (i = 0; i < 9; i++) {
|
||||
@@ -441,7 +440,7 @@ void addHitToFeed(hitfeed *hf, char *streak, int hit) {
|
||||
hf->besthit = hit;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void printHitFeed(game_state *gs, hitfeed *hf) {
|
||||
if (gs->trollEnabled) {
|
||||
/* print the stats at the bootom right corner */
|
||||
@@ -465,7 +464,7 @@ void printHitFeed(game_state *gs, hitfeed *hf) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void trollHandleImpressive(hitfeed *hf, game_state *gs, int hits) {
|
||||
if (hits >= IMPRESSIVEHIT) {
|
||||
hf->impstreakcounter++; //Increase the counter
|
||||
@@ -484,38 +483,35 @@ void trollHandleImpressive(hitfeed *hf, game_state *gs, int hits) {
|
||||
mvprintw(5, gs->centerx - 5, " ");
|
||||
refresh();
|
||||
usleep(100000);
|
||||
}
|
||||
}
|
||||
hf->impstreakcounter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void drawFigure(game_state *gs, int drawNext) {
|
||||
int row = 6;
|
||||
int length = 8;
|
||||
int i;
|
||||
static int state = 0;
|
||||
if (!state) {
|
||||
for (i = 0; i < row; i++) {
|
||||
mvprintw(i, gs->maxx - length - 1, "%s", base_figure[i]);
|
||||
}
|
||||
state++;
|
||||
} else {
|
||||
/* Draw the stages */
|
||||
if (drawNext) {
|
||||
mvprintw(stages[state - 1] + 1, gs->maxx - 4, "%s", figure[state - 1]);
|
||||
state++;
|
||||
}
|
||||
}
|
||||
int row = 6;
|
||||
int length = 8;
|
||||
int i;
|
||||
static int state = 0;
|
||||
if (!state) {
|
||||
for (i = 0; i < row; i++) {
|
||||
mvprintw(i, gs->maxx - length - 1, "%s", base_figure[i]);
|
||||
}
|
||||
state++;
|
||||
} else {
|
||||
/* Draw the stages */
|
||||
if (drawNext) {
|
||||
mvprintw(stages[state - 1] + 1, gs->maxx - 4, "%s", figure[state - 1]);
|
||||
state++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void animateLineClear(game_state *gs, int line, int offsetMultiplier) {
|
||||
/* sleep and vanish */
|
||||
int j = 0, k = gs->maxx, usec;
|
||||
usec = ((ANIM_DURATION * 1000000) + (100000 * offsetMultiplier)) / gs->maxx;
|
||||
|
||||
|
||||
for (j = 0; j <= gs->centerx; j++, k--) {
|
||||
mvprintw(line, j, "!");
|
||||
mvprintw(line, k, "!");
|
||||
@@ -525,5 +521,3 @@ void animateLineClear(game_state *gs, int line, int offsetMultiplier) {
|
||||
mvprintw(line, k, " ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user