Designing a Rubik's Cube Game Program first is to achieve the rotation of the Rubik's Cube and the rotation of the Rubik's Cube, which can be selected. This article introduces a quick and effective way.
Basic idea
The Rubi Cube has a total of 26, of which 6 is located at the center of each side. When the Rubik is rotated, they are not moving; 8 pieces are located in the top corner position, each has three different colors of different colors; there are two different different Small surface of color. According to this feature, we specify that the front and rear surface of the Rubik's cube is a surface, the upper and lower surfaces are the s surface, and the left right is d. Set color value sequence: a> s> d. Define array a [6] stores 6 central small pieces color, B [12] stores 12 colors of two small men, C [8] stores the color of three small men, A [i] The value is between 015 and vary; two faces from B [i]: B1> B2, then B [i] 03 These 4 bytes of the value of B2, 4-7 this 4 bytes store B1 value; C [i] 03 These 4 bytes store the color value of the D surface, 4-7 these 4 bytes store the color value of the S surface, 8-11 these 4 Bytes store the color value of the surface.
statement of problem
How to achieve the rotation of each face of the Rubik's Cube?
How to achieve the rotation of the Rubik's Cube so that the player can find the location of each small piece?
How to detect if the Rubik's Cube has been placed, that is, 9 small colors in each side?
Solution
Since the Rubik's cube has 6 faces, only 6 converted subunies are required. The following is the algorithm of these subunies as an example:
INTM (INTS) {INTD; D = ((S & OXF) | (S >> 4); Returnd;}
INTN (INTS) {INTD; D = (S >> 8) | (S & OXF) << 8) | (S & OXFO); Returnd;}
VoidC1 () {INTT;
T = B [0]; B [0] = m (B [4]); B [4] = m (B [8]); B [8] = m (B [5]); B [5] = m (t);
T = C [0]; C [0] = N (b [4]); B [4] = N (B [5]); C [5] = N (C [1]); C [1] = N (t);
Function INTM (INTS) exchanges S's 0-3 bytes to 4-7 bytes of values, INTN (INTS) exchanges S's 03 bytes to 8-11 bytes of values.
The function C1 () is achieved by the rotation of the Rubik's cube, because the upper transition of the Rubik's cube turns C [i] D side into a plane, turn the A surface into a D side, and the S is still the s surface, so to The 0-3 bytes of C (I) participating in the rotation are exchanged between 8-11 bytes, and then exchange each CI [i] value; since [i] is to change the AS surface to the SD surface or put SD The surface is changed to the AS surface, so the 0-3 bytes of each B [I] participating in the rotation are swapped in exchange with the value of 4-7 bytes, and then the value of each B [i] is swapped. This way, the rotation of the Rubik is completed.
INT1 (INTS) {INTD; D = (S >> 8 << 4) ((S & OXFO)
<< 4) (s & oxf); Returnd;}
Voidc3 () {INTT;
T = B [3]; B [3] = B [7]; B [7] = B [11]; B [11] = B [4]; B [4] = T;
T = C [0]; C [0] = L (C [3]); C [3] = L (C [7]); C [7] = L (C [4]); C [4] = L (t);}
Function INTL (INTS) is to exchange S's 4-7 bytes of 8-11 bytes. The function C3 () is implemented is the rotation of the Rubik's left. Because each B [i] not the Turn is not the AD side is the SD surface, the value of each B [i] is swapped as long as the value is rotated. Each C [I] is a transformation between the ASD surface and the SAD surface, so the 4-7 bytes of each C [I] are exchanged with 8-11 bytes, and then the value of each C [i] Cyclic exchange. This way, the leftmost rotation of the Rubik is completed. The algorithm of other subunies is similar. Realize the rotation of the right and upwards. Rotate upwards can first rotate the left and right layer, and then the value of the median A [I] and B [I] can be recycled, the program is as follows:
VoidTurn1 () {INTT; C4 (); C3 ()};
T = a [0]; A [0] = a [4]; A [4] = a [5]; A [5] = a [2]; a [2] = T;
T = B [0]; B [0] = m (b [2]; B [2] = m (B [2]); B [2] = B [10]; B [10] = m (B [8]); B [8] = m (t);
Similar to the method of rotating to the right.
Detecting the Rubik's Cube has been arranged, just detect if the value of each B [i] is consistent with the value of the two faces of the two faces, the value of the A [L] (with surface J> L) That is, whether there is B [I] = a [j] << 4 a [l]; at the same time, it is hysteresis every C [i] == a [j] << 8 a [l] << 4 A [H] (Side J> L> H), if you meet the conditions for all C [i], the Rubik's Skill has been exhausted.
Game process:
After running the program, the computer automatically disrupt the Rubik's cube, and then the player consistently with the keyboard to make the Rubi's square into the color of each nine. Operation method: '↑' Turn the magic square to rotate, '→' to turn right in the magic direction, by a blue circle logo it up and down, left and right, the position of the six layers of the six layers, press the spacer to turn the layer. The A key and the S key control the circular logo bidirectional conversion. Press the ESC button to exit the game.