gpt4 book ai didi

java - 连接四个垂直赢家

转载 作者:行者123 更新时间:2023-11-30 11:02:34 25 4
gpt4 key购买 nike

由于某些原因,当内部四个循环运行完毕时,此方法不会将 1 加到外部 for 循环中的 col。我只能在 col 设置的列中获得垂直连接四肢。例如,如果 col 等于 2,计算机将只能识别第 2 列中的垂直连接四。有什么问题吗?

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

最佳答案

我认为您的代码允许 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;
}

关于java - 连接四个垂直赢家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30720468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com