. NET to achieve the Rubik's Cube game (i) any order of the Magic Cube representation _ Practical Skills

Source: Internet
Author: User

The simple expression of the first Rubik's Cube

For any n-order Rubik's Cube has six faces (Surface), each face has n*n square. In object-oriented programming, we can take the cube (cube), the cube surface (Surface) and the square (block) are regarded as objects.

Definition of Rubik's Cube: Six faces stored in an array

' <summary> '
represents a specified order of Rubik's Cube '
</summary> public
Class cubeclass
' <summary> '
cube order ' </summary> public
Cuberank as Integer '
<summary> '
' Rubik's Cube's six surfaces
"' </summary> public
Surfacearray (5) as Cubesurfaceclass

Cube's surface definition: squares are stored as n*n two-dimensional arrays

' <summary> '
represents the face of a Rubik's Cube '
</summary> public
Class cubesurfaceclass
' < Summary> ' '
cube surface block data
' </summary> public
blockdata (,) as Cubeblockclass end
Class

Block definition of Rubik's Cube: Each block has a separate color

' <summary> '
represents a square on the magic side
' </summary> public
Class cubeblockclass
' <summary  >
"' Current block color '
</summary> public blockcolor as color public x as Integer ' is the
number of columns public
y as Integer ' Number of lines end
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 the square (Blockclass) is the individual color block of the Rubik's Cube.

So the N-order Rubik's Cube should be 6*n*n a color block, the third-order Rubik's Cube as an example, it should have 54 color blocks (Blockclass).

--------------------------------------------------------------------------------

The spatial relationship between the second section

The six sides of the Rubik's Cube are not independent, but there is a certain spatial relationship. The front in the cubeclass definition of the Surfacearray () to represent the six sides of the Rubik's Cube, now index 0~5 respectively to indicate the top floor of the Rubik's Cube, the bottom, left, right, front, rear six sides.

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 six sides of Rubik's Cube

Thus, the spatial orientation of each surface is determined, and the following definitions are added to Surfaceclass:

' <summary> '
the adjacent surface layer (top, bottom, left, right, front and rear) of the current surface layer
</summary>

Public Neiboursurface (5) as Cubesurfaceclass

The index of the Neiboursurface () is indicated sequentially from the 0~5 to the top, bottom, left, right, front, and rear of the front. For example, in Fig. 2.1 "Right", its neiboursurface ()

should be for

Neiboursurface (0): Top floor

Neiboursurface (1): Bottom

Neiboursurface (2): Front

Neiboursurface (3): Rear

Neiboursurface (4): On the right, the "front" of each face is its own

Neiboursurface (5): Left

But the above is the default is "right" "top" is the top layer. So we also need

Make a strict definition of the "top" of each face:


Fig. 2.2 The space position of the Rubik's Cube six plane unfold


Fig. 2.3 The direction of the "top" of the six sides of Rubik's Cube

From the above, we can determine the spatial relationship between each surface:

Dim Temparray (,) 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},

The matrix row value refers to the index of a surface in Surfacearray (), and the number of columns represents the index of the adjacent face in Surfacearray (). The "front" of each face mentioned above is itself, and the fifth column of the matrix is from 0 to 5.

Add the following method in the Cubeclass class and call in the constructor:

' <summary> '
initializes space adjacent relations between the Layers
' </summary>
Public Sub initsurface ()
Dim Temparray (,) 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
}} ' spatially adjacent relation matrix for
i = 0 to 5
for j = 0 to 5
surfacearray (i). Neiboursurface (j) = Surfacearray (Temparray (i, J))
next
Next

--------------------------------------------------------------------------------

Chapter III Initialization of the Rubik's Cube

Rubik's Cube (Cubeclass) constructor: Six-side color standard: top-white, bottom-yellow, left-orange, right-red, front-green, after-blue

' <summary> '
create a new Rubik's Cube
' </summary> '
<param name= ' Nrank ' > The specified order </param > Public
Sub New (ByVal Nrank as Integer)
Dim Colorarr () as Color = {color.white, color.yellow, Color.orange, Co Lor. Red, Color.green, color.blue} for
i = 0 to 5
surfacearray (i) = New Cubesurfaceclass (Nrank, I, Colorarr (i))
Ne XT
Cuberank = Nrank
initsurface ()

Magic Aspect (Cubesurfaceclass) constructor:

' <summary> '
the order of the current Rubik's Cube '
</summary> public cuberank as
Integer '
<summary> '
the cube's surface data '
</summary> public
blockdata (,) as Cubeblockclass
' ' <summary>
' Adjacent surface layer (top, bottom, left, right, front and rear)
' </summary> public
neiboursurface (5) as Cubesurfaceclass public
Index As Integer public
Sub New (nrank As Integer, nindex as Integer, Ncolor as Color)
ReDim blockdata (nRank-1, nRank-1)
Cuberank = Nrank
Index = nindex
Dim rnd as New Random for
i = 0 to nRank-1 for
j = 0 to NRank-1 Blockdata (i,
j) = New Cubeblockclass (i, J) Blockdata (i, J)
. Parentindex = Index
Blockdata (i, J). Blockcolor = Ncolor
Next
Next

Constructor for Rubik's Cube Block (cubeblockclass):

Public Parentindex as Integer '
<summary> ' '
current block color '
</summary> public
Blockcolor as  The
number of rows public y As Integer ' containing the number of columns public x As Integer ' is public
Sub New (NX As Integer, NY as Integer)
x = NX
y = NY

At this point, an arbitrary order of the Rubik's Cube can be expressed, and on this basis will be able to achieve the twisting logic of the Rubik's Cube.

The above mentioned is for. NET to achieve the Rubik's Cube Game (a) the expression of any order of the Rubik's Cube, I hope to help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.