From 99c159c98258c5f49e6a9256d06c238e363fe275 Mon Sep 17 00:00:00 2001 From: Aaron Bulmahn Date: Thu, 17 Apr 2014 22:13:35 +0200 Subject: [PATCH] fixed "game over" message not appearing --- 2048_curses.c | 29 ++++++++++++++--------------- 2048_no_curses.c | 28 +++++++++++++--------------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/2048_curses.c b/2048_curses.c index 9c9bf21..9836c08 100644 --- a/2048_curses.c +++ b/2048_curses.c @@ -207,13 +207,13 @@ void save_score() { } } -/* returns if there are any available spaces left on the grid */ -int space_left() +/* returns if there are any possible moves */ +int moves_available() { int i, j; for (i = 0; i < SZ; i++) for (j = 0; j < SZ; j++) - if (!g[i][j]) + if (!g[i][j] || ((i + 1 < SZ) && (g[i][j] == g[i + 1][j])) || ((j + 1 < SZ) && (g[i][j] == g[i][j + 1]))) return 1; return 0; } @@ -222,18 +222,9 @@ int space_left() /* could do this in a much smarter fashion by finding which spaces are free */ void rand_block() { - if (space_left()) { - int x_p, y_p; - while (g[x_p = rand() % SZ][y_p = rand() % SZ]); - g[x_p][y_p] = (rand() & 3) ? 2 : 4; - } - else { - endwin(); - printf("\n" - "YOU LOSE! - Your score was %d\n", s); - save_score(); - exit(EXIT_SUCCESS); - } + int x_p, y_p; + while (g[x_p = rand() % SZ][y_p = rand() % SZ]); + g[x_p][y_p] = (rand() & 3) ? 2 : 4; } /* quick floor log2(n) */ @@ -405,6 +396,14 @@ int main(int argc, char **argv) default: goto retry; } + + if (!moves_available()) { + endwin(); + printf("\n" + "YOU LOSE! - Your score was %d\n", s); + save_score(); + exit(EXIT_SUCCESS); + } if (moved) { ITER(n_blocks, rand_block()); diff --git a/2048_no_curses.c b/2048_no_curses.c index 500ec29..66dfd4e 100644 --- a/2048_no_curses.c +++ b/2048_no_curses.c @@ -206,13 +206,13 @@ void save_score() { } } -/* returns if there are any available spaces left on the grid */ -int space_left() +/* returns if there are any possible moves */ +int moves_available() { int i, j; for (i = 0; i < SZ; i++) for (j = 0; j < SZ; j++) - if (!g[i][j]) + if (!g[i][j] || ((i + 1 < SZ) && (g[i][j] == g[i + 1][j])) || ((j + 1 < SZ) && (g[i][j] == g[i][j + 1]))) return 1; return 0; } @@ -221,18 +221,9 @@ int space_left() /* do this in a smarter fashion */ void rand_block() { - if (space_left()) { - int x_p, y_p; - while (g[x_p = rand() % SZ][y_p = rand() % SZ]); - g[x_p][y_p] = (rand() & 3) ? 2 : 4; - } - else { - printf("\n" - "YOU LOSE\n" - "Your score was %d\n", s); - save_score(); - exit(EXIT_SUCCESS); - } + int x_p, y_p; + while (g[x_p = rand() % SZ][y_p = rand() % SZ]); + g[x_p][y_p] = (rand() & 3) ? 2 : 4; } /* draws the grid and fills it with the current values */ @@ -370,6 +361,13 @@ int main(int argc, char **argv) default: goto retry; } + + if (!moves_available()) { + printf("\n" + "YOU LOSE! - Your score was %d\n", s); + save_score(); + exit(EXIT_SUCCESS); + } if (moved) { ITER(n_blocks, rand_block());