0

i'm buisy learning myself to program and i'm trying to copy a game that i find fun to play to c++ (basicly just for the sake of learning. So if i formulated the question wrong please forgive me.)

Now i have an issue with randomising a 2 dimentional array. The thing is that i manage it partially to get it to work but i just fail to reason how i can make it fully work.

The code:

// Random generate and return nr 2 or 4 on calling this function.
int startValue1(){

    srand(time(NULL));

    int arrayStart[2] = {2, 4};
    int randIndex = rand() % 2;

    return arrayStart[randIndex];
}
// Random generate and return nr 4 or 2 on calling this function.
int startValue2(){

    srand(time(NULL));

    int arrayStart[2] = {4, 2};
    int randIndex = rand() % 2;

    return arrayStart[randIndex];
}

int tester(){

//generate 2 start values and assign to variables
    int a = startValue1();
    int b = startValue2();

//initialize 2 dimentional array and add the 2 starting numbers.
  int board[4][4] = {{a,0,0,0},
                    {0,0,0,0},
                    {0,0,0,0},
                    {0,0,0,b}
                    };

// print out the board in console to check/test the function(S).
   for(int row = 0; row<4; row++){

        for (int column = 0; column<4; column++){
            cout << board[row][column] << " ";
        }
        cout << endl << endl;
    }
    cout << endl;

//Randomize the elements in the 2 dimentional array.
   random_shuffle(board, board +4) ;


//print out the array to console after the board is randomised (for testing/checking only)
    for(int row = 0; row<4; row++){

        for (int column = 0; column<4; column++){
            cout << board[row][column] << " ";
        }
        cout << endl << endl;
    }
}

The output of this looks something like this :

0 0 0 0

0 0 0 0

4 0 0 0

0 0 0 2

The problem is that the elements only arrange verticaly and never horizontally as if the random_shuffle function only works on one dimention. I did read up on that function but i just fail to see/reason how i can make it work on a 2 dimentional array. Hopefully someone can give me some pointers or directions to the info on how to solve this.

Personally i thought that it would be best if i just now somehow arrange the elements horizontally and continue from there. But if there is a better solution i'm al open for it ofcourse.

(In case for anyone who wonders: i'm trying to remake the webgame 2048 in c++ and i plan to use this code to initialize the board and make it ready to play. Most likely it's not the best approach to solve this but i'm trying to tackle one problem at the time and learn from it and just keep redoing the code till it works.)

user2645886
  • 47
  • 1
  • 9
  • 1
    Move `srand(time(NULL));` out of every function and place it at the start of `main()`. It re-seeds the random sequence. – Weather Vane Apr 12 '15 at 18:39
  • 1
    `board` is a one-dimensional array of four elements, of type `int[4]`. It is those elements that you are shuffling. In other words, you move whole rows around, but never elements within the row. Make it `int* flat = &board[0][0]; random_shuffle(flat, flat + 4*4);` – Igor Tandetnik Apr 12 '15 at 18:39
  • @ igor Thank you verry much. I suspected there was a solution like that but i just couldn't get it right in my head. I see what went wrong there. Thanks for the info and example. – user2645886 Apr 12 '15 at 18:46
  • @WeatherVane after reading up on that; i was kinda going for that. If i dont code it like this, the start nrs. will be the same in most of the cases and thats something i dont want to occure to often. And after thinking further about it: i suspect that i'll make a class out of this as i have some more functions to add to this all ofcourse. – user2645886 Apr 13 '15 at 07:12
  • And as you *do* code it like this, the start nrs. from `rand()` *will* be the same in most of the cases, because the seed value is a seconds count, and you call `srand()` again immediately after the first call. This resets the random sequence to the same initial value, resulting in `a` and `b` having the values `2` and `4`, or `4` and `2`, although rarely they will both be `2` or `4` (not very random) if the seconds count just happens to tick between the two calls. I suggest you read up on the use of `srand()`. http://stackoverflow.com/questions/322938/recommended-way-to-initialize-srand – Weather Vane Apr 13 '15 at 16:10

0 Answers0