【问题标题】:Check scalable matrix diagonals for identical string检查相同字符串的可伸缩矩阵对角线
【发布时间】:2015-02-20 14:28:56
【问题描述】:

我正在创建一个可扩展的井字游戏程序,但在尝试检查字符串的对角线时遇到了问题。

我可以使用这种方法检查行:

    public boolean checkRowsForWin(String b){
    //Check all the rows for a winner
    for(int y = 0; y < size; y++){
        for (int x = 0; x < size; x++){
            if (globalGrid[y][x].equals(b)){
                inRow++;
                if (inRow >= neededToWin){
                    return true;
                }
            }else{
                inRow = 0;
            }
        }
        inRow = 0;
    }
    inRow = 0;
    return false;
}

我尝试了 for 循环和 if 语句的组合,我最后的修改如下。如果对角线仅包含右上角,则此方法有效,当我需要它来检查即使对角线不在角落时也是如此。

    public boolean checkDiagForWin(String b, int c, int d){
    for (int x = c, y = d; x < size && y < size; x++, y++){
        if (globalGrid[y][x].equals(b)){
            inRow++;
            if (inRow >= neededToWin){
                return true;
            }
        }
        else{
            inRow = 0;
        }
        inRow = 0;
        for (int x2 = size - 1, y2 = 0; x2 >=0 && y2 < size; x2--, y2++){
            if (globalGrid[y2][x2].equals(b)){
                inRow++;
                if (inRow >= neededToWin){
                    return true;
                }
            }
            else{
                inRow = 0;
            }
        }
        inRow = 0;
    }   
    inRow = 0;
    return false;
}

一行的数量和棋盘的大小是可以变化的,所以不是只检查两个相邻的位置那么简单。

【问题讨论】:

    标签: java matrix


    【解决方案1】:

    您需要从每个可能的位置开始该过程,一种可能的实现可能是:

    在每个位置开始检查过程的函数:

    public boolean checkDiagonals(String b) {
        /* Check the diagonals starting in every position */
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (checkDiagonalForWin(b, i, j) || checkOtherDiagonalForWin(b, i, j)) {
                    return true;
                }
            }
        }
        return false;
    }
    

    有一些功能可以检查从某个位置开始的对角线:

    public boolean checkDiagonalForWin(String b, int row, int col){
    
        for (int inRow = 0; row  < size && col < size; row++, col++) {
            //Check all the rows for a winner
            if (globalGrid[row][col].equals(b)){
                inRow++;
                if (inRow >= neededToWin){
                    return true;
                }
            }else{
                inRow = 0;
            }
        }
        return false;
    }
    
    
    public boolean checkOtherDiagonalForWin(String b, int row, int col){        
        for (int inRow = 0; row  < size && col >= 0; row++, col--) {
            //Check all the rows for a winner
            if (globalGrid[row][col].equals(b)){
                inRow++;
                if (inRow >= neededToWin){
                    return true;
                }
            }else{
                inRow = 0;
            }
        }
        return false;
    }
    

    【讨论】:

    • 这可行,但是它只检查它是否在角落,而对角线可以在板上的任何位置(对于较大的板,例如 8)。
    • @Tanner 我错过了这个要求,我相应地更新了我的答案。
    猜你喜欢
    • 2018-09-25
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    相关资源
    最近更新 更多