void clearLines()
{
    int x, y;
    bool lineFull;
    for(y=0; y<16; y++) {
        lineFull = true; //assume the line is full
        for(x=0; x<10; x++) {
            int color = grid[x][y];
            if(color == 0) {
                lineFull = false;
                break;
            }
        }

        if(lineFull) {
            deleteLine(y);
            incScore();
        }
    }
}
Exemple #2
0
/**
 * Search same blocks in a row/column/diagonal and remove them.
 */
void collapseMap (void)
{
    uint8_t x, y;
    const uint8_t start_x = 0, start_y = 0;
    const uint8_t end_x = MAP_SIZE_X, end_y = MAP_SIZE_Y;
    uint8_t x1, y1;
    uint8_t x2, y2;
    uint8_t same_cntr = 0;
    uint8_t same_start_x = 0;
    uint8_t same_start_y = 0;
    bool_t collapsed;
    uint8_t round = 0;

    do
    {
        /* VERTICAL, Fuggoleges */
        collapsed = FALSE;
        for (x = start_x; x < end_x; x++)
        {
            same_cntr = 0;
            for (y = start_y; y < end_y; y++)
            {
                if (MAP_IS_NOT_EMPTY(x, y))
                {
                    if ((y < end_y - 1) && (MAP(x, y) == MAP(x, y + 1)))
                    {
                        if (!same_cntr)
                        {
                            same_start_y = y;
                            same_cntr = 2;
                        }
                        else
                        {
                            same_cntr++;
                        }
                    }
                    else
                    {
                        if (same_cntr >= SAME_BLOCK_NUM)
                        {
                            round++;
                            incScore (same_cntr, SAME_BLOCK_VERT_FACTOR * round);
                            for (y2 = same_start_y; y2 <= y; y2++)
                            {
#ifdef DEBUG
                                printf("VERTICAL shift down x:%i y:%i\r\n", x, y2);
#endif
                                MAP_SELECT(x, y2);
                            }
#ifdef DEBUG
                            drawMap();
#endif
                            collapsed = TRUE;
                        }
                        same_cntr = 0;
                    }
                }
                else
                {
                    same_cntr = 0;
                }
            }
        }
        /* HORIZONTAL, Vizszintes */
        for (y = start_y; y < end_y; y++)
        {
            same_cntr = 0;
            for (x = start_x; x < end_x; x++)
            {
                if (MAP_IS_NOT_EMPTY(x, y))
                {
                    if ((x < end_x - 1) && (MAP(x, y) == MAP(x + 1, y)))
                    {
                        if (!same_cntr)
                        {
                            same_start_x = x;
                            same_cntr = 2;
                        }
                        else
                        {
                            same_cntr++;
                        }
                    }
                    else
                    {
                        if (same_cntr >= SAME_BLOCK_NUM)
                        {
                            round++;
                            incScore (same_cntr, SAME_BLOCK_HORIZ_FACTOR * round);
                            for (x2 = same_start_x; x2 <= x; x2++)
                            {
#ifdef DEBUG
                                printf("HORIZONTAL shift down x:%i y:%i\r\n", x2, y);
#endif
                                MAP_SELECT(x2, y);
                            }
#ifdef DEBUG
                            drawMap();
#endif
                            collapsed = TRUE;
                        }
                        same_cntr = 0;
                    }
                }
                else
                {
                    same_cntr = 0;
                }
            }
        }
        /********* DIAGONAL *********/
        for (y = start_y; y < end_y; y++)
        {
            same_cntr = 0;
            /* DIAGONAL RIGHT on Y, Atlos jobbra lejt Y */
            for (x = start_x, y1 = y; x < end_x && y1 < end_y; x++, y1++)
            {
                if (MAP_IS_NOT_EMPTY(x, y1))
                {
                    if ((x < end_x - 1) && (y1 < end_y - 1)
                            && MAP(x, y1) == MAP(x + 1, y1 + 1))
                    {
                        if (!same_cntr)
                        {
                            same_start_x = x;
                            same_start_y = y1;
                            same_cntr = 2;
                        }
                        else
                        {
                            same_cntr++;
                        }
                    }
                    else
                    {
                        if (same_cntr >= SAME_BLOCK_NUM)
                        {
                            round++;
                            incScore (same_cntr, SAME_BLOCK_DIAG_FACTOR * round);
                            for (x2 = same_start_x, y2 = same_start_y; x2 <= x && y2 <= y1; x2++, y2++)
                            {
#ifdef DEBUG
                                printf("DIAGONAL RIGHT Y shift down x:%i y:%i\r\n", x2, y2);
#endif
                                MAP_SELECT(x2, y2);
                            }
#ifdef DEBUG
                            drawMap();
#endif
                            collapsed = TRUE;
                        }
                        same_cntr = 0;
                    }
                }
                else
                {
                    same_cntr = 0;
                }
            }
            /* DIAGONAL LEFT on Y, Atlos balra lejt Y */
            for (x = start_x, y1 = y; x < end_x && y1 > 0; x++, y1--)
            {
                if (MAP_IS_NOT_EMPTY(x, y1))
                {
                    if ((x < end_x - 1) && MAP(x, y1) == MAP(x + 1, y1 - 1))
                    {
                        if (!same_cntr)
                        {
                            same_start_x = x;
                            same_start_y = y1;
                            same_cntr = 2;
                        }
                        else
                        {
                            same_cntr++;
                        }
                    }
                    else
                    {
                        if (same_cntr >= SAME_BLOCK_NUM)
                        {
                            round++;
                            incScore (same_cntr, SAME_BLOCK_DIAG_FACTOR * round);
                            for (x2 = same_start_x, y2 = same_start_y; x2 <= x && y2 >= y1; x2++, y2--)
                            {
#ifdef DEBUG
                                printf("DIAGONAL LEFT Y shift down x:%i y:%i\r\n", x2, y2);
#endif
                                MAP_SELECT(x2, y2);
                            }
#ifdef DEBUG
                            drawMap();
#endif
                            collapsed = TRUE;
                        }
                        same_cntr = 0;
                    }
                }
                else
                {
                    same_cntr = 0;
                }
            }
        }
        for (x = start_x; x < end_x; x++)
        {
            same_cntr = 0;
            /* DIAGONAL RIGHT on X, Atlos jobbra lejt X */
            for (x1 = x, y = start_y; x1 < end_x && y < end_y; x1++, y++)
            {
                if (MAP_IS_NOT_EMPTY(x1, y))
                {
                    if ((x1 < end_x - 1) && (y < end_y - 1)
                            && MAP(x1, y) == MAP(x1 + 1, y + 1))
                    {
                        if (!same_cntr)
                        {
                            same_start_x = x1;
                            same_start_y = y;
                            same_cntr = 2;
                        }
                        else
                        {
                            same_cntr++;
                        }
                    }
                    else
                    {
                        if (same_cntr >= SAME_BLOCK_NUM)
                        {
                            round++;
                            incScore (same_cntr, SAME_BLOCK_DIAG_FACTOR * round);
                            for (x2 = same_start_x, y2 = same_start_y; x2 <= x1 && y2 <= y; x2++, y2++)
                            {
#ifdef DEBUG
                                printf("DIAGONAL RIGHT X shift down x:%i y:%i\r\n", x2, y2);
#endif
                                MAP_SELECT(x2, y2);
                            }
#ifdef DEBUG
                            drawMap();
#endif
                            collapsed = TRUE;
                        }
                        same_cntr = 0;
                    }
                }
                else
                {
                    same_cntr = 0;
                }
            }
            /* DIAGONAL LEFT on X, Atlos balra lejt X */
            for (x1 = x, y = end_y - 1; x1 < end_x && y > 0; x1++, y--)
            {
                if (MAP_IS_NOT_EMPTY(x1, y))
                {
                    if ((x1 < end_x - 1) && MAP(x1, y) == MAP(x1 + 1, y - 1))
                    {
                        if (!same_cntr)
                        {
                            same_start_x = x1;
                            same_start_y = y;
                            same_cntr = 2;
                        }
                        else
                        {
                            same_cntr++;
                        }
                    }
                    else
                    {
                        if (same_cntr >= SAME_BLOCK_NUM)
                        {
                            round++;
                            incScore (same_cntr, SAME_BLOCK_DIAG_FACTOR * round);
                            for (x2 = same_start_x, y2 = same_start_y; x2 <= x1 && y2 >= y; x2++, y2--)
                            {
#ifdef DEBUG
                                printf("DIAGONAL LEFT X shift down x:%i y:%i\r\n", x2, y2);
#endif
                                MAP_SELECT(x2, y2);
                            }
#ifdef DEBUG
                            drawMap();
#endif
                            collapsed = TRUE;
                        }
                        same_cntr = 0;
                    }
                }
                else
                {
                    same_cntr = 0;
                }
            }
        }
        if (collapsed)
        {
            blinkMap (2);
            /* Delete same blocks */
            for (x = start_x; x < end_x; x++)
            {
                for (y = start_y; y < end_y; y++)
                {
                    if (MAP_IS_SELECTED(x, y))
                    {
                        shiftDownColumn (x, y);
                    }
                }
            }
        }
    } while (collapsed);
}