every thing is working
authorLaurent Mazet <mazet@softndesign.org>
Sun, 18 Aug 2024 22:17:42 +0000 (00:17 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 18 Aug 2024 22:17:42 +0000 (00:17 +0200)
function.c
function.h
pentomino.c

index ce21140e4206f2729611027c7301845ddb85fdc4..fdd4f0bbc80ac915221492605ea435f58e985093 100644 (file)
@@ -367,20 +367,12 @@ void freeblocks (blocks_t *blocks)
     free (blocks);
 }
 
-int findnext (int i, int *tab, int n)
+int findnext (int i, int n)
 {
-    int c = 0;
-    do {
-        i++;
-        if (i >= n) {
-            i = 0;
-        }
-        c++;
-        if (c > n) {
-            return -1;
-        }
-    } while (tab[i]);
-
+    i++;
+    if (i >= n) {
+        i = 0;
+    }
     return i;
 }
 
@@ -401,14 +393,14 @@ int testposition (board_t *board, block_t *block, int x, int y)
     return 1;
 }
 
-void settleblock (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 i, j;
 
     for (i = 0; i < block->width; i++) {
         for (j = 0; j < block->height; j++) {
             if (*getcell (block, i, j) != ' ') {
-                *getcell (board, x + i, y + j) = ' ' + block->color;
+                *getcell (board, x + i, y + j) = ' ' + ((mode) ? block->color : 0);
             }
         }
     }
index 325005f4cf5c6aa098d9a60b267aa16fe876607b..e1b8529a1d9674499c252a9c534dd91c4720ab97 100644 (file)
@@ -57,9 +57,9 @@ void freeblocks (blocks_t *blocks);
 
 int testposition (board_t *board, block_t *block, int x, int y);
 
-void settleblock (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 *tab, int n);
+int findnext (int i, int n);
 
 #endif /* __FUNCTION_H__ */
 
index b864f0ea19079433c12eb2eed782c13299aa3e19..812c4648d6cfbb4224d023e69a0e3a710d91e58c 100644 (file)
@@ -203,6 +203,12 @@ int main (int argc, char *argv[])
     int xcursor = (board->width - blocks->block[cursor]->width) / 2;
     int ycursor = (board->height - blocks->block[cursor]->height) / 2;
 
+    /* window positions (next) */
+    board_t *next = initboard (wblock + 2 * xoffset, hblock);
+    setscale (next, scale);
+    int xnext = next->xoffset = xboard + board->xsize + xoffset + 2;
+    int ynext = next->yoffset = yboard;
+
     /* window positions (bench) */
     int xbench = bench->xoffset = xboard;
     int ybench = bench->yoffset = yboard + board->ysize + 1 + yoffset;
@@ -228,10 +234,15 @@ int main (int argc, char *argv[])
     while (!stop) {
         boardwindow (board, 0);
 
+        boardwindow (next, 1);
         block_t *block = NULL;
         if (cursor >= 0) {
             block = blocks->block[cursor];
-            displayblock (board, block, xcursor, ycursor);
+            if (blocks->settle[cursor] == 0) {
+                boardwindow (next, 0);
+                displayblock (board, block, xcursor, ycursor);
+            }
+            displayblock (next, block, xoffset + (wblock - block->width) / 2, (hblock - block->height) / 2);
             msgwindow ("Can you solve this puzzle?", xmsg, ymsg, lmsg);
         } else {
             msgwindow ("Puzzle solved", xmsg, ymsg, lmsg);
@@ -268,10 +279,18 @@ int main (int argc, char *argv[])
         case '\n':
         case '\r':
         case 'h':
-            if (testposition (board, block, xcursor, ycursor)) {
-                settleblock (board, block, xcursor, ycursor);
-                blocks->settle[cursor] = 1;
-                cursor = findnext (cursor, blocks->settle, blocks->nb);
+            if (blocks->settle[cursor] == 0) {
+                if (testposition (board, block, xcursor, ycursor)) {
+                    settleblock (board, block, xcursor, ycursor, 1);
+                    blocks->settle[cursor] = 1;
+                    blocks->x[cursor] = xcursor;
+                    blocks->y[cursor] = ycursor;
+                }
+            } else {
+                blocks->settle[cursor] = 0;
+                xcursor = blocks->x[cursor];
+                ycursor = blocks->y[cursor];
+                settleblock (board, block, xcursor, ycursor, 0);
             }
             break;
         case KEY_UP:
@@ -316,7 +335,7 @@ int main (int argc, char *argv[])
             break;
         case ' ':
         case 'y':
-            cursor = findnext (cursor, blocks->settle, blocks->nb);
+            cursor = findnext (cursor, blocks->nb);
             break;
         }
     }