Java Tic Tac Toe错误

Java Tic Tac Toe错误,java,string,arraylist,gridworld,Java,String,Arraylist,Gridworld,根据说明,我必须使用GridWorld制作一个功能正常的井字游戏。我已经完成了大部分课程,但有一种方法我一直坚持。我不知道如何检查是否有一个赢家对角线或垂直。下面是方法 public String getWinner() { Grid<Piece> grid = getGrid(); if(grid == null) { return "no winner"; } String winner = ""; for (in

根据说明,我必须使用GridWorld制作一个功能正常的井字游戏。我已经完成了大部分课程,但有一种方法我一直坚持。我不知道如何检查是否有一个赢家对角线或垂直。下面是方法

public String getWinner()
{
    Grid<Piece> grid = getGrid();
    if(grid == null)
    {
        return "no winner";
    }

    String winner = "";
    for (int r = 0; r<grid.getNumRows(); r++)
    {
        Piece row0 = grid.get(new Location(r,0));
        Piece row1 = grid.get(new Location(r,1));
        Piece row2 = grid.get(new Location(r,2));

        if(row0 == null || row1 == null || row2 == null)
        {
            continue;
        }

        if(row0.getName().equals(row1.getName()) && row0.getName().equals(row2.getName()))
        {
            winner = row0.getName()+ " wins horizontally!";
            break;
        }
    }

    //check for vertical winner

    //check for diagonal winner


    if(isWorldFull() && winner.length() == 0)
    {
        winner =  "cat's game - no winner!\n\n";
    }
    else if(!isWorldFull() && winner.length() == 0)
    {
        winner = "no winner";
    }

    return winner;
}

我们将非常感谢您的任何帮助

啊哈!我现在明白了。谢谢你这么快就帮了我。在这种情况下,我需要同时检查col和row的对角线。@ChriskOlson如果您发现我的答案正确,请接受:
// vertically
for (int c = 0; r<grid.getNumCols(); c++)
{
    Piece col0 = grid.get(new Location(0,c));
    Piece col1 = grid.get(new Location(1,c));
    Piece col2 = grid.get(new Location(2,c));

    if(col0 == null || col1 == null || col2 == null)
    {
        continue;
    }

    if(col0.getName().equals(col1.getName()) && col0.getName().equals(col2.getName()))
    {
        winner = col0.getName()+ " wins vertically!";
        break;
    }
}

// diagonal 1
for (int x = 0; r<grid.getNumRows(); x++)
{
    Piece d0 = grid.get(new Location(x,x));
    Piece d1 = grid.get(new Location(x,x));
    Piece d2 = grid.get(new Location(x,x));

    if(d0 == null || d1 == null || d2 == null)
    {
        continue;
    }

    if(d0.getName().equals(d1.getName()) && d0.getName().equals(d2.getName()))
    {
        winner = d0.getName()+ " wins diagonally!";
        break;
    }
}

// diagonal 2
for (int x = 0; r<grid.getNumRows(); x++)
{
    Piece d0 = grid.get(new Location(x,2-x));
    Piece d1 = grid.get(new Location(x,2-x));
    Piece d2 = grid.get(new Location(x,2-x));

    if(d0 == null || d1 == null || d2 == null)
    {
        continue;
    }

    if(d0.getName().equals(d1.getName()) && d0.getName().equals(d2.getName()))
    {
        winner = d0.getName()+ " wins diagonally!";
        break;
    }
}
boolean check3Pieces(Piece p0, Piece p1, Piece p2, String caption) {
    if(p0 == null || p1 == null || p2 == null)
    {
        return false;
    }

    if(p0.getName().equals(p1.getName()) && p0.getName().equals(p2.getName()))
    {
        winner = p0.getName()+ " " + caption;
        return true;
    }
    return false;
}