#include <stdio.h>
#include <string.h>
-#include "block.h"
#include "function.h"
#include "type.h"
/* constants */
-int height = 20;
-int minwidth = 8;
-int width = 10;
-int maxwidth = 15;
-
int nbdigit = 5;
int maxscor = 100000;
-int nbholes = 2;
int savelen = 12;
int xoffset = 1;
int yoffset = 1;
-/* blocks */
-
-#define _nb_blocks_std 12
-
-block_t _blocks_std[_nb_blocks_std] = {
- {3, 3, yellow, " .... . "}, // F
- {1, 5, blue, "...."}, // I
- {2, 4, lightmagenta, ". . . .."}, // L
- {2, 4, lightblue, " .... . "}, // N
- {2, 3, lightgreen, " ....."}, // P
- {3, 3, lightcyan, "... . . "}, // T
- {3, 2, magenta, ". ...."}, // U
- {3, 3, cyan, ". . ..."}, // V
- {3, 3, lightergreen, ". .. .."}, // W
- {3, 3, red, " . ... . "}, // X
- {2, 4, darkblue, " ... . ."}, // Y
- {3, 3, brown, ".. . .."}, // Z
-};
-
-block_t *getblocks (char *name, int *nb)
-{
- block_t *pt = NULL;
-
- if (strcmp (name, "std") == 0) {
- pt = _blocks_std;
- *nb = _nb_blocks_std;
- }
-
- return pt;
-}
-
/* board */
board_t *getboard (char *name)
{
board_t *board = NULL;
- if (strcmp (name, "6x10") == 0) {
- board = initboard (6, 10);
- } else if (strcmp (name, "5x12") == 0) {
- board = initboard (5, 12);
- } else if (strcmp (name, "4x15") == 0) {
- board = initboard (4, 15);
- } else if (strcmp (name, "3x20") == 0) {
- board = initboard (3, 20);
+ if (strcmp (name, "6x6") == 0) {
+ board = initboard (6, 6);
+ } else if (strcmp (name, "8x8") == 0) {
+ board = initboard (8, 8);
+ } else if (strcmp (name, "10x10") == 0) {
+ board = initboard (10, 10);
+ } else if (strcmp (name, "12x12") == 0) {
+ board = initboard (12, 12);
} else if (strcmp (name, "list") == 0) {
- printf ("board: 6x10 5x12 4x15 3x20\n");
+ printf ("board: 6x6 8x8 10x10 12x12\n");
board = (board_t *)(-1);
}
+ *getcell (board, board->width / 2 - 1, board->height / 2 - 1) = '0';
+ *getcell (board, board->width / 2 - 0, board->height / 2 - 1) = '1';
+ *getcell (board, board->width / 2 - 1, board->height / 2 - 0) = '1';
+ *getcell (board, board->width / 2 - 0, board->height / 2 - 0) = '0';
+
return board;
}
#endif /* __CONSTANT_C__ */
-/* block definitions */
-
-block_t *getblocks (char *name, int *nb);
-
board_t *getboard (char *name);
#endif /* __CONSTANT_H__ */
case ' ':
mvaddcb (y, x, black);
break;
- case '1':
- mvaddcb (y, x, cyan);
- break;
- case '2':
- mvaddcb (y, x, yellow);
- break;
- case '3':
- mvaddcb (y, x, magenta);
- break;
- case '4':
- mvaddcb (y, x, brown);
- break;
- case '5':
+ case '0':
mvaddcb (y, x, blue);
break;
- case '6':
+ case '1':
mvaddcb (y, x, red);
break;
- case '7':
- mvaddcb (y, x, green);
+ case '2':
+ mvaddcb (y, x, lighterblue);
break;
- case '8':
- mvaddcb (y, x, white);
+ case '3':
+ mvaddcb (y, x, lighterred);
break;
}
}
}
}
-void displayblock (board_t *board, block_t *block, int x, int y)
+void displaycursor (board_t *board, int id, int x, int y)
{
- int i, j;
-
- if (x == -1) {
- x = board->width / 2;
- }
-
- if (y == -1) {
- y = (board->height - block->height) / 2;
- }
-
- for (i = 0; i < block->width; i++) {
- for (j = 0; j < block->height; j++) {
- if (*getcell (block, i, j) != ' ') {
- _element (board, x - block->width / 2 + i, y + j, '0' + block->color);
- }
- }
- }
+ _element (board, x, y, (id == 0) ? '2' : '3');
}
char *savewindow (int length, int xoffset, int yoffset)
void boardwindow (board_t *board, int mode);
-void displayblock (board_t *board, block_t *block, int x, int y);
+void displaycursor (board_t *board, int id, int x, int y);
char *savewindow (int length, int xoffset, int yoffset);
board->ysize = board->height = height;
board->xoffset = 0;
board->yoffset = 0;
- board->current = -1;
- board->next = -1;
return board;
}
int l = sprintf (buffer, "width: %d\n", board->width);
l += sprintf (buffer + l, "height: %d\n", board->height);
l += sprintf (buffer + l, "tab: \"%s\"\n", board->tab);
- l += sprintf (buffer + l, "current: %d\n", board->current);
- l += sprintf (buffer + l, "lines: %d\n", board->lines);
- l += sprintf (buffer + l, "next: %d\n", board->next);
- l += sprintf (buffer + l, "score: %d\n", board->score);
VERBOSE (INFO, _makecomments (buffer + l, board));
int width = 0;
int height = 0;
char *tab = NULL;
- int current = -1;
- int lines = 0;
- int next = -1;
- int score = 0;
char *saveptr1, *saveptr2;
height = atoi (value);
} else if (strcmp (keyword, "tab") == 0) {
tab = atos (value);
- } else if (strcmp (keyword, "current") == 0) {
- current = atoi (value);
- } else if (strcmp (keyword, "lines") == 0) {
- lines = atoi (value);
- } else if (strcmp (keyword, "next") == 0) {
- next = atoi (value);
- } else if (strcmp (keyword, "score") == 0) {
- score = atoi (value);
} else if (strcmp (keyword, "rem") == 0) {
/* nothing to do with remark */
} else {
if ((tab) && (strlen (tab) == (size_t)(width * height))) {
board = initboard (width, height);
memcpy (board->tab, tab, width * height);
- board->current = current;
- board->lines = lines;
- board->next = next;
- board->score = score;
}
return board;
}
-block_t *initblock (int width, int height)
-{
- block_t *block = (block_t *) malloc (sizeof (block_t));
- CHECKALLOC (block);
- block->tab = (char *) calloc (1, width * height + 1);
- CHECKALLOC (block->tab);
- memset (block->tab, ' ', width * height);
- block->width = width;
- block->height = height;
- return block;
-}
-
-block_t *changeblock (block_t *dest, block_t *src)
-{
- block_t *ret = NULL;
- if (dest && src) {
- free (dest->tab);
- memcpy (dest, src, sizeof (block_t));
- dest->tab = strdup (src->tab);
- CHECKALLOC (dest->tab);
- ret = dest;
- }
- return ret;
-}
-
-block_t *copyblock (block_t *block)
-{
- block_t *newblock = initblock (block->width, block->height);
- char *tab = newblock->tab;
- memcpy (newblock, block, sizeof (block_t));
- newblock->tab = tab;
- memcpy (newblock->tab, block->tab, block->width * block->height + 1);
- return newblock;
-}
-
-void freeblock (block_t *block)
-{
- if (block) {
- free (block->tab);
- }
- free (block);
-}
-
-block_t *rotateblock (block_t *block, int rot)
-{
- int i, j;
-
- rot = (rot > 0) ? rot % 4 : ((1 - rot / 4) * 4 + rot) % 4;
-
- block_t *newblock = NULL;
-
- switch (rot) {
- case 0:
- newblock = copyblock (block);
- break;
- case 1:
- newblock = initblock (block->height, block->width);
- newblock->color = block->color;
- for (i = 0; i < block->width; i++) {
- for (j = 0; j < block->height; j++) {
- *getcell (newblock, block->height - 1 - j, i) = *getcell (block, i, j);
- }
- }
- break;
- case 2:
- newblock = initblock (block->width, block->height);
- newblock->color = block->color;
- for (i = 0; i < block->width; i++) {
- for (j = 0; j < block->height; j++) {
- *getcell (newblock, block->width - 1 - i, block->height - 1 - j) = *getcell (block, i, j);
- }
- }
- break;
- case 3:
- newblock = initblock (block->height, block->width);
- newblock->color = block->color;
- for (i = 0; i < block->width; i++) {
- for (j = 0; j < block->height; j++) {
- *getcell (newblock, j, block->width - 1 - i) = *getcell (block, i, j);
- }
- }
- break;
- }
-
- changeblock (block, newblock);
- freeblock (newblock);
-
- return block;
-}
-
/* vim: set ts=4 sw=4 et: */
board_t *loadboard (char *str);
-block_t *initblock (int width, int height);
-
-block_t *changeblock (block_t *dest, block_t *src);
-
-block_t *copyblock (block_t *block);
-
-void freeblock (block_t *block);
-
-block_t *rotateblock (block_t *block, int rot);
-
#endif /* __FUNCTION_H__ */
/* vim: set ts=4 sw=4 et: */
char *filename = NULL;
int scale = 1;
-char *boardname = "6x10";
+char *boardname = "8x8";
char *help =
"<i> Move up cursor\n"
/* window positions */
int xboard = board->xoffset = xoffset + 1;
int yboard = board->yoffset = xoffset + 1;
- int xhelp = xboard + xoffset + 1 + board->xsize;
+ int xscore = xboard + xoffset + 1 + board->xsize;
+ int yscore = yboard;
+ int xhelp = xscore + xoffset;
+ int yhelp = yscore - 1;
int xcursor = 0;
int ycursor = 0;
- int yhelp = yboard - 1;
int xsave = max (xboard + (board->xsize - savelen) / 2, 1);
int ysave = yboard + (board->ysize - 1) / 2;
char *savename = NULL;
int ymsg = max (yboard + xoffset + 1 + board->ysize, yhelp + lhelp + yoffset + 1);
int lmsg = xhelp - xmsg + strmaxlen (help, '\n');
+ /* main loop */
+ int stop = 0;
+ int id = 0;
+ int mode = 0;
+ while (!stop) {
+ boardwindow (board, 0);
+ displaycursor (board, id, xcursor, ycursor);
+ int ch = getch ();
+ /* general controls */
+ switch (ch) {
+ case KEY_ESC:
+ case 'q':
+ stop = 1;
+ break;
+ case 's':
+ savename = savewindow (savelen, xsave, ysave);
+ if (savename != NULL) {
+ char *ptr = saveboard (board);
+ if (writedata (savename, ptr)) {
+ VERBOSE (WARNING, printf ("issue writing Board\n"));
+ }
+ free (ptr);
+ free (savename);
+ }
+ break;
+ }
+ /* test end of game */
+ if (mode == 1) {
+ continue;
+ }
+
+ /* game controls */
+ switch (ch) {
+ case '\n':
+ case '\r':
+ case ' ':
+ id = (id == 0) ? 1 : 0;
+ break;
+ case KEY_UP:
+ case 'i':
+ if (ycursor > 0) {
+ ycursor--;
+ }
+ break;
+ case KEY_LEFT:
+ case 'j':
+ if (xcursor > 0) {
+ xcursor--;
+ }
+ break;
+ case KEY_DOWN:
+ case 'k':
+ if (ycursor < board->height - 1) {
+ ycursor++;
+ }
+ break;
+ case KEY_RIGHT:
+ case 'l':
+ if (xcursor < board->width - 1) {
+ xcursor++;
+ }
+ break;
+ }
+
+ }
endwin ();
int ysize;
int xoffset;
int yoffset;
- int current;
- int lines;
- int next;
- int score;
} board_t;
-typedef struct {
- int width;
- int height;
- int color;
- char *tab;
-} block_t;
-
#endif /* __TYPE_H__ */
/* vim: set ts=4 sw=4 et: */