c++二维数组算术问题



我一直在使用c++开发一个简单的井字游戏。玩家通过提供所需元素轮流向动态大小的游戏板添加1和2。

用于决定玩家的回合和棋盘更新的功能可以正常工作,但下面的代码检查以0表示的棋盘是否获胜,例如:

  • 3个相邻的1和2
  • 3个垂直排列的1和2
  • 3对角线1和2
但是

不起作用。当测试时,棋盘只是更新每回合。我怀疑问题出在数组运算上。如何实现算法来成功地检测条件?提前谢谢。

bool chkCondition(int rows, int columns, int**board){
int counter=0;
for(int i=0; i<rows; i++){
    for(int j=0; j<columns; j++){
        if(board[i][j]==1 && board[i][j]+1==1 && board[i][j]+2==1){//checks horizontally for 1
            cout << "Player 1 wins!";
            return true;
        }else if(board[i][j]==1 && board[i][j]+columns==1 && board[i][j]+2*columns==1){//checks vertically for 1
            cout << "Player 1 wins!";
            return true;
        }else if(board[i][j]==1 && board[i][j]+columns+1==1 && board[i][j]+2*columns+2==1){//checks diagonally for 1
            cout << "Player 1 wins!";
            return true;
        }else if(board[i][j]==2 && board[i][j]+1==2 && board[i][j]+2==2){//checks horizontally for 2
            cout << "Player 2 wins!";
            return true;
        }else if(board[i][j]==2 && board[i][j]+columns==2 && board[i][j]+2*columns==2){//checks vertically for 2
            cout << "Player 2 wins!";
            return true;
        }else if(board[i][j]==2 && board[i][j]+columns+1==2 && board[i][j]+2*columns+2==2){//checks diagonally for 2 
            cout << "Player 2 wins!";
            return true;
        }else if(board[i][j]==0){//attempts to detect if the game is a draw
            if(counter==(rows+columns)-3){
                cout << "It's a draw!";
                return true;
            }
        }
    }
}
return false;
counter++;
}

编辑算法被更改为正确的格式,以查找元素而不是添加它们的值。新代码在这里:

bool chkCondition(int rows, int columns, int**board){
int counter=0;
for(int i=0; i<rows; i++){
    counter++;
    for(int j=0; j<columns; j++){
        if(board[i][j]==1 && board[i][j+1]==1 && board[i][j+2]==1){
            cout << "Player 1 wins!";
            return true;
        }else if(board[i][j]==2 && board[i][j+1]==2 && board[i][j+2]==2){
            cout << "Player 2 wins!";
            return true;
        }
        if(counter==rows | counter>rows-1){
            //cout << "L--------n";
            //cout << "checking verticaln";
            if(board[i][j]==1 && board[i][j-columns]==1 && board[i][j-columns-columns]==1){
                cout << "Player 1 wins!";
                return true;
            }else if(board[i][j]==2 && board[i][j-columns]==2 && board[i][j-columns-columns]==2){
                cout << "Player 2 wins!";
                return true;
            //cout << "checking diagonaln";
            }else if(board[i][j]==1 && board[i][j-columns-1]==1 && board[i][j-columns-columns-2]==1){
                cout << "Player 1 wins!";
                return true;
            }else if(board[i][j]==2 && board[i][j-columns-1]==2 && board[i][j-columns-columns-2]==2){
                cout << "Player 2 wins!";
                return true;
            }
        }
        if(counter==1 && counter<rows-1){
            //cout << "H--------n";
            //cout << "checking verticaln";
            if(board[i][j]==1 && board[i][j+columns]==1 && board[i][j+columns+columns]==1){
                cout << "Player 1 wins!";
                return true;
            }else if(board[i][j]==2 && board[i][j+columns]==2 && board[i][j+columns+columns]==2){
                cout << "Player 2 wins!";
                return true;
            //cout << "checking diagonaln";
            }else if(board[i][j]==1 && board[i][j+columns+1]==1 && board[i][j+columns+columns+2]==1){
                cout << "Player 1 wins!";
                return true;
            }else if(board[i][j]==2 && board[i][j+columns+1]==2 && board[i][j+columns+columns+2]==2){
                cout << "Player 2 wins!";
                return true;
            }
        }
        if(counter>2 && counter<rows-1){
            //cout << "M--------n";
            //cout << "checking verticaln";
            if(board[i][j]==1 && board[i][j+columns]==1 && board[i][j-columns]==1){
                cout << "Player 1 wins!";
                return true;
            }else if(board[i][j]==2 && board[i][j+columns]==2 && board[i][j-columns]==2){
                cout << "Player 2 wins!";
                return true;
            //cout << "checking diagonaln";
            }else if(board[i][j]==1 && board[i][j+columns+1]==1 && board[i][j-columns-1]==1){
                cout << "Player 1 wins!";
                return true;
            }else if(board[i][j]==2 && board[i][j+columns+1]==2 && board[i][j-columns-1]==2){
                cout << "Player 2 wins!";
                return true;
            }
        }
    }
}
return false;

}

外部if语句用于确定计数迭代中当前行的计数器是在表的开始,中间还是结束,以避免分段错误。这是有效的,程序不会崩溃,然而,所有的垂直和对角条件语句都不能工作,尽管算术是合乎逻辑的。有人知道是怎么回事吗?

正如注释所说,问题在于如何访问表。另一个问题是如何使用循环。如果你先水平检查,那么你的循环会到每一行,但不应该到每一列,否则你会得到一个很好的分割错误。例如第一部分(假设你真的把+1/+2放在了合适的位置

)
for(int i=0; i<rows; i++){
    for(int j=0; j<columns; j++){
        if(board[i][j]==1 && board[i][j+1]==1 && board[i][j+2]==1){//checks horizontally for 1
            cout << "Player 1 wins!";
            return true;

当j是columns-2或columns时,它会给你一个分段错误。要解决这个问题,你需要在你的第二个for之前停下来。水平检查和直线检查是一样的。像这样只做一个循环会更复杂,或者你需要添加更多的if来避免这个错误。

相关内容

  • 没有找到相关文章

最新更新