Advertisement

scrambling a rubik's cube using srand() C++ - Opengl

Started by April 09, 2003 06:09 PM
5 comments, last by Astro 21 years, 10 months ago
Hi all I am trying to scramble my rubik''s cube using the srand function in C++. I''m not sure how exactly to go about it. All my rotation of the cube itself is done using the keyboard keys, do I have to link the keyboard keys with the srand()?? Here is my code below that deals with the rotations Code: switch(key) { case ''z'': case ''Z'': controlKey[4]=true; sx++; sy=sz=0; if(sx>SIZE) sx=1; break; case ''c'': case ''C'': controlKey[6]=true; sz++; sx=sy=0; if(sz>SIZE) sz=1; break; case ''x'': case ''X'': controlKey[5]=true; sy++; sx=sz=0; if(sy>SIZE) sy=1; break; } void rotate_selected(int direction){ int i; Rubik *unit; for( i=0; i<26; i++){ unit=faces; if(direction<0) { rotcounter=-90; //for the anti-clockwise rotation if(unit->x1==sx-2) unit->Rotate(Back); else if(unit->y1==sy-2) unit->Rotate(CCW); else if(unit->z1==sz-2) unit->Rotate(Left); else if(direction>0){ rotcounter=90; //for the clockwise rotation if(unit->x1==sx-2) unit->Rotate(Forward); else if(unit->y1==sy-2) unit->Rotate(CW); else if(unit->z1==sz-2) unit->Rotate(Right); } } } } /* the input for the application*/ void inputUpdate(void) { float step = 3.0; int size=3; if(controlKey[0]) rotate_x -= step; /* up */ if(controlKey[1]) rotate_x += step; /* down */ if(controlKey[2]) rotate_y -= step; /* left */ if(controlKey[3]) rotate_y += step; /* right */ if(rotcounter==0){ if(controlKey[7]) { /* counter-clockwise rotation - A selected */ rotate_selected(1); } else if(controlKey[8]) { /* clockwise rotation - S selected */ rotate_selected(-1); } } } static void KeyboardUp( unsigned char key, int x, int y ) { (void) x; (void) y; switch(key) { case ''z'': case ''Z'': controlKey[4]=false; break; case ''x'': case ''X'': controlKey[5]=false; break; case ''c'': case ''C'': controlKey[6]=false; break; case ''a'': case ''A'': controlKey[7]=false; break; case ''s'': case ''S'': controlKey[8]=false; break; } } I was thinking of doing somethin like this to generate the scramble??? void scramble_cube() { GLint i, rotationcounter; char *axis; GLint new_state, st; /* srand() causes the computer to read its clock to obtain a value, therefore creates a different random number each execution of the program*/ srand( time( 0 ) ); for (i = 0; i < 10; i++) { new_state = rand() % 3; switch (new_state) { case 0: axis = "x1"; break; case 1: axis = "y1"; break; case 2: axis = "z1"; break; } st = rand() % 3; rotationcounter = rand() % 4; for (; rotationcounter > 0; rotationcounter--); ???????????? } } am I going about this the right way??? If anyone has any ideas, please let me know. Thanks again Astro
You must call srand(time(0)) only once - usually in main().


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
An alternative to scrambling the cube by simulating the random moves would be to construct a valid scrambled state - the positions of the last two edge pieces or the last two corner pieces and the orientations of the last one of each are determined by the rest of the cube. Of course, actually working out the invariants to use could be tricky. (I used to be able to reassemble a disassembled Rubik''s Cube successfully into a valid scrambled state, but I''ve forgotten the details)

Another point is that it''s known that it takes 21-36 (probably better limits exist, but googling hasn''t found them easily) moves to reach the most remote scrambled states by the shortest route, so you want to perform at least that many scrambling steps otherwise you''ve got the majority of starting states never arising.
I got the scrambling working by using srand()

thanks for all your help
quote:
Original post by rmsgrey Another point is that it's known that it takes 21-36 (probably better limits exist, but googling hasn't found them easily) moves to reach the most remote scrambled states by the shortest route, so you want to perform at least that many scrambling steps otherwise you've got the majority of starting states never arising.
I recall that they are very certain that the number of moves required to win from the furthest position away is in the very low 20's, and it is believed that it may even be as low as 18. It will be interesting to see when computers have more speed and RAM to be able to solve it 'GodLike'.

Jason Doucette - my online résumé page: www.jasondoucette.com
projects, real-time graphics, artificial intelligence, world records, wallpapers/desktops

[edited by - Jason Doucette on April 14, 2003 6:39:38 PM]
Jason Doucette / Xona.comDuality: ZF — Xbox 360 classic arcade shmup featuring Dual Play
What is the current state of computer''s solving a rubik''s cube? Can they do it? Can they get close? I''m interested in what is currently possible.
Advertisement
Take a look at this page:
Optimal Rubik''s cube solver. It states:

"...I wrote an optimal cube solver. This is supposed to be a practical implementation of "God''s Algorithm" for Rubik''s Cube. Although a very small fraction of positions may require months of searching, most can be solved in a day or so, and many require much less time than that. ... I use a 200MHz Pentium Pro processor, configured with 128Mb of RAM."

Jason Doucette - my online résumé page: www.jasondoucette.com
projects, real-time graphics, artificial intelligence, world records, wallpapers/desktops
"Great minds discuss ideas, average minds discuss events, small minds discuss people." - Admiral Hyman G. Rickover
Jason Doucette / Xona.comDuality: ZF — Xbox 360 classic arcade shmup featuring Dual Play

This topic is closed to new replies.

Advertisement