}
}
+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: */
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;
char msg[128] = {0};
switch (mode) {
+ case -1:
+ nextwindow (blocks, nb_blocks, xnext, ynext, current);
+ sprintf (msg, "Get ready player One. Press <s> 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 <p> to continue");
break;
case 1:
halfdelay (1);
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 <q> 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) {
}
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: