corrections and tests
authorLaurent Mazet <mazet@softndesign.org>
Wed, 24 Jul 2024 21:49:31 +0000 (23:49 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Wed, 24 Jul 2024 21:49:31 +0000 (23:49 +0200)
function.c
function.h
tetris.c
type.h

index 1eb7098f0d9a4bde4b95b278d2a24662c603e48f..778064bc805395a202073b046c0b20ec27335d3d 100644 (file)
@@ -39,16 +39,6 @@ board_t *initboard (int width, int height)
     return board;
 }
 
-board_t *copyboard (board_t *board)
-{
-    board_t *newboard = initboard (board->width, board->height);
-    char *tab = newboard->tab;
-    memcpy (newboard, board, sizeof (board_t));
-    newboard->tab = tab;
-    memcpy (newboard->tab, board->tab, board->width * board->height + 1);
-    return newboard;
-}
-
 board_t *setscale (board_t *board, int scale)
 {
     board->scale = scale;
@@ -105,8 +95,10 @@ 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: \"%d\"\n", board->current);
+    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));
 
@@ -174,7 +166,9 @@ board_t *loadboard (char *str)
     int height = 0;
     char *tab = NULL;
     int current = -1;
+    int lines = 0;
     int next = -1;
+    int score = 0;
 
     char *saveptr1, *saveptr2;
 
@@ -196,8 +190,12 @@ board_t *loadboard (char *str)
             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 {
@@ -212,7 +210,9 @@ board_t *loadboard (char *str)
         board = initboard (width, height);
         memcpy (board->tab, tab, width * height);
         board->current = current;
+        board->lines = lines;
         board->next = next;
+        board->score = score;
     }
 
     return board;
index 072049eed56610767b73aa5e1ed24f740eb9fd1a..86420f9beab1bfd1895e6400fb0ca6b97c815f2f 100644 (file)
@@ -23,8 +23,6 @@ board_t *initboard (int xsize, int ysize);
 
 board_t *initplay (board_t *board);
 
-board_t *copyboard (board_t *board);
-
 board_t *setscale (board_t *board, int scale);
 
 void freeboard (board_t *board);
index a94199dc55d2dff00a2bc26d502698b811ad027e..121ec1d73da6760828afdc7f2fd12e80d4f65fb8 100644 (file)
--- a/tetris.c
+++ b/tetris.c
@@ -132,7 +132,14 @@ int main (int argc, char *argv[])
         return 1;
     }
 
+    /* init all variables */
     board_t *board = NULL;
+    int current = -1;
+    int lines = 0;
+    int next = -1;
+    int score = 0;
+
+    /* read file */
     if (filename) {
         char *buffer = readdata (filename);
         if (buffer == NULL) {
@@ -145,6 +152,10 @@ int main (int argc, char *argv[])
             VERBOSE (ERROR, fprintf (stderr, "incorrect file (%s)\n", filename));
             return 1;
         }
+        current = board->current;
+        lines = board->lines;
+        next = board->next;
+        score = board->score;
     } else {
         board = initboard (width, height);
     }
@@ -156,12 +167,19 @@ int main (int argc, char *argv[])
     board_t *nextblock = initboard (maxblockwidth (blocks, nb_blocks) + 2, maxblockheight (blocks, nb_blocks));
     setscale (nextblock, scale);
 
+    /* init seed */
+    srand (time (NULL));
+
     /* get first bloc */
-    int current = rand () % nb_blocks;
+    if (current == -1) {
+        current = rand () % nb_blocks;
+    }
     block_t *cblock = copyblock (blocks + current);
     int xblock = board->width / 2;
     int yblock = 0;
-    int next = rand () % nb_blocks;
+    if (next == -1) {
+        next = rand () % nb_blocks;
+    }
     VERBOSE (DEBUG, fprintf (stderr, "%d/%d: (%d, %d)\n", current, nb_blocks, xblock, yblock));
 
     /* init curses window */
@@ -195,9 +213,7 @@ int main (int argc, char *argv[])
     int lmsg = xhelp - xmsg + strmaxlen (help, '\n');
 
     /* event loop */
-    int level = 0;
     int mode = -1;
-    int score = 0;
     int settle = 0;
     int speed = 5000;
     int stop = 0;
@@ -252,14 +268,14 @@ int main (int argc, char *argv[])
         }
 
         msgwindow (msg, xmsg, ymsg, lmsg);
-        int nblines = checkline (board, &score, &level);
+        int nblines = checkline (board, &score, &lines);
         while (nblines-- > 0) {
             boardwindow (board, 0);
             stackboard (board);
         }
         boardwindow (board, 0);
-        scorewindow (xscore, yscore, nbdigit, score, (level / 10) + 1);
-        int newspeed = 10 + 4900 / (level / 10 + 1);
+        scorewindow (xscore, yscore, nbdigit, score, (lines / 10) + 1);
+        int newspeed = 10 + 4900 / (lines / 10 + 1);
         if (newspeed != speed) {
             speed = newspeed;
             setendtime (&tend, speed);
@@ -350,6 +366,10 @@ int main (int argc, char *argv[])
         case 's':
             savename = savewindow (savelen, xsave, ysave);
             if (savename != NULL) {
+                board->current = current;
+                board->lines = lines;
+                board->next = next;
+                board->score = score;
                 char *ptr = saveboard (board);
                 if (writedata (savename, ptr)) {
                     VERBOSE (WARNING, printf ("issue writing Board\n"));
@@ -371,12 +391,23 @@ int main (int argc, char *argv[])
 }
 
 /* test: tetris.exe -f 2>&1 | grep 'no file' */
-/* test: tetris.exe -f tests/nofile.ttr 2>&1 | grep "can't read file" */
-/* test: tetris.exe -f tests/bogus.ttr 2>&1 | grep 'incorrect file' */
+/* test: tetris.exe -f nofile.ttr 2>&1 | grep "can't read file" */
+/* test: tetris.exe -f bogus.ttr 2>&1 | grep 'incorrect file' */
 /* test: tetris.exe -h | grep usage */
 /* test: tetris.exe -s 2>&1 | grep 'no scale' */
 /* test: tetris.exe -s 4 2>&1 | grep incorrect */
 /* test: tetris.exe -v 2>&1 | grep missing */
 /* test: tetris.exe _ 2>&1 | grep invalid */
+/* test: { sleep 1; echo -n s; sleep 1; echo ouuljk; sleep 1; echo q; } | tetris.exe -s 0 */
+/* test: { sleep 1; echo -n s; sleep 1; echo ouuljk; sleep 1; echo q; } | tetris.exe -s 1 */
+/* test: { sleep 1; echo -n s; sleep 1; echo ouuljk; sleep 1; echo q; } | tetris.exe -s 2 */
+/* test: { sleep 1; echo -n s; sleep 1; echo ouuljk; sleep 1; echo q; } | tetris.exe -s 3 */
+/* test: { sleep 1; echo -n skkkkkkkkkkkkkkkkkk; sleep 6; echo -n kkkkkkkkkkkkkkkkk; sleep 1; echo q; } | tetris.exe */
+/* test: { sleep 1; echo -n s; sleep 1; echo oup; sleep 1; echo puljk; sleep 1; echo -ne 'sb.ttr\e'; sleep 1; echo -e 'sab\b.ttr'; sleep 1; echo q; } | tetris.exe -v 3 */
+/* test: { sleep 1; echo -n s; sleep 1; echo q; } | tetris.exe -f a.ttr && rm a.ttr && test \! -f b.ttr */
+/* test: { echo -n s; sleep 1; echo ooolllk; sleep 1; echo uuukjjj; sleep 1; echo ooolllk; sleep 1; echo uuukjjj; sleep 1; echo ooolllk; sleep 1; echo uuukjjj; sleep 1; echo ooolllk; sleep 1; echo uuukjjj; sleep 1; echo cccc; sleep 1; echo q; } | tetris.exe */
+/* test: { echo -n s; sleep 1; echo lllololo; sleep 1; echo jjjujuju; sleep 1; echo lllololo; sleep 1; echo jjjujuju; sleep 1; echo q; } | tetris.exe */
+/* test: { echo -n s; sleep 1; echo ccccccccccccc; sleep 1; echo q; } | tetris.exe */
+/* test: { echo -n s; sleep 1; echo -n jjuuc; sleep 1; echo -n jjc; sleep 1; echo q; } | tetris.exe -f lines.ttr */
 
 /* vim: set ts=4 sw=4 et: */
diff --git a/type.h b/type.h
index 3b4478415a2362750f143e46660c3cc8f8e52d6d..199c75f7edc3501d218feccc8914e6c495e2450a 100644 (file)
--- a/type.h
+++ b/type.h
@@ -11,7 +11,9 @@ typedef struct {
     int xoffset;
     int yoffset;
     int current;
+    int lines;
     int next;
+    int score;
 } board_t;
 
 typedef struct {