Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9088291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:52:53+00:00 2026-06-16T21:52:53+00:00

debug : Run-Time Check Failure #2 – Stack around the variable ‘board’ was corrupted.

  • 0

debug :

Run-Time Check Failure #2 – Stack around the variable ‘board’ was corrupted.
The program ‘[5516] a12aa.exe: Native’ has exited with code 0 (0x0).

edit:
Oh I didn’t mention that happens only in gamemode 1 (playing aginst the computer) in game mode 2 I have the same assgiment and its working fine..

#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];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:52:54+00:00Added an answer on June 16, 2026 at 9:52 pm

    There are a few places where you assign to board[len][len], for example:

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

    This is out of bounds and is therefore undefined (in practice corrupting the stack).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I receive this Run-Time Check Failure upon the return in the following code. I
I'm trying to debug an objective C program. It used to run, and I'm
For some reasons I can't debug my C# program in Visual Studio! -> run
Short problem description: XCode 4.2 install right Target on device, but debug (run) always
I am using Eclipse GALILEO running on Linux Ubuntu with EPIC plugin to Run/Debug
i have succesfully installed OpenCv & Qt in Ubuntu..i can debug and run some
System.err.println() in Eclipse doesn't print in red. I checked preferences > Run/Debug > console
The Run > Debug History menu item in Eclipse (Helios) contains a MRU list
I have a valid developer profile. I could run and debug my app in
I have run into a very interesting problem trying to debug my custom PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.