From 702f24649f6ea1c4dec14cfd18a39bb000243db3 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Mon, 31 Dec 2018 23:45:07 -0500 Subject: Add game over check --- 2048.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/2048.c b/2048.c index fe17a30..260f67e 100644 --- a/2048.c +++ b/2048.c @@ -24,6 +24,25 @@ typedef unsigned uint; static uint score; static uint grid[4][4]; +static bool gameOver(void) { + for (uint y = 0; y < 4; ++y) { + for (uint x = 0; x < 4; ++x) { + if (!grid[y][x]) return false; + } + } + for (uint y = 0; y < 4; ++y) { + for (uint x = 0; x < 3; ++x) { + if (grid[y][x] == grid[y][x + 1]) return false; + } + } + for (uint x = 0; x < 4; ++x) { + for (uint y = 0; y < 3; ++y) { + if (grid[y][x] == grid[y + 1][x]) return false; + } + } + return true; +} + static void spawn(void) { uint y, x; do { @@ -228,11 +247,18 @@ static void draw(void) { } static void drawHelp(void) { + attr_set(A_NORMAL, 0, NULL); mvaddstr(HelpY + 0, HelpX, "Use the arrow keys to"); mvaddstr(HelpY + 1, HelpX, "slide and merge tiles."); mvaddstr(HelpY + 2, HelpX, "Press q to quit."); } +static void drawGameOver(void) { + attr_set(A_NORMAL, 0, NULL); + mvaddstr(HelpY + 0, HelpX, "Game over! Press q to"); + mvaddstr(HelpY + 1, HelpX, "view the scoreboard."); +} + static bool input(void) { switch (getch()) { break; case 'h': case KEY_LEFT: if (left()) spawn(); @@ -253,6 +279,7 @@ uint play2048(void) { uint help = 0; do { if (help++ == 3) erase(); + if (gameOver()) drawGameOver(); draw(); } while (input()); return score; -- cgit 1.4.1