code factorisation
authorLaurent Mazet <mazet@softndesign.org>
Sun, 11 Aug 2024 08:36:09 +0000 (10:36 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 11 Aug 2024 08:36:09 +0000 (10:36 +0200)
display.c
display.h
function.c
function.h
oneplayer.c
twoplayers.c

index 2facdf54592acd0735cccf8321b304e31be4ecbc..8dbce7a84aae627a97e993147f032f8f1dc3b256 100644 (file)
--- a/display.c
+++ b/display.c
@@ -304,4 +304,13 @@ void removelines (board_t *board, int nblines, int tempo)
     }
 }
 
+int processsettlement (board_t *board, block_t *block, int xblock, int yblock, int *pscore, int *plines, int tempo)
+{
+    settleblock (board, block, xblock, yblock);
+    int nblines = checkline (board, pscore, plines);
+    int penalty = computepenalty (nblines);
+    removelines (board, nblines, tempo);
+    return penalty;
+}
+
 /* vim: set ts=4 sw=4 et: */
index cbaef2c09543bca0c081c8565755d40e87157250..6c8dcbfdcfefb8f2e62309ad43eb6c35a3825c16 100644 (file)
--- a/display.h
+++ b/display.h
@@ -20,6 +20,8 @@ void msgwindow (char *msg, int xoffset, int yoffset, int length);
 
 void removelines (board_t *board, int nblines, int tempo);
 
+int processsettlement (board_t *board, block_t *block, int xblock, int yblock, int *pscore, int *plines, int tempo);
+
 #endif /* __DISPLAY_H__ */
 
 /* vim: set ts=4 sw=4 et: */
index 4a0878edafdaeb8c7c082d86974281ec3025c577..c9609fef5ec26f223ed3834e4ab210959c5a25de 100644 (file)
@@ -474,4 +474,9 @@ block_t *drawblock (board_t *board, block_t *blocks, int nb_blocks, block_t *cbl
     return cblock;
 }
 
+int computepenalty (int nblines)
+{
+    return (nblines == 4) ? 4 : (nblines > 1) ? nblines - 1 : 0;
+}
+
 /* vim: set ts=4 sw=4 et: */
index 310d2ab305c8af972ac326c92cca2678cb3061e7..1cb906564b976a5762521920ea5f5330d5e8a89f 100644 (file)
@@ -63,6 +63,8 @@ int addline (board_t *board, int nbholes);
 
 block_t *drawblock (board_t *board, block_t *blocks, int nb_blocks, block_t *cblock, int *current, int *xblock, int *yblock, int *next);
 
+int computepenalty (int nblines);
+
 #endif /* __FUNCTION_H__ */
 
 /* vim: set ts=4 sw=4 et: */
index 1e4f752db5645bee8816286245cf9bffe2166a9b..f29df9afbe2f657123c95f265fe5d83c02357f34 100644 (file)
@@ -137,18 +137,15 @@ int oneplayer (int width, int height, int scale, int chrono, char *filename)
             }
 
             if (settle) {
-                settleblock (board, cblock, xblock, yblock);
-                int nblines = checkline (board, &score, &lines);
-                removelines (board, nblines, computetempo (lines));
-                if (nblines && chrono) {
+                int oldlines = lines;
+                processsettlement (board, cblock, xblock, yblock, &score, &lines, computetempo (lines));
+                if ((lines > oldlines) && chrono) {
                     setendtime (&lineend, chronoratio * speed);
                 }
             }
             if (chrono && lineend.tv_sec && isovertime (&lineend)) {
                 if (!testvalidpos (board, cblock, xblock, yblock + 1)) {
-                    settleblock (board, cblock, xblock, yblock);
-                    int nblines = checkline (board, &score, &lines);
-                    removelines (board, nblines, computetempo (lines));
+                    processsettlement (board, cblock, xblock, yblock, &score, &lines, computetempo (lines));
                     settle = 1;
                 }
                 int isempty = addline (board, nbholes);
index 452e75ba97d3514bea8f6c31fbd925fa0579a696..59b5206c41aa9e30b092d89f910bb0c6b405b0d9 100644 (file)
@@ -25,10 +25,23 @@ static int computetempo (int lines)
     return 20 + 200 / (lines / 20 + 1);
 }
 
-static int computepenalty (int nblines)
+/*
+int reparenextturn (board_t *board, block_t **blocks, int nb_blocks, block_t **pblock, int *pcurrent, int *pxblock, int *pyblock, int *pnext, int player, int *scores, board_t *winblock)
 {
-    return (nblines == 4) ? 4 : (nblines > 1) ? nblines - 1 : 0;
+    char msg[128] = {0};
+    int mode = 1:
+    if (!testvalidpos (board, blocks + *pnext , boardwidth / 2, 0)) {
+        sprintf (msg, "To bad, player %s looses. Score is %d / %d", (player == 0) ? "One" : "Two", scores[0], scores[1]);
+        msgwindow (msg, xmsg, ymsg, lmsg);
+        mode = 2;
+    } else {
+        *pblock = drawblock (board, blocks, nb_blocks, *pblock, pcurrent, pxblock, pyblock, pnext);
+        boardwindow (winblock, 1);
+        displayblock (winblock, blocks + *pnext, -1, -1);
+    }
+    return mode;
 }
+*/
 
 /* main function for two players */
 int twoplayers (int width, int height, int scale)
@@ -41,8 +54,7 @@ int twoplayers (int width, int height, int scale)
     int current_right = -1;
     int lines = 0;
     int next = -1;
-    int score_left = 0;
-    int score_right = 0;
+    int scores[2] = {0, 0};
 
     setscale (board_left, scale);
     setscale (board_right, scale);
@@ -146,12 +158,9 @@ int twoplayers (int width, int height, int scale)
 
             /* Player one */
             if (settle_left) {
-                settleblock (board_left, cblock_left, xblock_left, yblock_left);
-                int nblines_left = checkline (board_left, &score_left, &lines);
-                penalty_right = computepenalty (nblines_left);
-                removelines (board_left, nblines_left, computetempo (lines));
+                penalty_right = processsettlement (board_left, cblock_left, xblock_left, yblock_left, scores + 0, &lines, computetempo (lines));
                 if (!testvalidpos (board_left, blocks + next , board_left->width / 2, 0)) {
-                    sprintf (msg, "To bad, player One looses. Score is %d / %d", score_left, score_right);
+                    sprintf (msg, "To bad, player One looses. Score is %d / %d", scores[0], scores[1]);
                     mode = 2;
                 } else {
                     cblock_left = drawblock (board_left, blocks, nb_blocks, cblock_left, &current_left, &xblock_left, &yblock_left, &next);
@@ -163,12 +172,9 @@ int twoplayers (int width, int height, int scale)
 
             /* Player two */
             if (settle_right) {
-                settleblock (board_right, cblock_right, xblock_right, yblock_right);
-                int nblines_right = checkline (board_right, &score_right, &lines);
-                penalty_left = computepenalty (nblines_right);
-                removelines (board_right, nblines_right, computetempo (lines));
+                penalty_left = processsettlement (board_right, cblock_right, xblock_right, yblock_right, scores + 1, &lines, computetempo (lines));
                 if (!testvalidpos (board_right, blocks + next , board_right->width / 2, 0)) {
-                    sprintf (msg, "To bad, player Two looses. Score is %d / %d", score_left, score_right);
+                    sprintf (msg, "To bad, player Two looses. Score is %d / %d", scores[0], scores[1]);
                     mode = 2;
                 } else {
                     cblock_right = drawblock (board_right, blocks, nb_blocks, cblock_right, &current_right, &xblock_right, &yblock_right, &next);
@@ -184,12 +190,9 @@ int twoplayers (int width, int height, int scale)
                 /* Player one */
                 if (penalty_left) {
                     if (!testvalidpos (board_left, cblock_left, xblock_left, yblock_left + 1)) {
-                        settleblock (board_left, cblock_left, xblock_left, yblock_left);
-                        int nblines_left = checkline (board_left, &score_left, &lines);
-                        penalty_right += computepenalty (nblines_left);
-                        removelines (board_left, nblines_left, computetempo (lines));
+                        penalty_right += processsettlement (board_left, cblock_left, xblock_left, yblock_left, scores + 0, &lines, computetempo (lines));
                         if (!testvalidpos (board_left, blocks + next , board_left->width / 2, 0)) {
-                            sprintf (msg, "To bad, player One looses. Score is %d / %d", score_right, score_left);
+                            sprintf (msg, "To bad, player One looses. Score is %d / %d", scores[1], scores[0]);
                             mode = 2;
                         } else {
                             cblock_left = drawblock (board_left, blocks, nb_blocks, cblock_left, &current_left, &xblock_left, &yblock_left, &next);
@@ -199,7 +202,7 @@ int twoplayers (int width, int height, int scale)
                     }
                     int isempty = addline (board_left, nbholes);
                     if (!isempty) {
-                        sprintf (msg, "Shame, player One looses. Score is %d / %d", score_right, score_left);
+                        sprintf (msg, "Shame, player One looses. Score is %d / %d", scores[1], scores[0]);
                         mode = 2;
                     }
                     penalty_left--;
@@ -208,12 +211,9 @@ int twoplayers (int width, int height, int scale)
                 /* Player two */
                 if (penalty_right) {
                     if (!testvalidpos (board_right, cblock_right, xblock_right, yblock_right + 1)) {
-                        settleblock (board_right, cblock_right, xblock_right, yblock_right);
-                        int nblines_right = checkline (board_right, &score_right, &lines);
-                        penalty_left += computepenalty (nblines_right);
-                        removelines (board_right, nblines_right, computetempo (lines));
+                        penalty_left += processsettlement (board_right, cblock_right, xblock_right, yblock_right, scores + 1, &lines, computetempo (lines));
                         if (!testvalidpos (board_right, blocks + next , board_right->width / 2, 0)) {
-                            sprintf (msg, "To bad, player Two looses. Score is %d / %d", score_left, score_right);
+                            sprintf (msg, "To bad, player Two looses. Score is %d / %d", scores[0], scores[1]);
                             mode = 2;
                         } else {
                             cblock_right = drawblock (board_right, blocks, nb_blocks, cblock_right, &current_right, &xblock_right, &yblock_right, &next);
@@ -223,7 +223,7 @@ int twoplayers (int width, int height, int scale)
                     }
                     int isempty = addline (board_right, nbholes);
                     if (!isempty) {
-                        sprintf (msg, "Shame, player Two looses. Score is %d / %d", score_left, score_right);
+                        sprintf (msg, "Shame, player Two looses. Score is %d / %d", scores[0], scores[1]);
                         mode = 2;
                     }
                     penalty_right--;
@@ -247,7 +247,7 @@ int twoplayers (int width, int height, int scale)
 
         boardwindow (board_left, 0);
         boardwindow (board_right, 0);
-        scorewindow (xscore, yscore, nbdigit, score_left, score_right, (lines / 20) + 1);
+        scorewindow (xscore, yscore, nbdigit, scores[0], scores[1], (lines / 20) + 1);
         int newspeed = 10 + 4900 / (lines / 20 + 1);
         if (newspeed < speed) {
             speed = newspeed;