欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)連連看算法

 更新時(shí)間:2021年06月06日 10:36:42   作者:趨向_quxiang  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)連連看算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

連連看是個(gè)經(jīng)典的小游戲,規(guī)則是:兩圖案相同的方塊在2折以內(nèi)的線連接下可以消除。里面的算法還是非常有趣,今天來(lái)研究一下。

初始化棋盤(pán)

假設(shè)有一個(gè)8*8的棋盤(pán),我們要將其擴(kuò)充至10*10,為什么?因?yàn)檫@樣外圍的連接就可以不用越界了。

消除基本條件

判斷是否具備消除的基本條件有 3 個(gè)

  • 兩個(gè)方塊不能是同一個(gè)坐標(biāo)
  • 兩個(gè)方塊必須是同種類型(圖案)
  • 兩個(gè)方塊中不能有任何一個(gè)已經(jīng)消除過(guò)的(消除過(guò)后的值用 mark 表示)
// 判斷是否具備消除的基本條件:兩個(gè)方塊不能是同一個(gè)坐標(biāo);兩個(gè)方塊必須是同種類型;兩個(gè)方塊中不能有任何一個(gè)已經(jīng)消除過(guò)的
public static boolean basicCondition(Point a, Point b) {
    return !a.equals(b) && board[a.x][a.y] == board[b.x][b.y] && !isNull(a) && !isNull(b);
}

// 判斷格子是否為空或已經(jīng)被消除
public static boolean isNull(Point c) {
    return board[c.x][c.y] == 0 || board[c.x][c.y] == mark;
}

0折消除

能0折消除,說(shuō)明兩個(gè)方塊一定在同一直線上;它們可能是同一水平直線,也可能是同一垂直直線

如果兩個(gè)方塊的相對(duì)位置滿足其中之一,并且我們?cè)偃ヅ袛噙B線經(jīng)過(guò)的方塊是否為空就行了。

// 判斷同一直線能否相連
public static boolean matchLine(Point a, Point b) {
    // 水平
    if (a.x == b.x) {
        int minY = Math.min(a.y, b.y), maxY = Math.max(a.y, b.y);
        for (int i = minY + 1; i < maxY; i++) {
            if (!isNull(new Point(a.x, i))) return false;
        }
        return true;
    }
    // 垂直
    else if (a.y == b.y) {
        int minX = Math.min(a.x, b.x), maxX = Math.max(a.x, b.x);
        for (int i = minX + 1; i < maxX; i++) {
            if (!isNull(new Point(i, a.y))) return false;
        }
        return true;
    }
    // 不在水平或垂直上
    return false;
}

1折消除

1折消除也就2種情況,就是上折和下折,這樣可以知道折點(diǎn)是(a.x, b.y)(b.x, a.y) ;即判斷a點(diǎn)到折點(diǎn)能否0折消除,且b點(diǎn)到折點(diǎn)能否0折消除,且折點(diǎn)處為空

// 判斷 1 折能否相連:拐角點(diǎn) c1 和 c2 與 a b 點(diǎn)能相連并且拐角點(diǎn)為空
public static boolean matchOneTurn(Point a, Point b) {
    Point c1 = new Point(a.x, b.y);
    Point c2 = new Point(b.x, a.y);
    return matchLine(a, c1) && matchLine(b, c1) && isNull(c1)
            || matchLine(a, c2) && matchLine(b, c2) && isNull(c2);
}

2折消除

2折消除的邏輯稍微麻煩了一點(diǎn)點(diǎn),即掃描 a 點(diǎn)所在的行和列,找一點(diǎn) c ,使得 a 與 c 能夠0折消除且 b 與 c 能1折消除;掃描 b 點(diǎn)所在的行和列,找一點(diǎn) c ,使得 b 與 c 能夠0折消除且 a 與 c 能1折消除,當(dāng)然,c 點(diǎn)不能與 a b 點(diǎn)重合,也必須為空。

// 判斷 2 折能否相連:掃描 a 所在的行和列,找一點(diǎn) c 使之與 a 直線匹配,與 b 1 折匹配;掃描 b 所在的行和列,找一點(diǎn) c 使之與 b 直線匹配,與 a 1 折匹配
public static boolean matchTwoTurn(Point a, Point b) {
    // 掃描 a b 所在的行
    for (int i = 0; i < c; i++) {
        Point c1 = new Point(a.x, i);
        Point c2 = new Point(b.x, i);
        if (i != a.y && matchLine(c1, a) && matchOneTurn(c1, b) && isNull(c1)
                || i != b.y && matchLine(c2, b) && matchOneTurn(c2, a) && isNull(c2))
            return true;
    }
    // 掃描 a b 所在的列
    for (int i = 0; i < r; i++) {
        Point c1 = new Point(i, a.y);
        Point c2 = new Point(i, b.y);
        if (i != a.x && matchLine(c1, a) && matchOneTurn(c1, b) && isNull(c1)
                || i != b.x && matchLine(c2, b) && matchOneTurn(c2, a) && isNull(c2))
            return true;
    }
    // 不存在這樣的 c 點(diǎn)
    return false;
}

將上述所有判斷整合,就完成了一對(duì)方塊完整的消除判斷

// 整合判斷
public static boolean match(Point a, Point b) {
    return basicCondition(a, b) && (matchLine(a, b) || matchOneTurn(a, b) || matchTwoTurn(a, b));
}

關(guān)鍵算法解決了,相信寫(xiě)一個(gè)連連看游戲的障礙被打破了,是不是躍躍欲試了呢?

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論