Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用预定义数组和win长度测试2D字符数组中的win-Java_Java - Fatal编程技术网

使用预定义数组和win长度测试2D字符数组中的win-Java

使用预定义数组和win长度测试2D字符数组中的win-Java,java,Java,我试图使一个checkWin方法返回真,如果有一个赢的组合。我使用嵌套for循环。board size和win size在游戏类中预定义,并在初始化2D阵列和检查win时传递给board类。我希望能够使用此检查方法在任何大小的板上赢得任何大小的组合。这样我就可以在多个游戏中重复使用该方法。Tic-Tac-Toe、Connect-Four、Gomoku等 为什么它不能正常工作?我很肯定我的if陈述是错误的。我会在一些测试中获胜,但在其他测试中不会。我从来没有在西北对角线上赢过,只有在东北对角线上赢

我试图使一个checkWin方法返回真,如果有一个赢的组合。我使用嵌套for循环。board size和win size在游戏类中预定义,并在初始化2D阵列和检查win时传递给board类。我希望能够使用此检查方法在任何大小的板上赢得任何大小的组合。这样我就可以在多个游戏中重复使用该方法。Tic-Tac-Toe、Connect-Four、Gomoku等

为什么它不能正常工作?我很肯定我的if陈述是错误的。我会在一些测试中获胜,但在其他测试中不会。我从来没有在西北对角线上赢过,只有在东北对角线上赢过。我可以很容易地测试某些长度,但我很难测试任何可能大小的电路板和任何尺寸的win长度

/**
 * Tests board initialized with size parameter in class constructor.
 * Utilizes nested loops to check all rows, columns, and diagonals for win.
 * 
 * @param WIN_SIZE, The size to check for valid win.
 * @return, Returns true if there is a winner.
 */
public boolean checkWin(int WIN_SIZE)
{
    //Test columns
    //Loop through columns
    for (int col = 0; col < size; col++) 
    {
        //Loop through rows
        for (int row = 0; row < size; row++) 
        {
            //Loop through win length
            for (int counter = 0; counter < WIN_SIZE; counter++)
            {
                try
                {
                if (board[counter][col] == '_' || board[counter][col] != board[counter+1][col])
                {
                    //Break if spot is blank or if next index does not match.
                    break;
                }
                if (counter == WIN_SIZE - 1)
                {
                    //Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination
                    return true;
                }
                }
                catch (ArrayIndexOutOfBoundsException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    //Test rows
    //Loop through rows
    for (int row = 0; row < size; row++) 
    {
        //Loop through columns
        for (int col = 0; col < size; col++) 
        {
            //Loop through win length
            for (int counter = 0; counter < WIN_SIZE; counter++)
            {
                try
                {
                    if (board[row][counter+col] == '_' || board[row][counter+col] != board[row][counter+col+1])
                    {
                        //Break if spot is blank or if next index does not match
                        break;
                    }
                    if (counter == WIN_SIZE - 1)
                    {
                        //Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination
                        return true;
                    }
                }
                catch (ArrayIndexOutOfBoundsException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }


    //Test Diagonals North-West
    //Loop through column
    for (int col = 0; col < size; col++) 
    {
        //Loop through rows
        for (int row = 0; row < size; row++) 
        {
            //Loop through length of a valid win
            for (int counter = 0; counter < WIN_SIZE; counter++)
            {
                try
                {
                    if (board[row+counter][counter] == '_' || board[row+counter][counter] != board[row+counter+1][counter+1])
                    {
                        //Break if spot is blank or if next index does not match
                        break;
                    }
                    else if (counter == WIN_SIZE - 1)
                    {
                        //Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination
                        return true;
                    }
                }
                catch (ArrayIndexOutOfBoundsException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    //Test Diagonals North-West
    //Loop through rows
    for (int col = 0; col < size; col++)
    {
        //Loop through columns
        for (int row = 0; row < size; row++)
        {
            //Loop through length of a valid win
            for (int counter = 0; counter < WIN_SIZE; counter++)
            {
                try
                {
                    if (board[row-counter][counter] == '_' || board[row-counter][counter] != board[row-counter-1][counter+1])
                    {
                        //Break if spot is blank or if next index does not match
                        break;
                    }
                    else if (counter == WIN_SIZE - 1)
                    {
                        //Returns true when for loop has gone through WIN_SIZE-1 iterations indicating a winning combination
                        return true;
                    }
                }
                catch(ArrayIndexOutOfBoundsException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
    //Returns false if no winner is found
    return false;
}
/**
*使用类构造函数中的size参数初始化测试板。
*利用嵌套循环检查所有行、列和对角线是否存在win。
* 
*@param WIN_SIZE,用于检查有效WIN的大小。
*@return,有赢家返回true。
*/
公共布尔checkWin(int WIN_SIZE)
{
//测试柱
//循环通过列
for(int col=0;col
在代码中考虑以下检查:

if (board[counter][col] == '_' || board[counter][col] != board[counter+1][col])

if (board[row+counter][counter] == '_' || board[row+counter][counter] != board[row+counter+1][counter+1])

if (board[row-counter][counter] == '_' || board[row-counter][counter] != board[row-counter-1][counter+1])
当您索引到
时,您使用的是
计数
,或者
计数
。但是,每个检查都应该使用所有三个变量。否则,您只能找到从电路板左侧或顶部边缘开始的组合


看起来您在其中一个循环(测试行的循环)中得到了正确的结果,但在其他三个循环中没有得到正确的结果。

谢谢,我找到了答案:

//Test Rows
board[row][counter+col] == '_' || board[row][counter+col] != board[row][counter+col+1]

//Test Columns
board[counter+row][counter+col] == '_' || board[counter+row][counter+col] !=     board[counter+row+1][counter+col+1]

//Test NW Diagonal
(board[counter+row][counter+col] == '_' || board[counter+row][counter+col] != board[counter+row+1][counter+col+1])

//Test NE Diagonal
board[row-counter][col+counter] == '_' || board[row-counter][col+counter] != board[row-counter-1][col+counter+1]

我把作业放在助教的位置上