a simple representation of the first Rubik's Cube
For any n -order Rubik's Cube, there are six faces (surface) with n*n squares per polygon. In object-oriented programming, we can view the cube (cube), the surface of the cube (surface) and the blocks (block) as objects.
Definition of a Rubik's cube: Six faces stored in an array
" " <summary>" "represents a cube of a specified order" " </summary> Public ClassCubeclass" " <summary> " "order of Rubik's Cube" " </summary> PublicCuberank as Integer " " <summary> " "six surfaces of a Rubik's Cube" " </summary> PublicSurfacearray (5) asCubesurfaceclassEnd Class
Cube's polygon definition: a two-dimensional array of blocks stored as N*n
" " <summary> " " a face that represents a Rubik's Cube " " </summary> Public Class Cubesurfaceclass " " <summary> " " block data for the cube surface " " </summary> Public as Cubeblockclass End Class
Block definition for Rubik's Cube: Each block has a separate color
" " <summary>" "represents a block on the magic side" " </summary> Public ClassCubeblockclass" " <summary> " "the color of the current block" " </summary> PublicBlockcolor asColor PublicX as Integer 'Number of columns PublicY as Integer 'Number of rowsEnd Class
Above, we have completed a simple definition of the Rubik's Cube class, and established a subordinate relationship. One thing to note here is that a block (Blockclass) is a single color block of a Rubik's Cube.
So the N-order Rubik's Cube should have 6*n*n color blocks, taking the third-order Rubik's Cube as an example, it should have 54 color blocks (Blockclass).
the spatial relationship between the second section faces
The six sides of the Rubik's Cube are not independent, but there is a certain spatial relationship. The previous definition of the Cubeclass in the Surfacearray () represents the six sides of the Rubik's Cube, now the index 0~5 respectively indicate the top of the Rubik's Cube, the bottom, left, right, front, rear six faces.
Surfacearray (0): Top floor
Surfacearray (1): Bottom
Surfacearray (2): Left
Surfacearray (3): Right
Surfacearray (4): Front
Surfacearray (5): Rear
Fig. 2.1 Space position of Rubik's Cube six plane
This determines the spatial orientation of each polygon and adds the following definition to Surfaceclass:
" " <summary> " " adjacent layers of the current surface (top, bottom, left, right, front, and rear) " " </summary> Public Neiboursurface (5 as Cubesurfaceclass
The index of Neiboursurface () is sequentially indicated from 0~5 when the front top, bottom, left, right, front, and rear. For example, in Figure 2.1, "right", its neiboursurface () should be
Neiboursurface (0): Top floor
Neiboursurface (1): Bottom
Neiboursurface (2): Front
Neiboursurface (3): Rear
Neiboursurface (4): On the right side, the "front" of each face is its own
Neiboursurface (5): Left
But above is the default "right" of "top" is the top layer. So we also need to make a strict definition of "top" of each face:
Fig. 2.2 Space position of Rubik's Cube six plane expansion
Fig. 2.3 The direction of the "top" of the Rubik's six face
From above, we can determine the spatial relationship between the faces:
DimTemparray (,) as Integer= {{2,3,4,5,0,1}, {3,2,4,5,1,0}, {1,0,4,5,2,3}, {0,1,4,5,3,2}, {0,1,2,3,4,5}, {0,1,3,2,5,4}}'spatial adjacency Relationship matrix
The matrix row value refers to the index of a polygon in Surfacearray (), and the number of columns represents the index of the polygon adjacent to the polygon at Surfacearray (). The "front" of each polygon mentioned above is itself , and the fifth column of the matrix is from 0 to 5.
Add the following method to the Cubeclass class and call it in the constructor:
" " <summary> " "Initialize the spatial adjacency between each surface layer" " </summary> Public Subinitsurface ()DimTemparray (,) as Integer= {{2,3,4,5,0,1}, {3,2,4,5,1,0}, {1,0,4,5,2,3}, {0,1,4,5,3,2}, {0,1,2,3,4,5}, {0,1,3,2,5,4}}'spatial adjacency Relationship matrix fori =0 to 5 forj =0 to 5Surfacearray (i). Neiboursurface (j)=Surfacearray (Temparray (i, j))Next Next End Sub
The third section of the Rubik's Cube initialization
Rubik's Cube (Cubeclass) constructor: Six-side color standard: top-white, bottom-yellow, left-orange, right-red, front-green, rear-blue
" " <summary> " "Create a new cube of a specified order" " </summary> " " <param name= "Nrank" >number of orders specified</param> Public Sub New(ByValNrank as Integer) DimColorarr () asColor ={color.white, Color.yellow, Color.orange, Color.Red, Color.green, Color.Blue} fori =0 to 5Surfacearray (i)=NewCubesurfaceclass (Nrank, I, Colorarr (i))NextCuberank=Nrank initsurface ()End Sub
The Magic aspect (Cubesurfaceclass) constructor:
" " <summary> " "the order of the current Rubik's Cube" " </summary> PublicCuberank as Integer " " <summary> " "data on the cube surface" " </summary> PublicBlockdata (,) asCubeblockclass" " <summary> " "adjacent layers of the current surface (top, bottom, left, right, front, and rear)" " </summary> PublicNeiboursurface (5) asCubesurfaceclass PublicIndex as Integer Public Sub New(Nrank as Integer, NIndex as Integer, Ncolor asColor)ReDimBlockdata (Nrank-1, Nrank-1) Cuberank=Nrank Index=NIndexDim Rnd as NewRandom fori =0 toNrank-1 forj =0 toNrank-1Blockdata (i, J)=NewCubeblockclass (i, J) Blockdata (I, J). Parentindex=Index Blockdata (i, J). Blockcolor=NcolorNext Next End Sub
The constructor of the cube block (cubeblockclass):
PublicParentindex as Integer " " <summary> " "the color of the current block" " </summary> PublicBlockcolor asColor PublicX as Integer 'Number of columns PublicY as Integer 'Number of rows Public Sub New(NX as Integer, NY as Integer) x=NX y=NYEnd Sub
At this point, an arbitrary order of the Rubik's Cube can be represented, and on this basis will be able to implement the magic cube twist logic.
resource sharing : Chinese Petroleum University (East China) Open class: Rubik's Cube and mathematical modelling (the course involves different methods than the above, and another way of thinking for your reference.) )
Rubik's Cube Game Realization (a): The representation of any order Rubik's Cube