summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-12-31 23:45:07 -0500
committerJune McEnroe <june@causal.agency>2018-12-31 23:45:07 -0500
commit702f24649f6ea1c4dec14cfd18a39bb000243db3 (patch)
tree061382886be228c22b833183569f52da21757e10
parentIgnore root and chroot.tar (diff)
downloadplay-702f24649f6ea1c4dec14cfd18a39bb000243db3.tar.gz
play-702f24649f6ea1c4dec14cfd18a39bb000243db3.zip
Add game over check
-rw-r--r--2048.c27
1 files changed, 27 insertions, 0 deletions
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;