From acfe17a1596c612d4c929a2e8424ac5870fbbb42 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 11 Aug 2024 14:50:34 +0200 Subject: [PATCH] code factorisation --- display.c | 18 ++++++++++++++++ display.h | 2 ++ oneplayer.c | 11 ++-------- twoplayers.c | 58 ++++++---------------------------------------------- 4 files changed, 28 insertions(+), 61 deletions(-) diff --git a/display.c b/display.c index 8dbce7a..509ca09 100644 --- 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: */ diff --git a/display.h b/display.h index 6c8dcbf..2b3db4c 100644 --- 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: */ diff --git a/oneplayer.c b/oneplayer.c index f29df9a..29405c6 100644 --- a/oneplayer.c +++ b/oneplayer.c @@ -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, ¤t, &xblock, &yblock, &next); - boardwindow (nextblock, 1); - displayblock (nextblock, blocks + next, -1, -1); - } - settle = 0; + mode = reparenextturn (board, blocks, nb_blocks, &cblock, ¤t, &xblock, &yblock, &next, 0, msg, &score, nextblock); } + settle = 0; break; case 2: diff --git a/twoplayers.c b/twoplayers.c index 59b5206..7a5a503 100644 --- a/twoplayers.c +++ b/twoplayers.c @@ -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, ¤t_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, ¤t_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, ¤t_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, ¤t_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, ¤t_left, &xblock_left, &yblock_left, &next); - boardwindow (nextblock, 1); - displayblock (nextblock, blocks + next, -1, -1); - } + mode = reparenextturn (board_left, blocks, nb_blocks, &cblock_left, ¤t_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, ¤t_right, &xblock_right, &yblock_right, &next); - boardwindow (nextblock, 1); - displayblock (nextblock, blocks + next, -1, -1); - } + mode = reparenextturn (board_right, blocks, nb_blocks, &cblock_right, ¤t_right, &xblock_right, &yblock_right, &next, 2, msg, scores, nextblock); } int isempty = addline (board_right, nbholes); if (!isempty) { -- 2.30.2