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 Swipe
    Private mainWin As MainWindow

    Private Const CLOCKWISE As Double = -90
    Private Const ANTI_CLOCKWISE As Double = 90

    Public Sub New(ByRef win As MainWindow)
        mainWin = win
    End Sub
    
    Public Sub RotateLayer(ByVal p1 As Path, ByVal p2 As Path)
        Dim p1_Location As FaceLocations = FacePath.GetLocation(p1)
        Dim p2_Location As FaceLocations = FacePath.GetLocation(p2)

        If p1_Location.ToString.StartsWith("F") Then
            FrontFaceSwipeLayerRotate(p1_Location, p2_Location)
        Else
            RightFaceSwipeLayerRotate(p1_Location, p2_Location)
        End If
    End Sub

    Private Sub FrontFaceSwipeLayerRotate(ByVal pathFL1 As FaceLocations, pathFL2 As FaceLocations)
        Select Case pathFL1
            Case FaceLocations.FUL
                If pathFL2 = FaceLocations.FU OrElse pathFL2 = FaceLocations.FUR Then
                    mainWin.CubeRotater.Rotate_U_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FL OrElse pathFL2 = FaceLocations.FDL Then
                    mainWin.CubeRotater.Rotate_L_Layer(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.FU
                If pathFL2 = FaceLocations.FUL Then
                    mainWin.CubeRotater.Rotate_U_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FUR Then
                    mainWin.CubeRotater.Rotate_U_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FC OrElse pathFL2 = FaceLocations.FD Then
                    mainWin.CubeRotater.Rotate_M_Layer(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.FUR
                If pathFL2 = FaceLocations.FU OrElse pathFL2 = FaceLocations.FUL Then
                    mainWin.CubeRotater.Rotate_U_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FR OrElse pathFL2 = FaceLocations.FDR Then
                    mainWin.CubeRotater.Rotate_R_Layer(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.FL
                If pathFL2 = FaceLocations.FC OrElse pathFL2 = FaceLocations.FR Then
                    mainWin.CubeRotater.Rotate_E_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FUL Then
                    mainWin.CubeRotater.Rotate_L_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FDL Then
                    mainWin.CubeRotater.Rotate_L_Layer(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.FC
                Select Case pathFL2
                    Case FaceLocations.FU
                        mainWin.CubeRotater.Rotate_M_Layer(CLOCKWISE)
                    Case FaceLocations.FD
                        mainWin.CubeRotater.Rotate_M_Layer(ANTI_CLOCKWISE)
                    Case FaceLocations.FL
                        mainWin.CubeRotater.Rotate_E_Layer(CLOCKWISE)
                    Case FaceLocations.FR
                        mainWin.CubeRotater.Rotate_E_Layer(ANTI_CLOCKWISE)
                End Select
            Case FaceLocations.FR
                If pathFL2 = FaceLocations.FC OrElse pathFL2 = FaceLocations.FL Then
                    mainWin.CubeRotater.Rotate_E_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FUR Then
                    mainWin.CubeRotater.Rotate_R_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FDR Then
                    mainWin.CubeRotater.Rotate_R_Layer(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.FDL
                If pathFL2 = FaceLocations.FD OrElse pathFL2 = FaceLocations.FDR Then
                    mainWin.CubeRotater.Rotate_D_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FL OrElse pathFL2 = FaceLocations.FUL Then
                    mainWin.CubeRotater.Rotate_L_Layer(CLOCKWISE)
                End If
            Case FaceLocations.FD
                If pathFL2 = FaceLocations.FDL Then
                    mainWin.CubeRotater.Rotate_D_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FDR Then
                    mainWin.CubeRotater.Rotate_D_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FC OrElse pathFL2 = FaceLocations.FU Then
                    mainWin.CubeRotater.Rotate_M_Layer(CLOCKWISE)
                End If
            Case FaceLocations.FDR
                If pathFL2 = FaceLocations.FD OrElse pathFL2 = FaceLocations.FDL Then
                    mainWin.CubeRotater.Rotate_D_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FR OrElse pathFL2 = FaceLocations.FUR Then
                    mainWin.CubeRotater.Rotate_R_Layer(CLOCKWISE)
                End If
        End Select
    End Sub

    Private Sub RightFaceSwipeLayerRotate(ByVal pathFL1 As FaceLocations, pathFL2 As FaceLocations)
        Select Case pathFL1
            Case FaceLocations.RUF
                If pathFL2 = FaceLocations.RU OrElse pathFL2 = FaceLocations.RUB Then
                    mainWin.CubeRotater.Rotate_U_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RF OrElse pathFL2 = FaceLocations.RDF Then
                    mainWin.CubeRotater.Rotate_F_Layer(CLOCKWISE)
                End If
            Case FaceLocations.RU
                If pathFL2 = FaceLocations.RUF Then
                    mainWin.CubeRotater.Rotate_U_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RUB Then
                    mainWin.CubeRotater.Rotate_U_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RC OrElse pathFL2 = FaceLocations.RD Then
                    mainWin.CubeRotater.Rotate_S_Layer(CLOCKWISE)
                End If
            Case FaceLocations.RUB
                If pathFL2 = FaceLocations.RU OrElse pathFL2 = FaceLocations.RUF Then
                    mainWin.CubeRotater.Rotate_U_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RB OrElse pathFL2 = FaceLocations.RDB Then
                    mainWin.CubeRotater.Rotate_B_Layer(CLOCKWISE)
                End If
            Case FaceLocations.RF
                If pathFL2 = FaceLocations.RC OrElse pathFL2 = FaceLocations.RB Then
                    mainWin.CubeRotater.Rotate_E_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RUF Then
                    mainWin.CubeRotater.Rotate_F_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RDF Then
                    mainWin.CubeRotater.Rotate_F_Layer(CLOCKWISE)
                End If
            Case FaceLocations.RC
                Select Case pathFL2
                    Case FaceLocations.RU
                        mainWin.CubeRotater.Rotate_S_Layer(ANTI_CLOCKWISE)
                    Case FaceLocations.RD
                        mainWin.CubeRotater.Rotate_S_Layer(CLOCKWISE)
                    Case FaceLocations.RF
                        mainWin.CubeRotater.Rotate_E_Layer(CLOCKWISE)
                    Case FaceLocations.RB
                        mainWin.CubeRotater.Rotate_E_Layer(ANTI_CLOCKWISE)
                End Select
            Case FaceLocations.RB
                If pathFL2 = FaceLocations.RC OrElse pathFL2 = FaceLocations.RF Then
                    mainWin.CubeRotater.Rotate_E_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RUB Then
                    mainWin.CubeRotater.Rotate_B_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RDB Then
                    mainWin.CubeRotater.Rotate_B_Layer(CLOCKWISE)
                End If
            Case FaceLocations.RDF
                If pathFL2 = FaceLocations.RD OrElse pathFL2 = FaceLocations.RDB Then
                    mainWin.CubeRotater.Rotate_D_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RF OrElse pathFL2 = FaceLocations.RUF Then
                    mainWin.CubeRotater.Rotate_F_Layer(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.RD
                If pathFL2 = FaceLocations.RDF Then
                    mainWin.CubeRotater.Rotate_D_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RDB Then
                    mainWin.CubeRotater.Rotate_D_Layer(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RC OrElse pathFL2 = FaceLocations.RU Then
                    mainWin.CubeRotater.Rotate_S_Layer(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.RDB
                If pathFL2 = FaceLocations.RD OrElse pathFL2 = FaceLocations.RDF Then
                    mainWin.CubeRotater.Rotate_D_Layer(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RB OrElse pathFL2 = FaceLocations.RUB Then
                    mainWin.CubeRotater.Rotate_B_Layer(ANTI_CLOCKWISE)
                End If
        End Select
    End Sub

    Public Sub RotateCube(ByVal p1 As Path, ByVal p2 As Path)
        Dim p1_Location As FaceLocations = FacePath.GetLocation(p1)
        Dim p2_Location As FaceLocations = FacePath.GetLocation(p2)

        If p1_Location.ToString.StartsWith("F") Then
            FrontFaceSwipeCubeRotate(p1_Location, p2_Location)
        Else
            RightFaceSwipeCubeRotate(p1_Location, p2_Location)
        End If
    End Sub

    Private Sub FrontFaceSwipeCubeRotate(ByVal pathFL1 As FaceLocations, pathFL2 As FaceLocations)
        Select Case pathFL1
            Case FaceLocations.FUL
                If pathFL2 = FaceLocations.FU OrElse pathFL2 = FaceLocations.FUR Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FL OrElse pathFL2 = FaceLocations.FDL Then
                    mainWin.CubeRotater.RotateCubeAround_X_axis(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.FU
                If pathFL2 = FaceLocations.FUL Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FUR Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FC OrElse pathFL2 = FaceLocations.FD Then
                    mainWin.CubeRotater.RotateCubeAround_X_axis(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.FUR
                If pathFL2 = FaceLocations.FU OrElse pathFL2 = FaceLocations.FUL Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FR OrElse pathFL2 = FaceLocations.FDR Then
                    mainWin.CubeRotater.RotateCubeAround_X_axis(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.FL
                If pathFL2 = FaceLocations.FC OrElse pathFL2 = FaceLocations.FR Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FUL Then
                    mainWin.CubeRotater.RotateCubeAround_X_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FDL Then
                    mainWin.CubeRotater.RotateCubeAround_X_axis(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.FC
                Select Case pathFL2
                    Case FaceLocations.FU
                        mainWin.CubeRotater.RotateCubeAround_X_axis(CLOCKWISE)
                    Case FaceLocations.FD
                        mainWin.CubeRotater.RotateCubeAround_X_axis(ANTI_CLOCKWISE)
                    Case FaceLocations.FL
                        mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                    Case FaceLocations.FR
                        mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                End Select
            Case FaceLocations.FR
                If pathFL2 = FaceLocations.FC OrElse pathFL2 = FaceLocations.FL Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FUR Then
                    mainWin.CubeRotater.RotateCubeAround_X_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FDR Then
                    mainWin.CubeRotater.RotateCubeAround_X_axis(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.FDL
                If pathFL2 = FaceLocations.FD OrElse pathFL2 = FaceLocations.FDR Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FL OrElse pathFL2 = FaceLocations.FUL Then
                    mainWin.CubeRotater.RotateCubeAround_X_axis(CLOCKWISE)
                End If
            Case FaceLocations.FD
                If pathFL2 = FaceLocations.FDL Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FDR Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FC OrElse pathFL2 = FaceLocations.FU Then
                    mainWin.CubeRotater.RotateCubeAround_X_axis(CLOCKWISE)
                End If
            Case FaceLocations.FDR
                If pathFL2 = FaceLocations.FD OrElse pathFL2 = FaceLocations.FDL Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.FR OrElse pathFL2 = FaceLocations.FUR Then
                    mainWin.CubeRotater.RotateCubeAround_X_axis(CLOCKWISE)
                End If
        End Select
    End Sub

    Private Sub RightFaceSwipeCubeRotate(ByVal pathFL1 As FaceLocations, pathFL2 As FaceLocations)
        Select Case pathFL1
            Case FaceLocations.RUF
                If pathFL2 = FaceLocations.RU OrElse pathFL2 = FaceLocations.RUB Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RF OrElse pathFL2 = FaceLocations.RDF Then
                    mainWin.CubeRotater.RotateCubeAround_Z_axis(CLOCKWISE)
                End If
            Case FaceLocations.RU
                If pathFL2 = FaceLocations.RUF Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RUB Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RC OrElse pathFL2 = FaceLocations.RD Then
                    mainWin.CubeRotater.RotateCubeAround_Z_axis(CLOCKWISE)
                End If
            Case FaceLocations.RUB
                If pathFL2 = FaceLocations.RU OrElse pathFL2 = FaceLocations.RUF Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RB OrElse pathFL2 = FaceLocations.RDB Then
                    mainWin.CubeRotater.RotateCubeAround_Z_axis(CLOCKWISE)
                End If
            Case FaceLocations.RF
                If pathFL2 = FaceLocations.RC OrElse pathFL2 = FaceLocations.RB Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RUF Then
                    mainWin.CubeRotater.RotateCubeAround_Z_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RDF Then
                    mainWin.CubeRotater.RotateCubeAround_Z_axis(CLOCKWISE)
                End If
            Case FaceLocations.RC
                Select Case pathFL2
                    Case FaceLocations.RU
                        mainWin.CubeRotater.RotateCubeAround_Z_axis(ANTI_CLOCKWISE)
                    Case FaceLocations.RD
                        mainWin.CubeRotater.RotateCubeAround_Z_axis(CLOCKWISE)
                    Case FaceLocations.RF
                        mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                    Case FaceLocations.RB
                        mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                End Select
            Case FaceLocations.RB
                If pathFL2 = FaceLocations.RC OrElse pathFL2 = FaceLocations.RF Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RUB Then
                    mainWin.CubeRotater.RotateCubeAround_Z_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RDB Then
                    mainWin.CubeRotater.RotateCubeAround_Z_axis(CLOCKWISE)
                End If
            Case FaceLocations.RDF
                If pathFL2 = FaceLocations.RD OrElse pathFL2 = FaceLocations.RDB Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RF OrElse pathFL2 = FaceLocations.RUF Then
                    mainWin.CubeRotater.RotateCubeAround_Z_axis(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.RD
                If pathFL2 = FaceLocations.RDF Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RDB Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(ANTI_CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RC OrElse pathFL2 = FaceLocations.RU Then
                    mainWin.CubeRotater.RotateCubeAround_Z_axis(ANTI_CLOCKWISE)
                End If
            Case FaceLocations.RDB
                If pathFL2 = FaceLocations.RD OrElse pathFL2 = FaceLocations.RDF Then
                    mainWin.CubeRotater.RotateCubeAround_Y_axis(CLOCKWISE)
                ElseIf pathFL2 = FaceLocations.RB OrElse pathFL2 = FaceLocations.RUB Then
                    mainWin.CubeRotater.RotateCubeAround_Z_axis(ANTI_CLOCKWISE)
                End If
        End Select
    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