From 3c76c53409cfa3c1a0520414a51472f851f5d84a Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 11 Aug 2024 10:36:09 +0200 Subject: [PATCH] code factorisation --- display.c | 9 +++++++++ display.h | 2 ++ function.c | 5 +++++ function.h | 2 ++ oneplayer.c | 11 ++++------- twoplayers.c | 54 ++++++++++++++++++++++++++-------------------------- 6 files changed, 49 insertions(+), 34 deletions(-) diff --git a/display.c b/display.c index 2facdf5..8dbce7a 100644 --- 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: */ diff --git a/display.h b/display.h index cbaef2c..6c8dcbf 100644 --- 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: */ diff --git a/function.c b/function.c index 4a0878e..c9609fe 100644 --- a/function.c +++ b/function.c @@ -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: */ diff --git a/function.h b/function.h index 310d2ab..1cb9065 100644 --- a/function.h +++ b/function.h @@ -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: */ diff --git a/oneplayer.c b/oneplayer.c index 1e4f752..f29df9a 100644 --- a/oneplayer.c +++ b/oneplayer.c @@ -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); diff --git a/twoplayers.c b/twoplayers.c index 452e75b..59b5206 100644 --- a/twoplayers.c +++ b/twoplayers.c @@ -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, ¤t_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, ¤t_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, ¤t_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, ¤t_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; -- 2.30.2