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 6983199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:23:15+00:00 2026-05-27T18:23:15+00:00

I have had a similar issue with quite a few projects I have worked

  • 0

I have had a similar issue with quite a few projects I have worked on involving classes containing arrays. I have a class that is supposed to handle a 2 dimensional matrix representing a TicTacToe game. There is an enumeration in the class for the status of the current game and one member function that has an enumeration return type. I cant seem to figure out why I can create the class set values in the matrix and as soon as I call the member function with the enumerated return type the whole array is reinitialized to 0. I think it has something to do with the constructor being called again or something along those lines but I have not been able to find anything after searching for the past few hours. Any help would be greatly appreciated.

Here is my header file containing the class information:

#ifndef TTT_H
#define TTT_H

#include <cstdlib>
#include <iostream>

using namespace std;

class TicTacToe
{
private:
    enum Status{WinX, WinO, Continue, Draw};
    int **board;

public:
    TicTacToe();
    ~TicTacToe();
    void PrintBoard();
    bool ValidMove(int, int);
    bool PlayerMove(int, int, int);
    Status GameStatus();    //this one causes the problem
    void Debug();
};

#endif 

Here is the code for CPP file with the member function definitions:

#include "TicTacToe.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cassert>

using namespace std;

TicTacToe::TicTacToe() 
{
    board = new int*[3];
    assert(board != 0);

    for(int i=0;i<3;i++)
    {
        cout << "Constructor Ran again" << endl;    //for testing
        board[i] = new int[3];
        assert(board[i] != 0);
        for(int j=0;j<3;j++)
            board[i][j] = 9;
    }
}

TicTacToe::TicTacToe(TicTacToe &copy)
{
    board = new int*[3];
    assert(board != 0);
}

TicTacToe::~TicTacToe()
{
    if(board)
        delete[] board;
}

void TicTacToe::PrintBoard() 
{
    for(int i=0;i<3;++i)
    {
        for(int j=0;j<3;++j)
        {
            cout << "| ";

            switch(board[i][j]){
            case 0:
                cout << "O ";
                break;
            case 1:
                cout << "X ";
                break;
            case 9:
                cout << "  ";
                break;
            }
        }
        cout << "|" << endl;
        cout << "------------" << endl;
    }
}

bool TicTacToe::ValidMove(int row, int col) 
{
    bool valid = false;
    if(row < 3 && col < 3)
    {
        if(board[row][col] == 9)
            valid = true;
    }
    return valid;
}

bool TicTacToe::PlayerMove(int player, int row, int col) 
{
    bool done = false;

    if(ValidMove(row,col) == true)
    {
        if(player == 1)
            board[row][col] = 1;
        else
            board[row][col] = 0;
        done = true;
    }

    return done;
}

TicTacToe::Status TicTacToe::GameStatus() //This function is the problem
{
    int check, empty = 0;
    bool done = false;

    for(int i=0;i<3;++i)
    {
        for(int j=0;j<3;++j)
        {
            check += board[i][j];
            if(board[i][j] = 9)
                empty++;
        }
        if(check == 0)
            return WinO;
        else if(check == 3)
            return WinX;
        check = 0;
    }

    if(empty == 0)
        return Draw;

    for(int i=0;i<3;++i)
    {
        for(int j=0;j<3;++j)
            check += board[j][i];
        if(check == 0)
            return WinO;
        else if(check == 3)
            return WinX;
        check = 0;
    }

    check = board[0][0] + board[1][1] + board[2][2];
    if(check == 0)
        return WinO;
    else if(check == 3)
        return WinX;
    check = 0;

    check = board[0][2] + board[1][1] + board[2][0];
    if(check == 0)
        return WinO;
    else if(check == 3)
        return WinX;
    check = 0;


    return Continue;
}



void TicTacToe::Debug()
{
    //cout << &board[0][0] << endl;
    for(int i=0;i<3;++i)
    {
        for(int j=0;j<3;++j)
            cout << board[i][j];
        cout << endl;
    }
}

Here is the driver file I am using to test:

#include "TicTacToe.h"
#include <iostream>
#include <cassert>

using namespace std;

int main()
{
    int row, col;
    bool valid;
    enum Status{WinX, WinO, Continue, Draw};
    TicTacToe * T;
    T = new TicTacToe;
    assert(T != 0);

    cout << "There are 2 players. P1 is x P2 is o" << endl;

    do
    {
        T->PrintBoard();

        valid = false;
        while(valid == false)
        {
            cout << "\nP1 choose a cell" ;
            cin >> row >> col;

            if(T->ValidMove(row, col) == true)
            {
                T->PlayerMove(1, row, col);
                valid = true;
            }
            else
            {
                cout << "Not a valid choice" << endl;
                valid = false;  
            }

        }

        T->PrintBoard();
        cout << endl;

        T->GameStatus();  //<<<<<this is the pain in my butt

        T->PrintBoard();

        valid = false;
        while(valid == false)
        {
            cout << "\nP2 choose a cell" ;
            cin >> row >> col;

            if(T->ValidMove(row, col) == true)
            {
                T->PlayerMove(2, row, col);
                valid = true;
            }
            else
            {
                cout << "Not a valid choice" << endl;
                valid = false;  
            }
        }   
    }
    while(/*T->GameStatus() == Continue*/ 1==1);
                  //the call to GameStatus was commented out of the
                  //while statement for testing


    return 0;

    }

I know the code inside of the GameStatus function is far from pretty but the array is messed up before any of those lines are processed.

I left all of the other functions just to show that they work properly without issue.

Thanks in advance for any help you may be able to give.

  • 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-05-27T18:23:16+00:00Added an answer on May 27, 2026 at 6:23 pm

    You’ve got a simple typo in your code..

    if(board[i][j] = 9) // will always return true (refp)
      empty++;
    

    Other remarks

    When looking at your code a bit more thoroughly I see that you have a few other miss-happens, intentional or unintentional.. that I don’t know:

    • int check is not initialized in TicTacToe::GameStatus
    • You are not freeing the allocated memory properly, you’ll
      need to free all entries in board, ie. delete board[i])

    I don’t like bugs, how can I get rid of the operator= vs operator== problem?

    A quite common method to circumvent the problem of making a typo and writing = when you mean to compare (==) two variables is to flip the operands around (if one of them is a constant value, such as 9.

    if(9 = board[i][j]) will not compile and such a bug would’ve never appeared in your code.

    I’ll have to say that I don’t like writing my statements that way.. though it’s a quite common method, especially in the "beginner" segment.

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

Sidebar

Related Questions

Earlier I had similar issue. I have created asp mvc default template project and
I have a test db on godaddy, I have had a similar issue before
i have seen similar posts here but i have had no luck solving my
I have found some similar Que's on SO but had not find the solution.
I have had this problem crop up a few times and I can't figure
I have had several situations when i would like to do that. This could
I have had a few problems getting this right, so I wanted to ask
A few days ago, I had an issue with ASP.Net threading. I wanted to
I have tried searching the database for a similar issue, but was unsuccessful in
I'm using NSFetchedResultsController. Previously I had a similar issue when the database has no

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.