correct end of game
authorLaurent Mazet <mazet@softndesign.org>
Wed, 21 Aug 2024 18:47:39 +0000 (20:47 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Wed, 21 Aug 2024 18:47:39 +0000 (20:47 +0200)
function.c
function.h
pentomino.c

index fdd4f0bbc80ac915221492605ea435f58e985093..8d84e7d6cee74963fffc31a6802873c81da262b5 100644 (file)
@@ -406,4 +406,19 @@ void settleblock (board_t *board, block_t *block, int x, int y, int mode)
     }
 }
 
+int endofgame (board_t *board)
+{
+    int i, j;
+
+    for (i = 0; i < board->width; i++) {
+        for (j = 0; j < board->height; j++) {
+            if (*getcell (board, i, j) == ' ') {
+                return 0;
+            }
+        }
+    }
+
+    return 1;
+}
+
 /* vim: set ts=4 sw=4 et: */
index e1b8529a1d9674499c252a9c534dd91c4720ab97..713d8c5244c1afd53bb87a3929bfb250f5724f57 100644 (file)
@@ -47,6 +47,8 @@ block_t *copyblock (block_t *block);
 
 void freeblock (block_t *block);
 
+int findnext (int i, int n);
+
 block_t *mirrorblock (block_t *block, int dir);
 
 block_t *rotateblock (block_t *block, int rot);
@@ -59,7 +61,7 @@ int testposition (board_t *board, block_t *block, int x, int y);
 
 void settleblock (board_t *board, block_t *block, int x, int y, int mode);
 
-int findnext (int i, int n);
+int endofgame (board_t *board);
 
 #endif /* __FUNCTION_H__ */
 
index 02d511cae64c619ee4052b6416a0589d9b36e672..cc7d78afa53fe0ac5e8b9f2799e39f7d48553fcc 100644 (file)
@@ -229,7 +229,7 @@ int main (int argc, char *argv[])
 
     /* window positions (bench) */
     int xbench = bench->xoffset = xboard;
-    int ybench = bench->yoffset = yboard + board->ysize + 1 + yoffset;
+    int ybench = bench->yoffset = yboard + max (board->ysize, next->ysize) + 1 + yoffset;
 
     /* window positions (help) */
     int xhelp = xbench + bench->xsize + xoffset + 1;
@@ -249,12 +249,14 @@ int main (int argc, char *argv[])
 
     /* event loop */
     int stop = 0;
+    int mode = 0;
     while (!stop) {
         boardwindow (board, 0);
 
         boardwindow (next, 1);
         block_t *block = NULL;
-        if (cursor >= 0) {
+        switch (mode) {
+        case 0:
             block = blocks->block[cursor];
             if (blocks->settle[cursor] == 0) {
                 boardwindow (next, 0);
@@ -262,11 +264,15 @@ int main (int argc, char *argv[])
             }
             displayblock (next, block, xoffset + (wblock - block->width) / 2, (hblock - block->height) / 2);
             msgwindow ("Can you solve this puzzle?", xmsg, ymsg, lmsg);
-        } else {
+            break;
+        case 1:
             msgwindow ("Puzzle solved", xmsg, ymsg, lmsg);
+            break;
         }
 
         int ch = getch ();
+
+        /* record key pressed */
         if (maxnbrecords) {
             if (nbrecords < maxnbrecords) {
                 rec[nbrecords++] = ch;
@@ -300,7 +306,7 @@ int main (int argc, char *argv[])
         }
 
         /* test end of game */
-        if (cursor < 0) {
+        if (mode == 1) {
             continue;
         }
 
@@ -315,6 +321,7 @@ int main (int argc, char *argv[])
                     blocks->settle[cursor] = 1;
                     blocks->x[cursor] = xcursor;
                     blocks->y[cursor] = ycursor;
+                    mode = endofgame (board);
                 }
             } else {
                 blocks->settle[cursor] = 0;