在Java中检测矩阵中的对角线(四子棋游戏的胜利)

问题描述 投票:0回答:1

我正在制作 Connect 4 游戏。目标是以相同的方式对齐四个项目。

我有一个双数组:

Player[6][7]
,由在该插槽上玩的玩家填充。

我正在研究胜利条件,有:

  • 线(工作
  • 列(工作
  • 从左上到右下的对角线 -> 不起作用
  • 从左下角到右上角的对角线 -> 不起作用

对角线不起作用。

这是我检查胜利的代码:

private final Player[][] board = new Player[6][7]; // the board filled

public Player hasWinner() {
    // Lines (rows)
    for(int y = 0; y < board.length; y++) { // simple lines
        Player last = null;
        int nb = 0;
        Player[] line = board[y];
        for(int x = 0; x < line.length; x++) {
            Player played = line[x];
            if((last == null || played == last) && played != null) { // new player or same as before
                nb++;
                if(nb == 4) // it's this !
                    return played;
            } else { // else reset
                nb = (played == null ? 0 : 1);
            }
            last = played;
        }
    }

    // Columns
    for(int x = 0; x < board[0].length; x++) { // simple columns
        Player last = null;
        int nb = 0;
        for(int y = 0; y < board.length; y++) { // for each columns
            Player played = board[y][x];
            if((last == null || played == last) && played != null) { // new player or same as before
                nb++;
                if(nb == 4) // it's this !
                    return played;
            } else { // else reset
                nb = (played == null ? 0 : 1);
            }
            last = played;
        }
    }
   
    // ➡️ HERE IS THE INTERESTING PART
    // Diagonals
    for(int i = -board.length; i < board[0].length; i++) { // diagonals
        Player last = null;
        int nb = 0;
        for(int j = 0; j < 9; j++) {
            if(board.length <= j || board[j].length <= j)
                continue;
            Player played = board[j][j];
            if((last == null || played == last) && played != null) { // new player or same as before
                nb++;
                if(nb == 4) // it's this !
                    return played;
            } else { // else reset
                nb = (played == null ? 0 : 1);
            }
            last = played;
        }
        for(int j = 9; j < 0; j--) {
            if(board.length <= j || board[j].length <= j)
                continue;
            Player played = board[j][board[j].length - j];
            if((last == null || played == last) && played != null) { // new player or same as before
                nb++;
                if(nb == 4) // it's this !
                    return played;
            } else { // else reset
                nb = (played == null ? 0 : 1);
            }
            last = played;
        }
    }
    return null;
}

怎样才能用对角线取得胜利?

java multidimensional-array game-development 2d-games
1个回答
0
投票

避免使用像

9
这样的神奇数字。使用矩阵行和列的长度作为上限。指定匹配大小的常量,即 4。

对于对角线,将当前玩家与

[row + dy, col + dx]
方向进行比较。

public class Connect4 {
    private static final int ROWS = 6;
    private static final int COLS = 7;
    private static final int MATCH_SIZE = 4; // Number of pieces needed to win

    private final Player[][] board = new Player[ROWS][COLS]; // the board

    public Player hasWinner() {
        // Check horizontal lines
        for (int y = 0; y < board.length; y++) {
            Player last = null;
            int count = 0;
            for (int x = 0; x < board[y].length; x++) {
                Player current = board[y][x];
                if (current != null && current == last) {
                    count++;
                    if (count == MATCH_SIZE) {
                        return current;
                    }
                } else {
                    count = 1;
                    last = current;
                }
            }
        }

        // Check vertical lines
        for (int x = 0; x < board[0].length; x++) {
            Player last = null;
            int count = 0;
            for (int y = 0; y < board.length; y++) {
                Player current = board[y][x];
                if (current != null && current == last) {
                    count++;
                    if (count == MATCH_SIZE) {
                        return current;
                    }
                } else {
                    count = 1;
                    last = current;
                }
            }
        }

        // Check diagonal lines (top-left to bottom-right)
        for (int y = 0; y <= board.length - MATCH_SIZE; y++) {
            for (int x = 0; x <= board[y].length - MATCH_SIZE; x++) {
                Player current = board[y][x];
                if (current != null &&
                    current == board[y + 1][x + 1] &&
                    current == board[y + 2][x + 2] &&
                    current == board[y + 3][x + 3]) {
                    return current;
                }
            }
        }

        // Check diagonal lines (bottom-left to top-right)
        for (int y = MATCH_SIZE - 1; y < board.length; y++) {
            for (int x = 0; x <= board[y].length - MATCH_SIZE; x++) {
                Player current = board[y][x];
                if (current != null &&
                    current == board[y - 1][x + 1] &&
                    current == board[y - 2][x + 2] &&
                    current == board[y - 3][x + 3]) {
                    return current;
                }
            }
        }

        return null; // No winner found
    }
}

重构逻辑

有很多重复,我们可以使用方向来扫描行、列等...

public class Connect4 {
    private static final int ROWS = 6;
    private static final int COLS = 7;
    private static final int MATCH_SIZE = 4; // Number of pieces needed to win

    private final Player[][] board = new Player[ROWS][COLS]; // the board

    public Player hasWinner() {
        for (int y = 0; y < ROWS; y++) {
            for (int x = 0; x < COLS; x++) {
                Player current = board[y][x];
                if (current == null) {
                    continue;
                }

                // Check right (horizontal)
                if (x <= COLS - MATCH_SIZE && checkDirection(y, x, 0, 1)) {
                    return current;
                }

                // Check down (vertical)
                if (y <= ROWS - MATCH_SIZE && checkDirection(y, x, 1, 0)) {
                    return current;
                }

                // Check down-right (diagonal)
                if (x <= COLS - MATCH_SIZE && y <= ROWS - MATCH_SIZE && checkDirection(y, x, 1, 1)) {
                    return current;
                }

                // Check down-left (anti-diagonal)
                if (x >= MATCH_SIZE - 1 && y <= ROWS - MATCH_SIZE && checkDirection(y, x, 1, -1)) {
                    return current;
                }
            }
        }

        return null; // No winner found
    }

    private boolean checkDirection(int y, int x, int dy, int dx) {
        Player player = board[y][x];
        for (int i = 1; i < MATCH_SIZE; i++) {
            if (board[y + i * dy][x + i * dx] != player) {
                return false;
            }
        }
        return true;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.