From ddf4fc8c404ebf66420beded2db6ad93688e94f9 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Wed, 24 Jul 2024 14:30:30 +0200 Subject: [PATCH] first functional version --- function.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ function.h | 4 ++++ tetris.c | 43 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/function.c b/function.c index 01de6c6..1eb7098 100644 --- a/function.c +++ b/function.c @@ -365,4 +365,54 @@ void settleblock (board_t *board, block_t *block, int x, int y) } } +int checkline (board_t *board, int *score, int *lines) +{ + int nb = 0; + int i, j; + for (j = 0; j < board->height; j++) { + int isfull = 1; + for (i = 0; i < board->width; i++) { + if (*getcell (board, i, j) == ' ') { + isfull = 0; + break; + } + } + if (isfull) { + nb++; + for (i = 0; i < board->width; i++) { + *getcell (board, i, j) = ' '; + } + } + } + + *score += (nb == 4) ? 129 : (nb == 3) ? 64 : (nb == 2) ? 27 : (nb == 1) ? 10 : 0; + *lines += nb; + + return nb; +} + +void stackboard (board_t *board) +{ + int i, j; + + for (j = board->height - 1; j >= 0; j--) { + int isempty = 1; + for (i = 0; i < board->width; i++) { + if (*getcell (board, i, j) != ' ') { + isempty = 0; + break; + } + } + if (isempty) { + while (j > 0) { + for (i = 0; i < board->width; i++) { + *getcell (board, i, j) = *getcell (board, i, j - 1); + } + j--; + } + break; + } + } +} + /* vim: set ts=4 sw=4 et: */ diff --git a/function.h b/function.h index 9bf01a0..072049e 100644 --- a/function.h +++ b/function.h @@ -57,6 +57,10 @@ int testvalidpos (board_t *board, block_t *block, int x, int y); void settleblock (board_t *board, block_t *block, int x, int y); +int checkline (board_t *board, int *score, int *lines); + +void stackboard (board_t *board); + #endif /* __FUNCTION_H__ */ /* vim: set ts=4 sw=4 et: */ diff --git a/tetris.c b/tetris.c index 2ed1429..e6293a2 100644 --- a/tetris.c +++ b/tetris.c @@ -191,8 +191,8 @@ int main (int argc, char *argv[]) int lmsg = xhelp - xmsg + strmaxlen (help, '\n'); /* event loop */ - int level = 1; - int mode = 0; + int level = 0; + int mode = -1; int score = 0; int settle = 0; int speed = 5000; @@ -202,10 +202,14 @@ int main (int argc, char *argv[]) char msg[128] = {0}; switch (mode) { + case -1: + nextwindow (blocks, nb_blocks, xnext, ynext, current); + sprintf (msg, "Get ready player One. Press to start"); + break; case 0: halfdelay (0); nextwindow (blocks, nb_blocks, xnext, ynext, current); - sprintf (msg, "Get ready player One"); + sprintf (msg, "Game in pause. Pause

to continue"); break; case 1: halfdelay (1); @@ -230,26 +234,42 @@ int main (int argc, char *argv[]) if (!testvalidpos (board, cblock, xblock, yblock)) { halfdelay (0); mode = 2; - sprintf (msg, "End of game"); + sprintf (msg, "To bad, you loose. Score is %d", score); } nextwindow (blocks, nb_blocks, xnext, ynext, next); break; case 2: freeblock (cblock); cblock = NULL; - sprintf (msg, "Press q to quit"); + sprintf (msg, "End of game. Press to quit"); } msgwindow (msg, xmsg, ymsg, lmsg); + int nblines = checkline (board, &score, &level); + while (nblines-- > 0) { + boardwindow (board); + stackboard (board); + } boardwindow (board); - scorewindow (xscore, yscore, nbdigit, score, level); + scorewindow (xscore, yscore, nbdigit, score, (level / 10) + 1); + int newspeed = 10 + 4900 / (level / 10 + 1); + if (newspeed != speed) { + speed = newspeed; + setendtime (&tend, speed); + } if (mode == 1) { displayblock (board, cblock, xblock, yblock); } int ch = getch (); switch (mode) { - case 0: + case -1: + switch (ch) { + case 's': + mode = 0; + ch = 'p'; + break; + } break; case 1: switch (ch) { @@ -306,9 +326,14 @@ int main (int argc, char *argv[]) } switch (ch) { case 'p': - mode = (mode == 0) ? 1 : 0; - if (mode == 1) { + switch (mode) { + case 0: + mode = 1; setendtime (&tend, speed); + break; + case 1: + mode = 0; + break; } break; case KEY_ESC: -- 2.30.2