Structure of a rubik's cube

0

I'm trying to make a rubik cube in vb and for some reason, in Rubik class, every time I add a new face all the previous ones are made the same color.

Public Class Form1
    Dim rkCube As New Rubik
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        rkCube.start()
    End Sub

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim x As Integer,
            y As Integer
        For l = 1 To 3
            For c = 1 To 3
                x = c * 50 - 50 + 1
                y = l * 50 - 50 + 1
                rubikBox.CreateGraphics().FillRectangle(New SolidBrush(rkCube.cube(0)(0)(0)), x, y, 49, 49)
            Next
        Next
    End Sub
End Class

Public Class Rubik
    Public _color As Color,
        line As New List(Of Color),
        face As New List(Of List(Of Color)),
        cube As New List(Of List(Of List(Of Color)))

    Function start()
        For f = 1 To 6 'faces
            face.Clear()
            For l = 1 To 3 'lines
                Select Case f
                    Case 1
                        _color = Color.Red
                    Case 2
                        _color = Color.Blue
                    Case 3
                        _color = Color.Orange
                    Case 4
                        _color = Color.Green
                    Case 5
                        _color = Color.White
                    Case 6
                        _color = Color.Yellow
                End Select
                line.Clear()
                For c = 1 To 3 'color
                    line.Add(_color)
                Next
                face.Add(line)
            Next
            cube.Add(face)
        Next
    End Function
End Class
    
asked by JoanTheGamer MC 26.12.2017 в 02:32
source

1 answer

0

The problem is that, in all your iterations, you always work with the same instance of line and face .

What you do not seem to understand is that when you do:

face.Add(line)

... you are not adding a copy of line to the list face . You are actually adding a reference to the instance face . So when then, in the next iteration, you do:

line.Clear()

... this not only clears the content of line , but it erases the content of what you just added to face , because in reality, both refer to the same content / object. And then when you continue adding to the list with:

face.Add(line)

... again, what you add is not a copy. So in the end, the list is filled with references to the same instance line .

And the same problem is in the way you add the instance face to the list cube . You always add a reference to the same instance face .

That's why, in the end, all your faces look the same, because in reality you never created several faces. You just modified the same face several times, and what you see is your last modification.

The solution is that, at each iteration, instead of just doing objeto.Clear() , you really create a new instance with keyword New .

Here's an example, where I also reduce your use of global variables. There are more improvements you can make, but it's just so you can see the point, that you have to create new instances for each iteration:

Public Class Rubik
   Public cube As New List(Of List(Of List(Of Color)))

   Sub start()
      For f = 1 To 6 'faces
         Dim face As New List(Of List(Of Color))    ' Crear nueva instancia
         For l = 1 To 3 'lines
            Dim _color As Color
            Select Case f
               Case 1
                  _color = Color.Red
               Case 2
                  _color = Color.Blue
               Case 3
                  _color = Color.Orange
               Case 4
                  _color = Color.Green
               Case 5
                  _color = Color.White
               Case 6
                  _color = Color.Yellow
            End Select
            Dim line As New List(Of Color)         ' Crear nueva instancia
            For c = 1 To 3 'color
               line.Add(_color)
            Next
            face.Add(line)
         Next
         cube.Add(face)
      Next
   End Sub
End Class
    
answered by 26.12.2017 в 04:01