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.