简体   繁体   English

检查二维数组中的相邻图块

[英]Checking adjacent tiles in 2-dimensional array

So i'm trying to make a game of Connect Four in Java, instead I'm connecting 6 instead of 4.所以我试图用 Java 制作一个连接四的游戏,而不是我连接 6 而不是 4。

I have a 2-dimensional array and X amount of players.我有一个二维数组和 X 数量的玩家。 I have to check if 6 blocks in succession (horizontal, vertical and diagonal) are marked by the same player.我必须检查连续 6 个块(水平、垂直和对角线)是否由同一玩家标记。 If they are, the program should print who won.如果是,程序应该打印谁赢了。

Now, I don't have problems with the checking or determining who won, though for the life of me I can't figure out how to prevent the program from crashing whenever it tries to check for a block that's outside the array.现在,我在检查或确定谁获胜方面没有问题,尽管在我的一生中,我无法弄清楚如何防止程序在尝试检查数组外的块时崩溃。

Now, I'm trying to avoid using try-catch or plopping 8 loops one after the other, and instead use one method for all of the directions with just variation in the parameters but I can't seem to make it work :\\现在,我试图避免使用 try-catch 或一个接一个地执行 8 个循环,而是对所有方向使用一种方法,只是参数有所变化,但我似乎无法使其工作:\\

Does anyone have a suggestion on how this might work?有没有人对这可能如何工作有任何建议?

I'm a beginner in programming and I've possibly missed something so that's why I'm asking for help :)我是编程的初学者,我可能错过了一些东西,所以这就是我寻求帮助的原因:)

Cheers干杯

Edit: here's the code.编辑:这是代码。 It's a bit long, that's why i want to shorten it and make it work somehow.它有点长,这就是为什么我想缩短它并使其以某种方式工作。 the Terminal class is the same as the System.out.println one. Terminal 类与 System.out.println 类相同。

void checkIfPlayerWins(Field field, Integer rowNumber, Integer colNumber) {
    Integer counter = 1;

    for (int i = 1; i <= 6; i++) {
        if (field.isOccupied(rowNumber, colNumber + i)) {
            counter++;
        } else {
            break;
        }
    }

    for (int i = 1; i <= 6; i++) {
        if (field.isOccupied(rowNumber - i, colNumber + i)) {
            counter++;
        } else {
            break;
        }
    }

    for (int i = 1; i <= 6; i++) {
        if (field.isOccupied(rowNumber - i, colNumber)) {
            counter++;
        } else {
            break;
        }
    }

    for (int i = 1; i <= 6; i++) {
        if (field.isOccupied(rowNumber - i, colNumber - i)) {
            counter++;
        } else {
            break;
        }
    }

    for (int i = 1; i <= 6; i++) {
        if (field.isOccupied(rowNumber, colNumber - i)) {
            counter++;
        } else {
            break;
        }
    }

    for (int i = 1; i <= 6; i++) {
        if (field.isOccupied(rowNumber + i, colNumber - i)) {
            counter++;
        } else {
            break;
        }
    }

    for (int i = 1; i <= 6; i++) {
        if (field.isOccupied(rowNumber + i, colNumber)) {
            counter++;
        } else {
            break;
        }
    }

    for (int i = 1; i <= 6; i++) {
        if (field.isOccupied(rowNumber + i, colNumber + i)) {
            counter++;
        } else {
            break;
        }
    }

    if (counter == 6) {
        Terminal.printLine("");
    }
}

here's the isOccupied method这是 isOccupied 方法

boolean isOccupied(Integer x, Integer y) {
    return !this.field[x][y].equals("**");

}

You could simply handle the case where you're attempting to check whether the Field is occupied at invalid coordinates in your isOccupied method:您可以简单地处理您尝试在isOccupied方法中检查Field是否在无效坐标处被占用的情况:

boolean isOccupied(Integer x, Integer y) {
    if(x < 0 || y < 0 || x >= numberOfColumns || y >= numberOfRows) {
        // Attempting to check outside the grid: it's non-occupied.
        return false;
    }

    return !this.field[x][y].equals("**");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM