summary refs log tree commit diff homepage
path: root/2048.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-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;