0

Hello I am trying to create a tick Tack Toe Game For my College Project, The Board size of the game needs to be GENERIC using 2D array in C++. So I'm having trouble while initializing Default numbers(Places) identifier in an array

    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            Boards[i][j] = initial++;
        }
    }
    for (int i = 0; i < SIZE; i++)
    {
        for (int j = 0; j < SIZE; j++)
        {
            if (Boards[i][j] < 10) cout << " " << Boards[i][j] << "   |   ";
            else cout << Boards[i][j] << "   |   ";
        }

        cout << endl;
    }

As the variable 'initial' is a integer and i have to increment it in loop.I am quite not sure how to save it in char array (BOARD) Board needs to be char to display X,O

10
  • What does initial even represent? Why do you need to increment it? Commented Jun 2, 2017 at 21:40
  • Initial is the default values The Default values need to be 0-Size*Size; @Mureinik Commented Jun 2, 2017 at 21:41
  • why don't you leave it 0 without incrementing it ? By the way can you confirm that SIZE is a const ? Commented Jun 2, 2017 at 21:43
  • Why would i want to set it 0 while i want to store 0 - N numbers in the array this is how the board is supposed to look like. prntscr.com/ff8po6 Yep SIZE is a CONST Commented Jun 2, 2017 at 21:45
  • What issue are you having? It looks like you successfully stored the integer value in your char array. You just have to make sure the integer is never going to be above 127, because then it will not fit in a char. Commented Jun 2, 2017 at 21:46

1 Answer 1

1

Now that you have posted the full code, I can see a problem on this line and the other one like it:

cout << Boards[i][j] << "   |   ";

Since the type of Boards[i][j] is a char, the C++ standard library will just send that char to your terminal, and the terminal will try to interpret it as an ASCII character. You need to cast it to an int first so that the C++ standard library will format it properly for you:

cout << (int)Boards[i][j] << "   |   ";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.