javastringarraylistgridworld

Java Tic Tac Toe Error


As per specifictaions, I must make a functioning Tic Tac Toe game using GridWorld. I have completed most of the class, but there is a method that I am stuck on. I'm not sure how to check if there is a winner diagonally or vertically. Below is the method.

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;
}

Any and all help will be much appreciated.


Solution

  • // 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;
        }
    }
    

    Note that you could extract the code for comparing the three cells (row0,1,2,col0,1,2,d0,1,2) into a separate method like this:

    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;
    }