string - Java Tic Tac Toe Error -


as per specifictaions, must make functioning tic tac toe game using gridworld. have completed of class, there method stuck on. i'm not sure how check if there winner diagonally or vertically. below method.

public string getwinner() {     grid<piece> grid = getgrid();     if(grid == null)     {         return "no winner";     }      string winner = "";     (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 vertical winner      //check 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 , appreciated.

// vertically (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 (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 (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 extract code comparing 3 cells (row0,1,2,col0,1,2,d0,1,2) separate method 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; } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -