Help with runtime error in code.

Firstly the code, or the error prone parts of it.
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
//....
std::string Moves[6][3] = 
	{
		{"U","U'","U2"},
		{"D","D'","D2"},
		{"F","F'","F2"},
		{"B","B'","B2"},
		{"R","R'","R2"},
		{"L","L'","L2"}
	};
std::string scramble = "";
std::random_shuffle(Moves, Moves+18);
scramble = "";
int tempx = -1, tempy = -1;
int x = 0,y = 0;
for (int i = 0; i < 25; i++)
{
	while (true && tempx != -1 && tempy != -1)
	{
		x = rand()%6;
		y = rand()%3;
		if (tempx != x && tempy != y)
			break;
	}
	tempx = x;
	tempy = y;
	scramble += Moves[x][y];
	scramble += " ";
}
/....


Now to come to the problem.
When I run the program, a dialogue box comes up with the error:
Unhandled exception at 0x77026344 in Cube Scrambler.exe: 0xC0000005: Access violation.

I know that the error is related to y, tempy[/code, or the Multidimensional array [code]Moves.
If I make the program using a single dimensional array, and excluding x & y, the error doesn't come up (though I don't get to do exactly as I want).
As to what I am making,
This is to generate an set of moves to scramble a Rubik's cube.
The 2-d array, Moves contains a list of all the regular moves to be used in the scramble.
Now I wouldn't want U & U' together as they would cancel each other out (one is clockwise turn, other anti-clockwise turn), ans so on for any set of moves.
Similarly, U2 & U together wont work either. It is the same as U'. Since I am limiting myself to 25 move scramble (most common), this would be waste of moves.


That aside, I tried another approach, one which gave no runtime error.
I made Moves into a single dimensional
array, got rid of anything related to y or tempy.
Now my logic was that as there is a difference of 3 between 2 consecutive sets of similar moves, if I find the difference of the temp& x ( the previous and current index for Moves) and see if it is greater than or equal to 3, then it might work, but for some reason it didn't.
Mathematically, the above was:
abs(temp-x) >= 3

Any help?

EDIT:
(and sorry for such a long post...)
One more thing. Everything in the code, not mentioned here is just showing the scramble etc, and not regarding the problem in any way whatsoever.
Last edited on
What compiler do you use?? This doesn't compile in GCC v4.3.4 (ideone.com). I had to change your line 12 to std::random_shuffle(*Moves, *Moves + 18); to make it work because Moves is a 6-item array of std::string[3], not really an individual array element (string).

See https://ideone.com/Ny6uh for the sample code I compiled. That shows the randomization result. Have you done a similar check on your side?
Opps. Forgot to remove the random shuffle...
That is the root of the problem in both the cases.
I use the Visual C++ compiler.
Thanks for it.
Topic archived. No new replies allowed.