I am currently coding a basic tic tac toe game in c++ for my university assessment and keep receiving an error

-3

In the function setBoard(), I am trying to set a position on the grid to be equal to a X or a O depending on the player but keep receiving this error while debugging the program.

Unhandled exception at 0x008439FF in TicTacToe.exe: 0xC0000005: Access violation reading location 0x33B80330. the error is in the function setBoard() where it reads:

if (g_acBoard[iX][iY] == ' ')

full code below

#include <iostream>
using namespace std;

const int g_kiSize = 3;
char g_acBoard[g_kiSize][g_kiSize] = { {' ',' ',' '},{' ',' ',' '},{' ',' ',' '} };
void resetBoard();
bool setBoard(int iX, int iY, char cValue);
void displayBoard();
void togglePlayer(char& cPlayer);
bool checkWinner(int iX, int iY);
void getRowColumn(int& iX, int& iY);

void getRowColumn(int& iX, int& iY)
{
    int iColumn;
    int iRow;
    bool loopExit = false;

    while (loopExit = false)
    {
        cout << "+----+----+----+" << endl << "+ 00 + 01 + 02 +" << endl << "+----+----+----+" << endl << "+ 10 + 11 + 12 +" << endl << "+----+----+----+" << endl << "+ 20 + 21 + 22 +" << endl << "+----+----+----+" << endl;
        cout << "Please enter the coordinate numbers (first number will be your x coordinate, second will be your y coordinate): " << endl;
        cin >> iRow;
        while (iRow > 2 || iRow < 0)
        {
            cout << "Invalid Entry, you must enter a value between 0 and 2." << endl;
            cout << "Please try again: " << endl;
            cin >> iRow;
        }

        cout << endl;

        cin >> iColumn;
        while (iColumn > 2 || iColumn < 0)
        {
            cout << "Invalid Entry, you must enter a value between 0 and 2." << endl;
            cout << "Please try again: " << endl;
            cin >> iColumn;
        }
        cout << endl;
        iX = iRow;
        iY = iColumn;
        loopExit = true;
    }
}






void togglePlayer(char& cPlayer)
{
    if (cPlayer = 'X')
    {
        cPlayer = 'O';
    }
    else
    {
        cPlayer = 'X';
    }
}


void resetBoard()
{
    for (int i = 0; i < g_kiSize; i++)
    {
        for (int j = 0; j < g_kiSize; j++)
        {
            g_acBoard[i][j] = ' ';
        }
    }
}
bool setBoard(int iX, int iY, char cValue)
{
    if (g_acBoard[iX][iY] == ' ')
    {
        g_acBoard[iX][iY] = cValue;
        return true;
    }
    else
    {
        return false;
    }
}

void displayBoard()
{
    cout << "+---+---+---+" << endl;
    for (int i = 0; i < g_kiSize; i++)
    {
        for (int j = 0; j < g_kiSize; j++)
        {
            cout << "| " << g_acBoard[i][j] << " ";
        }
        cout << "| " << endl;
        cout << "+---+---+---+" << endl;
    }
}

bool checkWinner(int iX, int iY)
{
    if ((g_acBoard[0][0] == 'X' && g_acBoard[0][1] == 'X' && g_acBoard[0][2] == 'X') || (g_acBoard[0][0] == 'O' && g_acBoard[0][1] == 'O' && g_acBoard[0][2] == 'O'))
    {
        return true;
    }

    else if ((g_acBoard[1][0] == 'X' && g_acBoard[1][1] == 'X' && g_acBoard[1][2] == 'X') || (g_acBoard[1][0] == 'O' && g_acBoard[1][1] == 'O' && g_acBoard[1][2] == 'O'))
    {
        return true;
    }

    else if ((g_acBoard[2][0] == 'X' && g_acBoard[2][1] == 'X' && g_acBoard[2][2] == 'X') || (g_acBoard[2][0] == 'O' && g_acBoard[2][1] == 'O' && g_acBoard[2][2] == 'O'))
    {
        return true;
    }

    else if ((g_acBoard[0][0] == 'X' && g_acBoard[1][0] == 'X' && g_acBoard[2][0] == 'X') || (g_acBoard[0][0] == 'O' && g_acBoard[1][0] == 'O' && g_acBoard[2][0] == 'O'))
    {
        return true;
    }

    else if ((g_acBoard[0][1] == 'X' && g_acBoard[1][1] == 'X' && g_acBoard[2][1] == 'X') || (g_acBoard[0][1] == 'O' && g_acBoard[1][1] == 'O' && g_acBoard[2][1] == 'O'))
    {
        return true;
    }

    else if ((g_acBoard[0][2] == 'X' && g_acBoard[1][2] == 'X' && g_acBoard[2][2] == 'X') || (g_acBoard[0][2] == 'O' && g_acBoard[1][2] == 'O' && g_acBoard[2][2] == 'O'))
    {
        return true;
    }

    else if ((g_acBoard[0][0] == 'X' && g_acBoard[1][1] == 'X' && g_acBoard[2][2] == 'X') || (g_acBoard[0][0] == 'O' && g_acBoard[1][1] == 'O' && g_acBoard[2][2] == 'O'))
    {
        return true;
    }

    else if ((g_acBoard[0][2] == 'X' && g_acBoard[1][1] == 'X' && g_acBoard[2][0] == 'X') || (g_acBoard[0][2] == 'O' && g_acBoard[1][1] == 'O' && g_acBoard[2][0] == 'O'))
    {
        return true;
    }
    else
    {
        return false;
    }
}



int main()
{
    int iColumn;
    int iRow;
    char cPlayer = 'X';
    bool close = false;
    do
    {
        displayBoard();
        getRowColumn(iRow, iColumn);
        while (!setBoard(iRow, iColumn, cPlayer))
        {
            cout << "Sorry, that place is taken by the other player, Please try again." << endl;
            getRowColumn(iRow, iColumn);
        }
        checkWinner(iRow, iColumn);

        if (checkWinner(iRow, iColumn) == false)
        {
            togglePlayer(cPlayer);
        }
        else if (checkWinner(iRow, iColumn) == true)
        {
            cout << "The winner is " << cPlayer << "!" << endl;
            close = true;

        }

    } while (close = false);
    system("pause");
}
c++
asked on Stack Overflow Dec 11, 2018 by Georgia Proffitt • edited Dec 11, 2018 by πάντα ῥεῖ

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0