Click here to Skip to main content
15,896,915 members
Articles / Desktop Programming / WPF

WPF: Rubik's Cube

Rate me:
Please Sign up or sign in to vote.
4.97/5 (112 votes)
24 Sep 2014CPOL3 min read 175K   10.2K   154  
A WPF 3D Rubik's Cube application
Public Class Rotater
    Private cubePieces As List(Of CubePiece)
    Private mainWin As MainWindow

    Public Sub New(ByRef win As MainWindow)
        mainWin = win
        cubePieces = New List(Of CubePiece)
        CreatePiecesList()
    End Sub

    ''' <summary>
    ''' Layers viewed from U-to-D. 
    ''' CubePieceLocation is the initial location of a cube piece in 3D space with White-U; Green-F; Red-R.
    ''' </summary>
    Private Sub CreatePiecesList()
        ' U layer.
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.FUL, .piece = mainWin.WGO_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.FU, .piece = mainWin.WG_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.FUR, .piece = mainWin.WGR_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.RU, .piece = mainWin.WR_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.BUR, .piece = mainWin.WRB_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.BU, .piece = mainWin.WB_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.BUL, .piece = mainWin.WBO_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.LU, .piece = mainWin.WO_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.UC, .piece = mainWin.WC_Cubie})
        ' E layer.
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.FL, .piece = mainWin.GO_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.FC, .piece = mainWin.GC_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.FR, .piece = mainWin.GR_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.RC, .piece = mainWin.RC_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.BR, .piece = mainWin.RB_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.BC, .piece = mainWin.BC_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.BL, .piece = mainWin.BO_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.LC, .piece = mainWin.OC_Cubie})
        ' D layer.
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.FDL, .piece = mainWin.YGO_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.FD, .piece = mainWin.YG_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.FDR, .piece = mainWin.YGR_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.RD, .piece = mainWin.YR_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.BDR, .piece = mainWin.YRB_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.BD, .piece = mainWin.YB_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.BDL, .piece = mainWin.YBO_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.LD, .piece = mainWin.YO_Cubie})
        cubePieces.Add(New CubePiece With {.PieceLocation = PieceLocations.DC, .piece = mainWin.YC_Cubie})
    End Sub

#Region "Rotation of layers around Y-axis"

    Public Sub Rotate_U_Layer(ByVal angle As Double)
        Dim pieces = cubePieces.Where(Function(c) c.PieceLocation = PieceLocations.FUL Or c.PieceLocation = PieceLocations.FU _
                                                Or c.PieceLocation = PieceLocations.FUR Or c.PieceLocation = PieceLocations.RU _
                                                Or c.PieceLocation = PieceLocations.BUR Or c.PieceLocation = PieceLocations.BU _
                                                Or c.PieceLocation = PieceLocations.BUL Or c.PieceLocation = PieceLocations.LU _
                                                Or c.PieceLocation = PieceLocations.UC)
        For Each piece In pieces
            piece.RotateAround_Y_axis(angle)
        Next
    End Sub

    Public Sub Rotate_E_Layer(ByVal angle As Double)
        Dim pieces = cubePieces.Where(Function(c) c.PieceLocation = PieceLocations.FL Or c.PieceLocation = PieceLocations.FC _
                                                Or c.PieceLocation = PieceLocations.FR Or c.PieceLocation = PieceLocations.RC _
                                                Or c.PieceLocation = PieceLocations.BR Or c.PieceLocation = PieceLocations.BC _
                                                Or c.PieceLocation = PieceLocations.BL Or c.PieceLocation = PieceLocations.LC)
        For Each piece In pieces
            piece.RotateAround_Y_axis(angle)
        Next
    End Sub

    Public Sub Rotate_D_Layer(ByVal angle As Double)
        Dim pieces = cubePieces.Where(Function(c) c.PieceLocation = PieceLocations.FDL Or c.PieceLocation = PieceLocations.FD _
                                          Or c.PieceLocation = PieceLocations.FDR Or c.PieceLocation = PieceLocations.RD _
                                          Or c.PieceLocation = PieceLocations.BDR Or c.PieceLocation = PieceLocations.BD _
                                          Or c.PieceLocation = PieceLocations.BDL Or c.PieceLocation = PieceLocations.LD _
                                          Or c.PieceLocation = PieceLocations.DC)
        For Each piece In pieces
            piece.RotateAround_Y_axis(angle)
        Next
    End Sub

#End Region

#Region "Rotation of layers around X-axis"

    Public Sub Rotate_R_Layer(ByVal angle As Double)
        Dim pieces = cubePieces.Where(Function(c) c.PieceLocation = PieceLocations.FUR Or c.PieceLocation = PieceLocations.RU _
                                                Or c.PieceLocation = PieceLocations.BUR Or c.PieceLocation = PieceLocations.BR _
                                                Or c.PieceLocation = PieceLocations.BDR Or c.PieceLocation = PieceLocations.RD _
                                                Or c.PieceLocation = PieceLocations.FDR Or c.PieceLocation = PieceLocations.FR _
                                                Or c.PieceLocation = PieceLocations.RC)
        For Each piece In pieces
            piece.RotateAround_X_axis(angle)
        Next
    End Sub

    Public Sub Rotate_M_Layer(ByVal angle As Double)
        Dim pieces = cubePieces.Where(Function(c) c.PieceLocation = PieceLocations.FU Or c.PieceLocation = PieceLocations.UC _
                                                Or c.PieceLocation = PieceLocations.BU Or c.PieceLocation = PieceLocations.BC _
                                                Or c.PieceLocation = PieceLocations.BD Or c.PieceLocation = PieceLocations.DC _
                                                Or c.PieceLocation = PieceLocations.FD Or c.PieceLocation = PieceLocations.FC)
        For Each piece In pieces
            piece.RotateAround_X_axis(angle)
        Next
    End Sub

    Public Sub Rotate_L_Layer(ByVal angle As Double)
        Dim pieces = cubePieces.Where(Function(c) c.PieceLocation = PieceLocations.FUL Or c.PieceLocation = PieceLocations.LU _
                                          Or c.PieceLocation = PieceLocations.BUL Or c.PieceLocation = PieceLocations.BL _
                                          Or c.PieceLocation = PieceLocations.BDL Or c.PieceLocation = PieceLocations.LD _
                                          Or c.PieceLocation = PieceLocations.FDL Or c.PieceLocation = PieceLocations.FL _
                                          Or c.PieceLocation = PieceLocations.LC)
        For Each piece In pieces
            piece.RotateAround_X_axis(angle)
        Next
    End Sub

#End Region

#Region "Rotation of layers around Z-axis"

    Public Sub Rotate_B_Layer(ByVal angle As Double)
        Dim pieces = cubePieces.Where(Function(c) c.PieceLocation = PieceLocations.BUL Or c.PieceLocation = PieceLocations.BU _
                                                Or c.PieceLocation = PieceLocations.BUR Or c.PieceLocation = PieceLocations.BR _
                                                Or c.PieceLocation = PieceLocations.BDR Or c.PieceLocation = PieceLocations.BD _
                                                Or c.PieceLocation = PieceLocations.BDL Or c.PieceLocation = PieceLocations.BL _
                                                Or c.PieceLocation = PieceLocations.BC)
        For Each piece In pieces
            piece.RotateAround_Z_axis(angle)
        Next
    End Sub

    Public Sub Rotate_S_Layer(ByVal angle As Double)
        Dim pieces = cubePieces.Where(Function(c) c.PieceLocation = PieceLocations.LU Or c.PieceLocation = PieceLocations.UC _
                                                Or c.PieceLocation = PieceLocations.RU Or c.PieceLocation = PieceLocations.RC _
                                                Or c.PieceLocation = PieceLocations.RD Or c.PieceLocation = PieceLocations.DC _
                                                Or c.PieceLocation = PieceLocations.LD Or c.PieceLocation = PieceLocations.LC)
        For Each piece In pieces
            piece.RotateAround_Z_axis(angle)
        Next
    End Sub

    Public Sub Rotate_F_Layer(ByVal angle As Double)
        Dim pieces = cubePieces.Where(Function(c) c.PieceLocation = PieceLocations.FUL Or c.PieceLocation = PieceLocations.FU _
                                          Or c.PieceLocation = PieceLocations.FUR Or c.PieceLocation = PieceLocations.FR _
                                          Or c.PieceLocation = PieceLocations.FDR Or c.PieceLocation = PieceLocations.FD _
                                          Or c.PieceLocation = PieceLocations.FDL Or c.PieceLocation = PieceLocations.FL _
                                          Or c.PieceLocation = PieceLocations.FC)
        For Each piece In pieces
            piece.RotateAround_Z_axis(angle)
        Next
    End Sub

#End Region

    Public Sub RotateCubeAround_X_axis(ByVal angle As Double)
        For Each piece In cubePieces
            piece.RotateAround_X_axis(angle)
        Next
    End Sub

    Public Sub RotateCubeAround_Y_axis(ByVal angle As Double)
        For Each piece In cubePieces
            piece.RotateAround_Y_axis(angle)
        Next
    End Sub

    Public Sub RotateCubeAround_Z_axis(ByVal angle As Double)
        For Each piece In cubePieces
            piece.RotateAround_Z_axis(angle)
        Next
    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Kenya Kenya
Experienced C# software developer with a passion for WPF.

Awards,
  • CodeProject MVP 2013
  • CodeProject MVP 2012
  • CodeProject MVP 2021

Comments and Discussions