From 0c13aa2cbdb231dfe5ae6a9362cbda89617e8375 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Mon, 19 Aug 2024 00:17:42 +0200 Subject: [PATCH] every thing is working --- function.c | 22 +++++++--------------- function.h | 4 ++-- pentomino.c | 31 +++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/function.c b/function.c index ce21140..fdd4f0b 100644 --- a/function.c +++ b/function.c @@ -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); } } } diff --git a/function.h b/function.h index 325005f..e1b8529 100644 --- a/function.h +++ b/function.h @@ -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__ */ diff --git a/pentomino.c b/pentomino.c index b864f0e..812c464 100644 --- a/pentomino.c +++ b/pentomino.c @@ -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; } } -- 2.30.2