Multidimensional Array Assignment

I posted this in beginners and it generated no response so I figured I'd post it here
Hi guys, semi-noob here. I am making my third implementation of Tic Tac Toe with Artificial intelligence with this version being more streamlined and with less (Hopefully 0) globals. I am having trouble with the assignment and passing of multidimensional arrays. Specifically one, GameBoardInit[3][3], that contains a numeric representation of the game board

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
int main()//Game Loop
{
    using namespace std;
    //Variables
    int NumberOfPlayers;
    string PlayerOneName, PlayerTwoName;
    int Difficulty;
    char GameBoardState[3][3] = {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
    int GameBoardInit[3][3] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    int Move;

    //Game Initialization
    while (NumberOfPlayers == 0 || NumberOfPlayers > 2) //PseudoRecursive Menu Loop.
    {
        NumberOfPlayers = GameSetup();
    }
    PlayerOneName = PlayerNameInput(1, NumberOfPlayers); //Gather Player 1 Info
    if (NumberOfPlayers == 2) PlayerTwoName = PlayerNameInput(2, 0); //Gather Player 2 Info
    Difficulty = SetDifficulty();
    DisplayGameBoard(GameBoardState);
    //GameBoardInit[3][3] = InitializeGameBoard(GameBoardState);//This is the one I am having problems with
    Move = ComputerMoveMainLoop(GameBoardInit, Difficulty);
    cout << Move;

    return 0;
}


The Problematic Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int InitializeGameBoard(char GameBoardChar[][3])
{
    int x;
    int y;
    int GameBoardInit[3][3];
    for (x = 0; x < 3; x++)//Cycle Through Collumns
    {
        for (y = 0; y < 3; y++)//Cycle Through Rows
        {
            if (GameBoardChar[x][y] == 'X') GameBoardInit[x][y] = 1;
            else if (GameBoardChar[x][y] == 'O') GameBoardInit[x][y] = 2;
        }
    }
    return GameBoardInit[3][3];
}


Is there a way to alter GameboardInit and pass it back with another function. Running this code currently crashes the program. I'm using windows if that helps any.
Remember that in C and C++, the range of indices in an array is 0 to N-1, where N is the number of elements in the array.

1
2
int xs[ 12 ];
xs[ 12 ] = -7; // hey, I'm indexing an element not in the array. 

With multidimensional arrays, the error is compounded.

Hope this helps.
Duoas, You are correct, and I am aware of this. However, I am not assigning a value to one single point on the array, but reassigning values to the whole array. Is there a way to do this like I have set up?
closed account (D80DSL3A)
You can't return an entire array like that. Try passing the 2nd array to the function as an argument:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void InitializeGameBoard(char GameBoardChar[][3], int GameBoardInit[][3] )
{
    int x;
    int y;
    for (x = 0; x < 3; x++)//Cycle Through Collumns
    {
        for (y = 0; y < 3; y++)//Cycle Through Rows
        {
            if (GameBoardChar[x][y] == 'X') GameBoardInit[x][y] = 1;
            else if (GameBoardChar[x][y] == 'O') GameBoardInit[x][y] = 2;
        }
    }
    return;// return nothing
}

Call it in main() like so:
InitializeGameBoard(GameBoardState, GameBoardInit);
But then they would have to be Globals, right?
There's no need. Just like how you didn't need GameBoardState to be global to pass it, neither do you need GameBoardInit to be global.
Yeah, but when I want to use them in other functions they won't work?
The example given to you used the same names as your variables for arguments. But they aren't exactly the same thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void sum( int a[], unsigned n )
{
  int result = 0;
  for (unsigned x = 0; x < n; x++)
    result += a[ x ];
  return result;
}

int main()
{
  int primes[ 7 ] = { 2, 3, 5, 7, 11, 13, 17 };
  cout << "The sum of the first seven primes is " << sum( primes, 7 ) << "\n";

  int fibonaccis[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 };
  cout << "The sum of the first six fibonacci numbers is " << sum( fibonaccis, 6 ) << "\n";
}

The argument a could have been named "primes" or "fibonaccis" or anything else, because it doesn't matter. When you pass your array as argument, it gets a new name inside the function.

The new name could be the same as the old name, but that doesn't matter.


So, any time you have a function, pass the game board arrays as arguments the same way.

Hope this helps.
closed account (D80DSL3A)
You would have to pass them to other functions too.

The use of global variables is often discouraged here, but I do use them sometimes for the "core" objects in a game eg. the gameboard, or other objects which the whole game centers on. I think it's reasonable in these cases.
Agreed.
Topic archived. No new replies allowed.