1

how do I assign char array 'startData'

int main()
{

Player plyr_1('X');
Player plyr_2('O');

char startData[8][8] = {{' ',' ',' ',' ',' ',' ',' ',' '},
                        {' ',' ',' ',' ',' ',' ',' ',' '},
                        {' ',' ',' ',' ',' ',' ',' ',' '},
                        {' ',' ',' ','O','X',' ',' ',' '},
                        {' ',' ',' ','X','O',' ',' ',' '},
                        {' ',' ',' ',' ',' ',' ',' ',' '},
                        {' ',' ',' ',' ',' ',' ',' ',' '},
                        {' ',' ',' ',' ',' ',' ',' ',' '}};

at the start of this loop

menu = false;

while(menu == false)
{
    int turnCounter = 1;

    Player currentPlyr('X');
    currentPlyr = plyr_1;

    plyr_1.data = startData;
    plyr_2.data = startData;

    plyr_1.score = 2;
    plyr_2.score = 2;

    selectMenu = currentPlyr.mainMenu();

inside array 'data' in created class 'plyr_1' and 'plyr_2'

'data' is a public char array from class 'Game'

class Game
{
    public:
        char data[8][8];

to reset the board everytime the loop starts?

Thanks

Dax Fohl
  • 10,654
  • 6
  • 46
  • 90
  • If you show code, tag what language it is. It helps others find the question and also affects the syntax highlighting. C++? – crashmstr Jul 29 '15 at 19:14
  • Why don't you use a function that copies over the array manually if you need a copy of it. if you don't need a copy and are just using it to read information, why not use a pointer? – jcjunction Jul 29 '15 at 19:18
  • [This answer](http://stackoverflow.com/a/3599048/509868) might help – anatolyg Jul 29 '15 at 19:47

2 Answers2

2

If your problem is that you can't assign arrays, there are several solutions.

You can either use a custom function to reset your array:

#include <cstddef>

constexpr std::size_t arraySize = 8;
void resetMyArray(const int startingArray[][arraySize], 
                  int toReset[][arraySize])
{
  for( std::size_t i = 0 ; i < arraySize ; ++i )
    for( std::size_t j = 0 ; j < arraySize ; ++j )
      toReset[i][j] = startingArray[i][j];
}

int main(int argc, char* argv[])
{
  int x[arraySize][arraySize];
  int y[arraySize][arraySize];
  resetMyArray(x, y);
  return 0;
}

or use an std::array instead:

#include <array>
#include <cstddef>

int main(int argc, char* argv[])
{
  constexpr std::size_t arraySize = 8;
  std::array<std::array<int, arraySize>, arraySize> x;
  std::array<std::array<int, arraySize>, arraySize> y;
  x = y;
  return 0;
}

Edit: You could also use an std::vector, but that will probably be less efficient because the std::vector doesn't know how big your array will be at compile-time.

Edit: If you're concerned about the array's scope, πάντα ῥεῖ gives a solution.
Personally, i'd declare it as static constexpr in the Game class (which is a bit like defining a global) although i don't know exactly how your classes interact.

Caninonos
  • 1,214
  • 7
  • 12
0

Well, you could have a global array that is used to initialize your board, and will be shared among several Player instances.

Just provide startData as global static outside main() scope, and have Player receive a reference for that instance.


Though, I don't think the above mentioned would be a good solution.

You should rather have a class Board containing and consistently managing an internal member startData (or better boardData), and share a reference to this among Player instances.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190