1. 程式人生 > >MFC實現連連看三:消子演算法

MFC實現連連看三:消子演算法

兩個位置的圖片能否消除,有三種情況:

1.一條直線連線,這種也是最簡單的一種消除方法

bool LinkInLine(CPoint p1, CPoint p2) 
{
    conner1.x = conner1.y = -1; // 記錄拐點位置
    conner2.x = conner2.y = -1;

    BOOL b = true;
    if (p1.y == p2.y) // 兩個點再同一行
    {
        int min_x = min(p1.x, p2.x);
        int max_x = max(p1.x, p2.x);
        for
(int i = min_x+1; i < max_x; i++) { if (game->map[i][p1.y] != 0) { b = false; } } } else if (p1.x == p2.x) // 在同一列 { int min_y = min(p1.y, p2.y); int max_y = max(p1.y, p2.y); for (int i = min_y + 1
; i < max_y; i++) { if (game->map[p1.x][i] != 0) { b = false; } } } else // 不在同一直線 { b = false; } return b; }

2.兩條直線消除,即經過一個拐點。

兩個頂點經過兩條直線連線有兩種情況,即兩個拐點分兩種情況。

bool OneCornerLink(CPoint p1, CPoint p2) 
{
    conner1.x = conner1.y = -1
; conner2.x = conner2.y = -1; int min_x = min(p1.x, p2.x); int max_x = max(p1.x, p2.x); int min_y = min(p1.y, p2.y); int max_y = max(p1.y, p2.y); // 拐點1 int x1 = p1.x; int y1 = p2.y; //拐點2 int x2 = p2.x; int y2 = p1.y; BOOL b = true; if (game->map[x1][y1] != 0 && game->map[x2][y2] != 0) { b = false; } else { if (game->map[x1][y1] == 0) // 拐點1位置無圖片 { for (int i = min_x + 1; i < max_x; i++) { if (game->map[i][y1] != 0) { b = false; break; } } for (int i = min_y + 1; i < max_y; i++) { if (game->map[x1][i] != 0) { b = false; break; } } if (b) { conner1.x = x1; conner1.y = y1; return b; } } if (game->map[x2][y2] == 0) // 拐點2位置無圖片 { b = true; for (int i = min_x + 1; i < max_x; i++) { if (game->map[i][y2] != 0) { b = false; break; } } for (int i = min_y + 1; i < max_y; i++) { if (game->map[x2][i] != 0) { b = false; break; } } if (b) { conner1.x = x2; conner1.y = y2; return b; } } } return b; }

3.三條直線消除,即經過兩個拐點。
這是可以通過橫向掃描和縱向掃描,掃描的時候可以得到連個拐點,判斷兩個頂點經過這兩個拐點後是否能消除

bool TwoCornerLink(CPoint p1, CPoint p2) 
{
    conner1.x = conner1.y = -1;
    conner2.x = conner2.y = -1;

    int min_x = min(p1.x, p2.x);
    int max_x = max(p1.x, p2.x);
    int min_y = min(p1.y, p2.y);
    int max_y = max(p1.y, p2.y);
    bool b;
    for (int i = 0; i < MAX_Y; i++) // 掃描行
    {
        b = true;
        if (game->map[p1.x][i] == 0 && game->map[p2.x][i] == 0) // 兩個拐點位置無圖片
        {
            for (int j = min_x + 1; j < max_x; j++) // 判斷連個拐點之間是否可以連線
            {
                if (game->map[j][i] != 0)
                {
                    b = false;
                    break;
                }
            }

            if (b)
            {
                int temp_max = max(p1.y, i);
                int temp_min = min(p1.y, i);
                for (int j = temp_min + 1; j < temp_max; j++) // 判斷p1和它所對應的拐點之間是否可以連線
                {
                    if (game->map[p1.x][j] != 0)
                    {
                        b = false;
                        break;
                    }
                }
            }

            if (b)
            {
                int temp_max = max(p2.y, i);
                int temp_min = min(p2.y, i);
                for (int j = temp_min + 1; j < temp_max; j++) // 判斷p2和它所對應的拐點之間是否可以連線
                {
                    for (int j = temp_min + 1; j < temp_max; j++)
                    {
                        if (game->map[p2.x][j] != 0)
                        {
                            b = false;
                            break;
                        }
                    }
                }
            }
            if (b) // 如果存在路線,返回true
            {
                conner1.x = p1.x;
                conner1.y = i;
                conner2.x = p2.x;
                conner2.y = i;
                return b;
            }
        } 

    }// 掃描行結束


    for (int i = 0; i < MAX_X; i++) // 掃描列
    {
        b = true;
        if (game->map[i][p1.y] == 0 && game->map[i][p2.y] == 0) // 連個拐點位置無圖片
        {
            for (int j = min_y + 1; j < max_y; j++) // 兩個拐點之間是否可以連線
            {
                if (game->map[i][j] != 0)
                {
                    b = false;
                    break;
                }
            }

            if (b)
            {
                int temp_max = max(i, p1.x);
                int temp_min = min(i, p1.x);
                for (int j = temp_min + 1; j < temp_max; j++) // 判斷p1和它所對應的拐點之間是否可以連線
                {
                    if (game->map[j][p1.y] != 0)
                    {
                        b = false;
                        break;
                    }
                }
            }

            if (b)
            {
                int temp_max = max(p2.x, i);
                int temp_min = min(p2.x, i);
                for (int j = temp_min + 1; j < temp_max; j++)
                {
                    if (game->map[j][p2.y] != 0)
                    {
                        b = false;
                        break;
                    }
                }
            }
            if (b) // 如果存在路線,返回true
            {
                conner1.y = p1.y;
                conner1.x = i;
                conner2.y = p2.y;
                conner2.x = i;
                return b;
            }
        }

    } // 掃描列結束

    return b;
}