fixed divided by 0 bug #9; max x/y -1
This commit is contained in:
13
hangman.c
13
hangman.c
@@ -113,6 +113,8 @@ void updateScreen(game_state *gs) {
|
||||
|
||||
void initCoordinates(game_state *gs) {
|
||||
getmaxyx(stdscr, gs->maxy, gs->maxx);
|
||||
gs->maxy -= 1;
|
||||
gs->maxx -= 1;
|
||||
gs->centery = gs->maxy / 2;
|
||||
gs->centerx = gs->maxx / 2;
|
||||
}
|
||||
@@ -255,9 +257,16 @@ void printGameStats(game_state *gs) {
|
||||
mvprintw(gs->centery + 1, gs->centerx - (gs->wordLength / 2), gs->guessWord);
|
||||
} else {
|
||||
char message[100];
|
||||
float result = 0;
|
||||
sprintf(message, "Game won! Total guesses: %i", gs->guesses);
|
||||
mvprintw(gs->centery, gs->centerx - (strlen(message) / 2), message);
|
||||
sprintf(message, "Wrong guesses: %i, right/wrong ratio: %.2f", gs->moves, (float)(gs->guesses - gs->moves) / gs->moves);
|
||||
if (gs->moves != 0) {
|
||||
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", gs->moves, result);
|
||||
mvprintw(gs->centery + 1, gs->centerx - (strlen(message) / 2), message);
|
||||
|
||||
}
|
||||
@@ -321,7 +330,7 @@ void trollHitScreen(game_state *gs, int hits) {
|
||||
refresh();
|
||||
/* sleep and vanish */
|
||||
int j = 0, k = gs->maxx - 1;
|
||||
for (j = 0; j < gs->centerx; j++, k--) {
|
||||
for (j = 0; j <= gs->centerx; j++, k--) {
|
||||
mvprintw(5, j, "!");
|
||||
mvprintw(5, k, "!");
|
||||
refresh();
|
||||
|
Reference in New Issue
Block a user