to me makes sense, again i'm near expert. i'm working on project first programming course , i've been stuck on question day, have done 5 hours of cumulative research trying figure out how possible.
basically need shuffle these 'cards' memory game , can't figure out why it's not working. program runs fine none of images change. put cout
in there make sure value changing each time, don't know why won't swap values. tried shuffle , random_shuffle thing did not right, if show me has code ever grateful. i'm puzzled why won't work. if provide working example explaining went wrong amazing.
void shuffle(int board[][ncols]) { random_device rd; mt19937 gen(rd()); uniform_int_distribution<> dis(1, 6); (int = 0; < 20; i++) { int randomx = dis(gen); swap(board[randomx][randomx], board[randomx][randomx]); cout << "num = " << randomx << endl; } }
i'm right in calling function using shuffle(board);
correct? it's weird because there's built in function called shuffle correct?
the main reason swap(board[randomx][randomx], board[randomx][randomx]);
nothing trying exchange board diagonal element itself. leads unchanged board.
first of all, need select 2 random elements, need select random variables line , column of each element array, or, in example, select elements in diagonal of matrix (these ones equal line , column ids) so:
for (i = 0; < 20;) { int randxa = dis(gen), // i'll suppose right work randya = dis(gen), randxb = dis(gen), randxb = dis(gen); if (randxa != randxb || randya != randyb) { /* have 2 distinct elements. */ swap(board[randxa][randya], board[randxb][randyb]); /* need increment conditionally or can * finish less 20 swaps. */ i++; } /* if */ } /* */
be careful, supposed dis(gen);
generating valid number. if matrix not square, you'll have adjust randx<a,b>
, randy<a,b>
have different ranges (one goes 0..maxx-1
, other 0..maxy-1
. in case, you'll have use:
int randxa = rand() % maxx, randxb = rand() % maxx, randya = rand() % maxy, randyb = rand() % maxy;