code factorisation
authorLaurent Mazet <mazet@softndesign.org>
Sun, 11 Aug 2024 12:50:34 +0000 (14:50 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 11 Aug 2024 12:50:34 +0000 (14:50 +0200)
display.c
display.h
oneplayer.c
twoplayers.c

index 8dbce7a84aae627a97e993147f032f8f1dc3b256..509ca09727ccc99f8737cd97ab88843f5e8fe776 100644 (file)
--- a/display.c
+++ b/display.c
@@ -313,4 +313,22 @@ int processsettlement (board_t *board, block_t *block, int xblock, int yblock, i
     return penalty;
 }
 
+int reparenextturn (board_t *board, block_t *blocks, int nb_blocks, block_t **pblock, int *pcurrent, int *pxblock, int *pyblock, int *pnext, int player, char *msg, int *scores, board_t *winblock)
+{
+    int mode = 1;
+    if (!testvalidpos (board, blocks + *pnext, board->width / 2, 0)) {
+        if (player == 0) {
+            sprintf (msg, "To bad, you loose. Score is %d", *scores);
+        } else {
+            sprintf (msg, "To bad, %s. Score is %d / %d", (player == 1) ? "player One losses" : "player Two losses", scores[0], scores[1]);
+        }
+        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;
+}
+
 /* vim: set ts=4 sw=4 et: */
index 6c8dcbfdcfefb8f2e62309ad43eb6c35a3825c16..2b3db4cce912cac9ec4484f9b3341160072a1224 100644 (file)
--- a/display.h
+++ b/display.h
@@ -22,6 +22,8 @@ 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);
 
+int reparenextturn (board_t *board, block_t *blocks, int nb_blocks, block_t **pblock, int *pcurrent, int *pxblock, int *pyblock, int *pnext, int player, char *msg, int *scores, board_t *winblock);
+
 #endif /* __DISPLAY_H__ */
 
 /* vim: set ts=4 sw=4 et: */
index f29df9afbe2f657123c95f265fe5d83c02357f34..29405c6018881736d82e0fcf2567d60d42e56620 100644 (file)
@@ -157,16 +157,9 @@ int oneplayer (int width, int height, int scale, int chrono, char *filename)
                 setendtime (&lineend, chronoratio * speed);
             }
             if (settle) {
-                if (!testvalidpos (board, blocks + next , board->width / 2, 0)) {
-                    sprintf (msg, "To bad, you loose. Score is %d", score);
-                    mode = 2;
-                } else {
-                    cblock = drawblock (board, blocks, nb_blocks, cblock, &current, &xblock, &yblock, &next);
-                    boardwindow (nextblock, 1);
-                    displayblock (nextblock, blocks + next, -1, -1);
-                }
-                settle = 0;
+                mode = reparenextturn (board, blocks, nb_blocks, &cblock, &current, &xblock, &yblock, &next, 0, msg, &score, nextblock);
             }
+            settle = 0;
 
             break;
         case 2:
index 59b5206c41aa9e30b092d89f910bb0c6b405b0d9..7a5a503e5c3a331aea901a01f5965aff98ef7b5a 100644 (file)
@@ -25,24 +25,6 @@ static int computetempo (int lines)
     return 20 + 200 / (lines / 20 + 1);
 }
 
-/*
-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)
-{
-    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)
 {
@@ -159,30 +141,16 @@ int twoplayers (int width, int height, int scale)
             /* Player one */
             if (settle_left) {
                 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", scores[0], scores[1]);
-                    mode = 2;
-                } else {
-                    cblock_left = drawblock (board_left, blocks, nb_blocks, cblock_left, &current_left, &xblock_left, &yblock_left, &next);
-                    boardwindow (nextblock, 1);
-                    displayblock (nextblock, blocks + next, -1, -1);
-                }
-                settle_left = 0;
+                mode = reparenextturn (board_left, blocks, nb_blocks, &cblock_left, &current_left, &xblock_left, &yblock_left, &next, 1, msg, scores, nextblock);
             }
+            settle_left = 0;
 
             /* Player two */
             if (settle_right) {
                 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", scores[0], scores[1]);
-                    mode = 2;
-                } else {
-                    cblock_right = drawblock (board_right, blocks, nb_blocks, cblock_right, &current_right, &xblock_right, &yblock_right, &next);
-                    boardwindow (nextblock, 1);
-                    displayblock (nextblock, blocks + next, -1, -1);
-                }
-                settle_right = 0;
+                mode = reparenextturn (board_right, blocks, nb_blocks, &cblock_right, &current_right, &xblock_right, &yblock_right, &next, 2, msg, scores, nextblock);
             }
+            settle_right = 0;
 
             /* Penalty */
             while ((penalty_left > 0) || (penalty_right > 0)) {
@@ -191,14 +159,7 @@ int twoplayers (int width, int height, int scale)
                 if (penalty_left) {
                     if (!testvalidpos (board_left, cblock_left, xblock_left, yblock_left + 1)) {
                         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", scores[1], scores[0]);
-                            mode = 2;
-                        } else {
-                            cblock_left = drawblock (board_left, blocks, nb_blocks, cblock_left, &current_left, &xblock_left, &yblock_left, &next);
-                            boardwindow (nextblock, 1);
-                            displayblock (nextblock, blocks + next, -1, -1);
-                        }
+                        mode = reparenextturn (board_left, blocks, nb_blocks, &cblock_left, &current_left, &xblock_left, &yblock_left, &next, 1, msg, scores, nextblock);
                     }
                     int isempty = addline (board_left, nbholes);
                     if (!isempty) {
@@ -212,14 +173,7 @@ int twoplayers (int width, int height, int scale)
                 if (penalty_right) {
                     if (!testvalidpos (board_right, cblock_right, xblock_right, yblock_right + 1)) {
                         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", scores[0], scores[1]);
-                            mode = 2;
-                        } else {
-                            cblock_right = drawblock (board_right, blocks, nb_blocks, cblock_right, &current_right, &xblock_right, &yblock_right, &next);
-                            boardwindow (nextblock, 1);
-                            displayblock (nextblock, blocks + next, -1, -1);
-                        }
+                        mode = reparenextturn (board_right, blocks, nb_blocks, &cblock_right, &current_right, &xblock_right, &yblock_right, &next, 2, msg, scores, nextblock);
                     }
                     int isempty = addline (board_right, nbholes);
                     if (!isempty) {