Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++&引用;运行时检查失败#2-围绕变量';董事会';已损坏”;_C++_Stack - Fatal编程技术网

C++ c++&引用;运行时检查失败#2-围绕变量';董事会';已损坏”;

C++ c++&引用;运行时检查失败#2-围绕变量';董事会';已损坏”;,c++,stack,C++,Stack,调试: 运行时检查失败#2-变量“board”周围的堆栈已损坏。 程序“[5516]a12aa.exe:Native”已退出,代码为0(0x0) 编辑: 哦,我没有提到只有在游戏模式1(对着电脑玩)中才会发生这种情况。在游戏模式2中,我有同样的设备,而且工作正常 #include <iostream> #include <cstring> using namespace std; const int len=3; //functions: void printBoar

调试:

运行时检查失败#2-变量“board”周围的堆栈已损坏。 程序“[5516]a12aa.exe:Native”已退出,代码为0(0x0)

编辑: 哦,我没有提到只有在游戏模式1(对着电脑玩)中才会发生这种情况。在游戏模式2中,我有同样的设备,而且工作正常

#include <iostream>
#include <cstring>

using namespace std;

const int len=3;
//functions:
void printBoard(char board[len][len]);// prints game board

char userTurn (char board[len][len],int player);
char compTurn(char board[len][len]);

bool checkWin(char board[len][len]);// 
bool checkTie(char board[len][len]);// check if board is full 

char compDef(char board[len][len],bool &move);// computer defend move
char compAtt(char board[len][len],bool &move);// computer attck move
char nextMove(char board[len][len],bool &move);// regular move



int main() {

    char board[len][len] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
    int gamemode;
    int winner=0;

    cout<<"Please choose game mode <1 - 1 player, 2 - 2 players>: ";
    cin>>gamemode;


    switch (gamemode){
    case 1: {
        printBoard(board);
        int player=1;
        while (!checkWin(board)&&!checkTie(board)) {
            cout<<"User turn: ";
            board[len][len] = userTurn(board,player);
            printBoard(board);
            winner=1;
            if (!checkWin(board)&&!checkTie(board)) {
                cout<<"Computer turn..."<<endl;
                board[len][len] = compTurn(board);
                printBoard(board);
                winner=2;
            } // if
        } //while
        cout<<"Game Over!!!"<<endl;
        if (checkTie(board)&&!checkWin(board))// board is full and nobody won
            cout<<"Its a Tie!!!"<<endl;
        else if (winner==1) // winner 1 - user ,, winner 2 - computer
            cout<<"You Win!!!"<<endl;
        else 
            cout<<"Computer Wins!!!"<<endl;
            } //case
            break;
    case 2: {
        printBoard(board);
        int player;
        while (!checkWin(board)&&!checkTie(board)) {
            cout<<"User 1 turn: ";
            player=1;
            board[len][len] = userTurn(board,player);
            printBoard(board);
            winner=1;
            if (!checkWin(board)&&!checkTie(board)) {
                cout<<"User 2 turn: ";
                player=2;
                board[len][len] = userTurn(board,player);
                printBoard(board);
                winner=2;
            } // if
        } //while
        cout<<"Game Over!!!"<<endl;
        if (checkTie(board)&&!checkWin(board))// board is full and nobody won
            cout<<"Its a Tie!!!"<<endl;
        else if (winner==1) // winner 1 - user 1,, winner 2 - user 2
            cout<<"User 1 Win!!!"<<endl;
        else 
            cout<<"User 2 Wins!!!"<<endl;           
            } //case
            break;
    default: { // choice is not 1 or 2
        cout<<"Choice unvaild!"<<endl;
             }  //default
             break;
    }

    return 0;
}//main

void printBoard (char board[len][len]) {
    for (int i=0;i<3;i++) {
        cout<<"-------"<<endl;
        cout<<"|"<<board[i][0]<<"|"<<board[i][1]<<"|"<<board[i][2]<<"|"<<endl;
    }
    cout<<"-------"<<endl;
}

char userTurn (char board[len][len],int player) {
    int i,j;
    do {
        cout<<"Please enter valid coordinates for i and j: ";
        cin>>i>>j;
        if (!(i>=0)||!(i<=2)||!(j>=0)||!(j<=2))
            cout<<"Invaild corrdinates!!!"<<endl;
        else if (board[i][j]!=' ')
            cout<<"The cell already has value!!"<<endl;
        else {
            char symbol;
            if (player==1)
                symbol='X';
            else
                symbol='O';
            board[i][j]= symbol;
            return board[len][len];
        }
    } while (board[i][j]!=' '||!(i>=0)||!(i<=2)||!(j>=0)||!(j<=2));// while chosen coordinates is not empty and 0<i<2 and 0<j<2
}

bool checkWin(char board[len][len]){
    for (int i=0;i<3;i++) {
        if (board[i][0]==board[i][1]&&board[i][0]==board[i][2]&&board[i][0]!=' ')// check win rows
            return true;
    }
    for (int i=0;i<3;i++) {
        if (board[0][i]==board[1][i]&&board[0][i]==board[2][i]&&board[0][i]!=' ')// check win cloumns
            return true;
    }
    if (board[0][0]==board[1][1]&&board[0][0]==board[2][2]&&board[0][0]!=' ')// check win diagonals
        return true;
    if (board[0][2]==board[1][1]&&board[0][2]==board[2][0]&&board[0][2]!=' ')
        return true;

    return false;
}//checkWin

bool checkTie(char board[len][len]){// check ifboard is full 
    for (int i=0;i<3;i++) {
        if (board[i][0]==' '||board[i][1]==' '||board[i][2]==' ')
            return false;
    }
    return true;
}


char compTurn(char board[len][len]){// README.txt
    bool move=false;
    if (board[1][1]==' '){
        board[1][1]='O';
        return board[len][len];
    }
    board[len][len]=compAtt(board,move);
    if (!move)
        board[len][len]=compDef(board,move);
    if (!move)
        board[len][len]=nextMove(board,move);

    return board[len][len];
}

char compDef(char board[len][len], bool &move) {
    //rows
    for (int i=0;i<3;i++) {
        if (board[i][0]=='X'&&board[i][1]=='X'&&board[i][2]==' '){
            board[i][2]='O';
            move=true;
            return board[len][len];
        }
    }
    for (int i=0;i<3;i++) {
        if (board[i][0]=='X'&&board[i][1]==' '&&board[i][2]=='X'){
            board[i][1]='O';
            move=true;
            return board[len][len];
        }
    }
    for (int i=0;i<3;i++) {
        if (board[i][0]==' '&&board[i][1]=='X'&&board[i][2]=='X'){
            board[i][0]='O';
            move=true;
            return board[len][len];
        }
    }
    //col
    for (int i=0;i<3;i++) {
        if (board[0][i]=='X'&&board[1][i]=='X'&&board[2][i]==' '){
            board[2][i]='O';
            move=true;
            return board[len][len];
        }
    }
    for (int i=0;i<3;i++) {
        if (board[0][i]=='X'&&board[1][i]==' '&&board[2][i]=='X'){
            board[1][i]='O';
            move=true;
            return board[len][len];
        }
    }
    for (int i=0;i<3;i++) {
        if (board[0][i]==' '&&board[1][i]=='X'&&board[2][i]=='X'){
            board[0][i]='O';
            move=true;
            return board[len][len];
        }
    }
    // diagonals

    if (board[0][0]=='X'&&board[1][1]=='X'&&board[2][2]==' '){
        board[2][2]='O';
        move=true;
        return board[len][len];
    }
    if (board[0][0]=='X'&&board[1][1]==' '&&board[2][2]=='X'){
        board[1][1]='O';
        move=true;
        return board[len][len];
    }
    if (board[0][0]==' '&&board[1][1]=='X'&&board[2][2]=='X'){
        board[0][0]='O';
        move=true;
        return board[len][len];
    }
    // 2
    if (board[0][2]=='X'&&board[1][1]=='X'&&board[2][0]==' '){
        board[2][0]='O';
        move=true;
        return board[len][len];
    }
    if (board[0][2]=='X'&&board[1][1]==' '&&board[2][0]=='X'){
        board[1][1]='O';
        move=true;
        return board[len][len];
    }
    if (board[0][2]==' '&&board[1][1]=='X'&&board[2][0]=='X'){
        board[0][2]='O';
        move=true;
        return board[len][len];
    }
}

char compAtt(char board[len][len],bool &move) {
    for (int i=0;i<3;i++) {
        if (board[i][0]=='O'&&board[i][1]=='O'&&board[i][2]==' '){
            board[i][2]='O';
            move=true;
            return board[len][len];
        }
    }
    for (int i=0;i<3;i++) {
        if (board[i][0]=='O'&&board[i][1]==' '&&board[i][2]=='O'){
            board[i][1]='O';
            move=true;
            return board[len][len];
        }
    }
    for (int i=0;i<3;i++) {
        if (board[i][0]==' '&&board[i][1]=='O'&&board[i][2]=='O'){
            board[i][0]='O';
            move=true;
            return board[len][len];
        }
    }
    //col
    for (int i=0;i<3;i++) {
        if (board[0][i]=='O'&&board[1][i]=='O'&&board[2][i]==' '){
            board[2][i]='O';
            move=true;
            return board[len][len];
        }
    }
    for (int i=0;i<3;i++) {
        if (board[0][i]=='O'&&board[1][i]==' '&&board[2][i]=='O'){
            board[1][i]='O';
            move=true;
            return board[len][len];
        }
    }
    for (int i=0;i<3;i++) {
        if (board[0][i]==' '&&board[1][i]=='O'&&board[2][i]=='O'){
            board[0][i]='O';
            move=true;
            return board[len][len];
        }
    }
    // diagonals

    if (board[0][0]=='O'&&board[1][1]=='O'&&board[2][2]==' '){
        board[2][2]='O';
        move=true;
        return board[len][len];
    }
    if (board[0][0]=='O'&&board[1][1]==' '&&board[2][2]=='O'){
        board[1][1]='O';
        move=true;
        return board[len][len];
    }
    if (board[0][0]==' '&&board[1][1]=='O'&&board[2][2]=='O'){
        board[0][0]='O';
        move=true;
        return board[len][len];
    }
    // 2
    if (board[0][2]=='O'&&board[1][1]=='O'&&board[2][0]==' '){
        board[2][0]='O';
        move=true;
        return board[len][len];
    }
    if (board[0][2]=='O'&&board[1][1]==' '&&board[2][0]=='O'){
        board[1][1]='O';
        move=true;
        return board[len][len];
    }
    if (board[0][2]==' '&&board[1][1]=='O'&&board[2][0]=='O'){
        board[0][2]='O';
        move=true;
        return board[len][len];
    }
}


char nextMove(char board[len][len],bool &move) {
    // coreners
    if (board[0][0]==' '){
        board[0][0]='O';
        move=true;
        return board[len][len];
    }
    if (board[2][0]==' '){
        board[2][0]='O';
        move=true;
        return board[len][len];
    }
    if (board[0][2]==' '){
        board[0][2]='O';
        move=true;
        return board[len][len];
    }
    if (board[2][2]==' '){
        board[2][2]='O';
        move=true;
        return board[len][len];
    }
    // centers
    if (board[0][1]==' '){
        board[0][1]='O';
        move=true;
        return board[len][len];
    }
    if (board[1][0]==' '){
        board[1][0]='O';
        move=true;
        return board[len][len];
    }
    if (board[2][1]==' '){
        board[2][1]='O';
        move=true;
        return board[len][len];
    }
    if (board[1][2]==' '){
        board[1][2]='O';
        move=true;
        return board[len][len];
    }
}
#包括
#包括
使用名称空间std;
常数int len=3;
//职能:
无效印刷板(字符板[len][len]);//打印游戏板
char userTurn(字符板[len][len],int播放器);
char compTurn(char board[len][len]);
bool checkWin(字符板[len][len]);//
bool checkTie(字符板[len][len]);//检查电路板是否已满
char compDef(char board[len][len],bool&move);//电脑防御招式
char compat(char board[len][len],bool&move);//计算机攻击移动
char nextMove(char board[len][len],bool&move);//常规动作
int main(){
字符板[len][len]={{{'','','',},{'','','',},{'','','','','',};
int游戏模式;
int=0;
cout>游戏模式;
开关(游戏模式){
案例1:{
印刷板;
int player=1;
而(!checkWin(board)和&!checkTie(board)){

cout有几个地方可以指定给
板[len][len]
,例如:

                board[len][len] = userTurn(board,player);
这超出了范围,因此未定义(实际上会损坏堆栈)。

您正在执行以下操作:

board[len][len] = userTurn(board,player);

这是写在数组的结尾。< / P>你好,欢迎访问StAccOffFuff.com。请花些时间阅读。我也建议你阅读该网站,它提供了如何编写好的问题代码示例的技巧。最后,作为一般技巧,不要在C++中使用原始数组,而应该使用它。(例如,参见Coddick对原因的回答)这会让你以后的生活更轻松。谢谢你的快速回答。那么我如何从函数中返回字符数组呢?@user1951263问题不在于从函数中返回字符数组,而在于赋值。记住数组的索引是从零到

大小-1
。我如何修复它?哦,我没有提到这种情况,只有我自己n游戏模式1(对着电脑玩)在游戏模式2中,我有相同的设备和它的工作状态。@user1951263:它不是“工作状态良好”,只是没有造成明显的崩溃。分配到
板[len][len]是数组外的界限,是一个必须修复的错误。@ USER1951263:你甚至不必通过指针传递板。C++中的纯数组已经被引用传递到函数中,因此在<代码> UsRoT/<代码>中对<代码>板/代码>的任何修改已经在“代码”>代码>主板< /代码>中反映在<代码>主< /代码>中。