首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接四个立式冠军

连接四个立式冠军
EN

Stack Overflow用户
提问于 2015-06-08 23:02:38
回答 1查看 120关注 0票数 2

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

代码语言:javascript
复制
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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-09 01:12:09

我认为您的代码允许4个垂直空序列。需要为每个if语句添加currP != null

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30720468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档