根据规范,我必须使用 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;
}
任何和所有的帮助将不胜感激。