more code
authorLaurent Mazet <mazet@softndesign.org>
Sun, 21 Jul 2024 20:01:15 +0000 (22:01 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 21 Jul 2024 20:01:15 +0000 (22:01 +0200)
constant.c
constant.h
function.c
function.h
tetris.c
type.h

index eb0f8ee00620f80c418e10ad634252912ff9a001..0fcb035753002227ce2a0d089156c7954578aeb7 100644 (file)
@@ -5,4 +5,28 @@
 
 #include "constant.h"
 
+#define _nb_blocks_std 6
+
+block_t _blocks_std[_nb_blocks_std] = {
+    {3, 2, 2, " .... "},
+    {3, 2, 3, "..  .."},
+    {2, 3, 4, ". . .."},
+    {2, 3, 5, " . ..."},
+    {2, 2, 6, "...."},
+    {1, 4, 7, "...."}
+};
+
+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;
+}
+
+
 /* vim: set ts=4 sw=4 et: */
index e270e129c300189dd292ed5c15d5ce470d72b28c..b31eb6f0249fb0a98ca0c29b1064f8dd0bc352d6 100644 (file)
@@ -3,6 +3,8 @@
 
 #include "type.h"
 
+block_t *getblocks (char *name, int *nb);
+
 #endif /* __CONSTANT_H__ */
 
 /* vim: set ts=4 sw=4 et: */
index d12ca6fd63c6cb45939e5b6deca9caf27e3370e3..154833d7f4cb5b688680d4485961df8e4a21f4fb 100644 (file)
@@ -29,14 +29,12 @@ board_t *initboard (int width, int height)
     board->tab = (char *) calloc (1, width * height + 1);
     CHECKALLOC (board->tab);
     memset (board->tab, ' ', width * height);
-    board->current = (char *) calloc (1, width * height + 1);
-    CHECKALLOC (board->current);
-    memset (board->current, ' ', width * height);
     board->scale = 0;
     board->xsize = board->width = width;
     board->ysize = board->height = height;
     board->xoffset = 0;
     board->yoffset = 0;
+    board->current = -1;
     board->next = -1;
     return board;
 }
@@ -45,12 +43,9 @@ board_t *copyboard (board_t *board)
 {
     board_t *newboard = initboard (board->width, board->height);
     char *tab = newboard->tab;
-    char *current = newboard->current;
     memcpy (newboard, board, sizeof (board_t));
     newboard->tab = tab;
-    newboard->current = current;
     memcpy (newboard->tab, board->tab, board->width * board->height + 1);
-    memcpy (newboard->current, board->current, board->width * board->height + 1);
     return newboard;
 }
 
@@ -82,7 +77,6 @@ void freeboard (board_t *board)
 {
     if (board) {
         free (board->tab);
-        free (board->current);
     }
     free (board);
 }
@@ -102,7 +96,7 @@ int _makecomments (char *buffer, board_t *board)
 
 char *saveboard (board_t *board)
 {
-    int size = 3 * (8 + 3) + 8 + board->width * board->height + 1;
+    int size = 4 * (8 + 3) + 8 + board->width * board->height + 1;
     VERBOSE (INFO, size += board->height * (8 + board->width));
 
     char *buffer = (char *) calloc (size, 1);
@@ -111,7 +105,7 @@ char *saveboard (board_t *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: \"%s\"\n", board->current);
+    l += sprintf (buffer + l, "current: \"%d\"\n", board->current);
     l += sprintf (buffer + l, "next: %d\n", board->next);
 
     VERBOSE (INFO, _makecomments (buffer + l, board));
@@ -179,7 +173,7 @@ board_t *loadboard (char *str)
     int width = 0;
     int height = 0;
     char *tab = NULL;
-    char *current = NULL;
+    int current = -1;
     int next = -1;
 
     char *saveptr1, *saveptr2;
@@ -201,7 +195,7 @@ board_t *loadboard (char *str)
         } else if (strcmp (keyword,  "tab") == 0) {
             tab = atos (value);
         } else if (strcmp (keyword,  "current") == 0) {
-            current = atos (value);
+            current = atoi (value);
         } else if (strcmp (keyword,  "next") == 0) {
             next = atoi (value);
         } else if (strcmp (keyword,  "rem") == 0) {
@@ -214,25 +208,101 @@ board_t *loadboard (char *str)
     }
 
     board_t *board = NULL;
-    if ((tab) && (strlen (tab) == (size_t)(width * height)) &&
-        (current) && (strlen (current) == (size_t)(width * height))) {
+    if ((tab) && (strlen (tab) == (size_t)(width * height))) {
         board = initboard (width, height);
         memcpy (board->tab, tab, width * height);
-        memcpy (board->current, current, width * height);
+        board->current = current;
         board->next = next;
     }
 
     return board;
 }
 
-char *getcell (board_t *board, int x, int y)
+block_t *initblock (int width, int height)
 {
-    return board->tab + x + board->width * y;
+    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;
 }
 
-char getvalue (board_t *board, int x, int y)
+block_t *changeblock (block_t *dest, block_t *src)
 {
-    return (x >= 0) && (x < board->width) && (y >= 0) && (y < board->height) ? *getcell (board, x, y) : 0;
+    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 *rotateelement (block_t *element, int rot)
+{
+    int i, j;
+
+    rot = (rot > 0) ? rot % 4 : ((1 - rot / 4) * 4 + rot) % 4;
+
+    block_t *newelement = NULL;
+
+    switch (rot) {
+    case 0:
+        newelement = copyblock (element);
+        break;
+    case 1:
+        newelement = initblock (element->height, element->width);
+        for (i = 0; i < element->width; i++) {
+            for (j = 0; j < element->height; j++) {
+                *getcell (newelement, element->height - 1 - j, i) = *getcell (element, i, j);
+            }
+        }
+        break;
+    case 2:
+        newelement = initblock (element->width, element->height);
+        for (i = 0; i < element->width; i++) {
+            for (j = 0; j < element->height; j++) {
+                *getcell (newelement, element->width - 1 - i, element->height - 1 - j) = *getcell (element, i, j);
+            }
+        }
+        break;
+    case 3:
+        newelement = initblock (element->height, element->width);
+        for (i = 0; i < element->width; i++) {
+            for (j = 0; j < element->height; j++) {
+                *getcell (newelement, j, element->width - 1 - i) = *getcell (element, i, j);
+            }
+        }
+        break;
+    }
+
+    changeblock (element, newelement);
+    freeblock (newelement);
+
+    return element;
 }
 
 /* vim: set ts=4 sw=4 et: */
index b0d4842796cd7e517e093842f29926863288d5ec..883808e528a15e8c93cf3c0bc5a1f48d91aac154 100644 (file)
@@ -37,9 +37,19 @@ char *readdata (char *filename);
 
 board_t *loadboard (char *str);
 
-char *getcell (board_t *board, int x, int y);
+#define getcell(b, x, y) ({ __typeof__ (b) _b = (b); int _x = (x), _y = (y);_b->tab + _x + _b->width * _y; })
 
-char getvalue (board_t *board, int x, int y);
+#define getvalue(b, x, y) ({ __typeof__ (b) _b = (b); int _x = (x), _y = (y); (_x >= 0) && (_x < _b->width) && (_y >= 0) && (_y < _b->height) ? *getcell (_b, _x, _y) : 0; })
+
+block_t *initblock (int xsize, int ysize);
+
+block_t *changeblock (block_t *dest, block_t *src);
+
+block_t *copyblock (block_t *block);
+
+void freeblock (block_t *block);
+
+block_t *rotateelement (block_t *element, int rot);
 
 #endif /* __FUNCTION_H__ */
 
index 22c6b24077440c4ce2911f9873b89f81fd071c48..da41f710c99ab1fc07ac0325a04a362c7f3016f6 100644 (file)
--- a/tetris.c
+++ b/tetris.c
@@ -130,6 +130,12 @@ int main (int argc, char *argv[])
     }
     setscale (board, scale);
 
+    /* get blocks */
+    int nb_blocks = 0;
+    block_t *block_t = getblocks ("std", &nb_blocks);
+    int current = rand () % nb_blocks;
+    int next = rand () % nb_blocks;
+
     /* init curses window */
     initscr ();
     noecho ();
@@ -160,6 +166,8 @@ int main (int argc, char *argv[])
 
     /* event loop */
     int mode = 0;
+    int speed = 255;
+
     int stop = 0;
     while (!stop) {
 
@@ -168,9 +176,11 @@ int main (int argc, char *argv[])
             char msg[128] = {0};
             switch (mode) {
             case 0:
+                halfdelay (0);
                 sprintf (msg, "Get ready player One");
                 break;
             case 1:
+                halfdelay (speed);
                 break;
             }
             msgwindow (msg, xmsg, ymsg, lmsg);
diff --git a/type.h b/type.h
index 3c66510dbea04c5a436350dcaf3f85bca05639e8..3b4478415a2362750f143e46660c3cc8f8e52d6d 100644 (file)
--- a/type.h
+++ b/type.h
@@ -5,15 +5,22 @@ typedef struct {
     int width;
     int height;
     char *tab;
-    char *current;
     int scale;
     int xsize;
     int ysize;
     int xoffset;
     int yoffset;
+    int current;
     int next;
 } board_t;
 
+typedef struct {
+    int width;
+    int height;
+    int color;
+    char *tab;
+} block_t;
+
 #endif /* __TYPE_H__ */
 
 /* vim: set ts=4 sw=4 et: */