Array problems

Hi, i've been learning c++ for a month or so now. It's been going well but i've just hit a snag while trying to make a tic tac toe game. In my array when I input a value into the left column it will also change the value in the right column but one higher. In other works modifying gameArray[2][0] will change gameArray[1][2]. Puzzled as to why this is happening.

I've also been making a connect 4 game and the same thing has happened when inputting a value into the leftmost column the rightmost column one row higher will also change.

This is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//Tic Tac Toe Game
//V1.0 Player 1 vs PLayer 2

#include <iostream>

/*Function for printing out the board*/

void boardPrint(char gameArray[2][2])
{
     std::cout << std::endl;           //to keep the new board away from anything else on the screen
     for(int iii = 0; iii<3; iii++)
     {
             for(int jjj=0; jjj<3; jjj++)
             {
                     if(jjj!=2)
                         std::cout << "   " << gameArray[iii][jjj] << "\t|";               // | used to give structure to grid
                     else
                         std::cout << "   " << gameArray[iii][jjj] << std::endl;
             }
             if(iii!=2)
                std::cout << "-------------------------" << std::endl;
     }
     std::cout << std::endl;
}

void takeTurn(char playerSymbol, int playerNumber ,char (&gameArray)[2][2]) //Reference used so the array can be changed by the function
{
          std::cout << "Player " << playerNumber << "s turn.\n";
          std::cout << "Row number: ";
          int rowNumber;
          std::cin >> rowNumber;
          std::cout << "Column number: ";
          int columnNumber;
          std::cin >> columnNumber;
          gameArray[rowNumber - 1][columnNumber - 1] = playerSymbol;
}

/*sets all elements of array to 0*/

void clearBoard(char(&gameArray)[2][2])
{
     for(int iii=0; iii < 3; iii++)
     {
             for(int jjj=0; jjj < 3; jjj++)
             {
                     gameArray[iii][jjj]= 0;
             }
     }
}


int main()
{
    bool game = true;
    char gameArray[2][2];
    clearBoard(gameArray);
    
    std::cout << "Welcome to the game of tic tac toe.\n\nInstructions:\nPlayer 1 will start and will be X.\n"
    << "To make a move you must enter the row number and then the column number of the \nspace you want to play each followed by the return key.\n\n"; 
    
    while(game)
    {
        for(int iii=1; iii<=9; iii++)                                               //There will be 9 turns taken
        {
            if(iii%2==1)
            {
                takeTurn('X',1,gameArray);
                boardPrint(gameArray);
            }
            else
            {
                takeTurn('O',2,gameArray);
                boardPrint(gameArray);
            }
        }
                                                               
    
    }  
    
    return 0;
}


Any useful tips on how to improve my coding will also be vastly appreciated.
Thanks
:)
closed account (2b5z8vqX)
Any useful tips on how to improve my coding will also be vastly appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for(int iii=0; iii < 3; iii++)
{
             for(int jjj=0; jjj < 3; jjj++)
             {
                     gameArray[iii][jjj]= 0;
             }
}

for(int iii = 0; iii<3; iii++)
{
             for(int jjj=0; jjj<3; jjj++)
             {
                     if(jjj!=2)
                         std::cout << "   " << gameArray[iii][jjj] << "\t|"; 
                     else
                         std::cout << "   " << gameArray[iii][jjj] << std::endl;
             }
             if(iii!=2)
                std::cout << "-------------------------" << std::endl;
}

Your program attempts to access elements of the array at out-of-bounds array indexes multiple times. This results in undefined behavior (http://en.wikipedia.org/wiki/Undefined_behavior) and could be the cause of the problem you mentioned in your post.

The bounds of an array is its size (the number of elements it contains) minus one, so the right operand of the less than operator (<) in the conditions of the preceding for loops should be two.
Last edited on
The bounds of an array is its size (the number of elements it contains) minus one, so the right operand of the less than operator (<) in the conditions of the preceding for loops should be two.


Would it not be 2 if I was using the operator (<=)? If i change the loop values from 3 to 2 in the boardClear function then it wont clear the board fully.
Last edited on
Would it not be 2 if I was using the operator (<=)? If i change the loop values from 3 to 2 in the boardClear function then it wont clear the board fully.


concerning:

char gameArray[2][2];

This is an array with 4 elements. The valid indexes for gameArray are:

1
2
3
4
gamearray[0][0]
gameArray[0][1]
gameArray[1][0]
gameArray[1][1]


Valid indices are 0 to size-1, and size is 2 here, so valid indices are 0 to 1.
Last edited on
ahh thank you so much.
Silly mistake was counting from 0 so 2 was a 3 to me, if you know what i mean.
Also what would be the easiest way to check if the game has been won. Is there any quicker way than a bunch of if statements or a switch statement?
Topic archived. No new replies allowed.