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;
}
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);
}
}
}
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__ */
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;
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);
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:
break;
case ' ':
case 'y':
- cursor = findnext (cursor, blocks->settle, blocks->nb);
+ cursor = findnext (cursor, blocks->nb);
break;
}
}