由于某些原因,当内部四个循环运行时,此方法不会在外部For循环中添加1到col。我只能得到垂直连接4在列中,col被设置为。例如,如果col等于2,计算机只会识别列2中的垂直连接4。有什么问题吗?
public Player colWinner(){
for(int col = 0; col < grid[0].length; col++){
for(int row = 0; row < grid.length/2; row++){
Player currP = getCell(row,col);
if((currP == getCell(row + 1, col)) && (currP == getCell(row + 2, col)) && (currP == getCell(row + 3, col))){
return currP;
}
else{
continue;
}
}
for(int row = grid.length/2; row < grid.length; row++){
Player currP = getCell(row,col);
if((currP == getCell(row - 1, col)) && (currP == getCell(row - 2, col)) && (currP == getCell(row - 3, col))){
return currP;
}
else{
continue;
}
}
}
return null;
}发布于 2015-06-09 01:12:09
我认为您的代码允许4个垂直空序列。需要为每个if语句添加currP != null。
public Player colWinner(){
for(int col = 0; col < grid[0].length; col++){
for(int row = 0; row < grid.length/2; row++){
Player currP = getCell(row,col);
if(currP != null && currP == getCell(row + 1, col) && currP == getCell(row + 2, col) && currP == getCell(row + 3, col)){
return currP;
}
}
for(int row = grid.length/2; row < grid.length; row++){
Player currP = getCell(row,col);
if(currP != null && currP == getCell(row - 1, col) && currP == getCell(row - 2, col) && currP == getCell(row - 3, col)){
return currP;
}
}
}
return null;
}https://stackoverflow.com/questions/30720468
复制相似问题