首页 > 编程技术 > java

Java实现连连看算法

发布时间:2021-6-6 15:00

连连看是个经典的小游戏,规则是:两图案相同的方块在2折以内的线连接下可以消除。里面的算法还是非常有趣,今天来研究一下。

初始化棋盘

假设有一个8*8的棋盘,我们要将其扩充至10*10,为什么?因为这样外围的连接就可以不用越界了。

消除基本条件

判断是否具备消除的基本条件有 3 个

// 判断是否具备消除的基本条件:两个方块不能是同一个坐标;两个方块必须是同种类型;两个方块中不能有任何一个已经消除过的
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);
}

// 判断格子是否为空或已经被消除
public static boolean isNull(Point c) {
    return board[c.x][c.y] == 0 || board[c.x][c.y] == mark;
}

0折消除

能0折消除,说明两个方块一定在同一直线上;它们可能是同一水平直线,也可能是同一垂直直线

如果两个方块的相对位置满足其中之一,并且我们再去判断连线经过的方块是否为空就行了。

// 判断同一直线能否相连
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种情况,就是上折和下折,这样可以知道折点是(a.x, b.y)(b.x, a.y) ;即判断a点到折点能否0折消除,且b点到折点能否0折消除,且折点处为空

// 判断 1 折能否相连:拐角点 c1 和 c2 与 a b 点能相连并且拐角点为空
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折消除的逻辑稍微麻烦了一点点,即扫描 a 点所在的行和列,找一点 c ,使得 a 与 c 能够0折消除且 b 与 c 能1折消除;扫描 b 点所在的行和列,找一点 c ,使得 b 与 c 能够0折消除且 a 与 c 能1折消除,当然,c 点不能与 a b 点重合,也必须为空。

// 判断 2 折能否相连:扫描 a 所在的行和列,找一点 c 使之与 a 直线匹配,与 b 1 折匹配;扫描 b 所在的行和列,找一点 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 点
    return false;
}

将上述所有判断整合,就完成了一对方块完整的消除判断

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

关键算法解决了,相信写一个连连看游戏的障碍被打破了,是不是跃跃欲试了呢?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

标签:[!--infotagslink--]

您可能感兴趣的文章: