Arrays duplicating themselves?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1384 Views - Last Post: 07 June 2014 - 01:41 AM Rate Topic: -----

#1 [email protected]   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 13-May 14

Arrays duplicating themselves?

Posted 06 June 2014 - 10:06 AM

Got 2 arrays.

Public Class Form1
    '(...)
    Dim TileState(3,3) As Integer
    Dim UndoTileState(3,3) As Integer
    '(...)
End Class


So if I set one array to another, like
UndoTileState = TileState
or
TileState = UndoTileState
then if I set for example
TileState(0,0) = 2
then compare the two arrays with an If command
If UndoTileState = TileState Then MsgBox("They are the same")
it always returns that they are the same.

What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays duplicating themselves?

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2674
  • View blog
  • Posts: 10,670
  • Joined: 03-December 12

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 10:09 AM

What are actually trying to compare? Based on what you have given, you are comparing arrays, and both are arrays.
Was This Post Helpful? 0
  • +
  • -

#3 [email protected]   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 13-May 14

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 10:20 AM

View Postastonecipher, on 06 June 2014 - 10:09 AM, said:

What are actually trying to compare? Based on what you have given, you are comparing arrays, and both are arrays.


The values inside the arrays. If at least one of the 4*4=16 values doesn't match, then the arrays don't match.
Was This Post Helpful? 0
  • +
  • -

#4 [email protected]   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 13-May 14

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 10:27 AM

I've just tried a for loop.
Dim areEqual as Boolean = True
For x = 0 to 3
    For y = 0 to 3
        If TileState(x,y) = UndoTileState(x,y) Then areEqual = False
    Next
Next
If areEqual Then MsgBox("They are the same")

This code gives the same result.
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 10:47 AM

instead of using arr() = arr2() use Array.Copy
Array.Copy(source, target, target.length)

Right now, if you were to put a breakpoint in and look, both arrays are truly the same. I'm pretty sure it is because of how they are referenced and since you set them = to each other you basically are pointing to the same array. Array.Copy should take care of that. With option strict on, you would have been told that you can't use = and to use is instead, because they are reference types.

This post has been edited by CharlieMay: 06 June 2014 - 10:50 AM

Was This Post Helpful? 1
  • +
  • -

#6 [email protected]   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 13-May 14

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 10:56 AM

Thanks. I'm currently on my tablet, so I'll try it out and report back when I get to my PC.
Was This Post Helpful? 0
  • +
  • -

#7 [email protected]   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 13-May 14

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 11:11 AM

Even when using Array.Copy, I still get the same result. I've put a breakpoint to where one of the TileState values change. Opened the Watch Window, and typed in TileState and UndoTileState. Expanded both. They both show that one of their values have been changed. They're still identical.
Posted Image
Was This Post Helpful? 0
  • +
  • -

#8 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 12:59 PM

Could you please post your updated code.
Also this peace of code doesnt make sense to me:
If TileState(x,y) = UndoTileState(x,y) Then areEqual = False

First why you are setting areEqual to false if they are equal? then you should break the loop after getting one item which is not equal, as the next check might overwrite the value...

This post has been edited by smohd: 06 June 2014 - 12:59 PM

Was This Post Helpful? 1
  • +
  • -

#9 [email protected]   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 13-May 14

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 01:26 PM

View Postsmohd, on 06 June 2014 - 12:59 PM, said:

If TileState(x,y) = UndoTileState(x,y) Then areEqual = False

Whoops. Meant to use <>, it's easy to make a mistake. Do you want the whole code (800-something lines) or just the relevant sections?
The next check wont overwrite since there is no "areEqual = True", and the declaring is outside the loop.

Reason for edit: Forgot to answer one of the questions

This post has been edited by [email protected]: 06 June 2014 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#10 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 01:31 PM

View Post[email protected], on 07 June 2014 - 02:11 AM, said:

Do you want the whole code (800-something lines) or just the relevant sections?

Of course only the relevant task.

View Post[email protected], on 07 June 2014 - 02:11 AM, said:

The next check wont overwrite since there is no "areEqual = True", and the declaring is outside the loop.

opps sorry you are right, but by the way the break makes more sense as to reduce the execution of unnecessary loops.
Was This Post Helpful? 0
  • +
  • -

#11 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2674
  • View blog
  • Posts: 10,670
  • Joined: 03-December 12

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 01:43 PM

I don't know the values the arrays are holding, but would sorting the arrays matter? Then, your search would be more efficient. You would just break out when the values aren't equal.
Was This Post Helpful? 0
  • +
  • -

#12 [email protected]   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 13-May 14

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 01:45 PM

View Postsmohd, on 06 June 2014 - 01:31 PM, said:

opps sorry you are right, but by the way the break makes more sense as to reduce the execution of unnecessary loops.


Well executing an additional millisecond won't kill you. :D/>

Here's the code:
Public Class Form1
    Dim BGColor As Color = Color.FromArgb(250, 248, 239)
    Dim WinColor As Color = Color.FromArgb(243, 221, 143)
    Dim GridPos(3, 3) As Point
    Dim TileState(3, 3) As Integer
    Dim UndoTileState(3, 3) As Integer
    'Dim TileSize As Size = New Size(102, 102)
    Dim playing As Boolean = False
    Dim bestChanged As Boolean = False
    Dim score As Integer
    Dim best As Single
    Dim GameMode As String = "Classic"
    Dim winEnvironment As Boolean = False
    Dim gameAlreadyWon As Boolean = False

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call fillGridPos()
        Call setTilesPos()
        Me.BackColor = BGColor
        Grid.Image = New Bitmap("data/gui/grid.png")
        Grid.BackColor = BGColor
        Banner.Image = New Bitmap("data/gui/banner.png")
        Banner.BackColor = BGColor
        ScoreBox.Image = New Bitmap("data/gui/score.png")
        ScoreBox.BackColor = BGColor
        BestBox.Image = New Bitmap("data/gui/best.png")
        BestBox.BackColor = BGColor
        MenuButton.Image = New Bitmap("data/gui/menu.png")
        Call clearAllTiles()
        Call loadBestScore()
        Randomize()
    End Sub

    Private Sub fillGridPos...

    Private Sub loadBestScore...

    Private Sub createScore...

    Private Sub saveBestScore...

    Private Sub setTilesPos...

    Private Sub clearAllTiles()
        For x = 0 To 3
            For y = 0 To 3
                Dim TileToClear As Object = generateTileObject(x, y)
                TileToClear.Image = New Bitmap("data/tiles/tile_0.png")
                TileState(x, y) = 0
            Next y
        Next x
        Array.Copy(TileState, UndoTileState, UndoTileState.Length)
    End Sub

    Private Function generateTileObject(ByVal TileX, ByVal TileY)
        If TileX = 0 And TileY = 0 Then Return Tile00
        If TileX = 0 And TileY = 1 Then Return Tile01
        If TileX = 0 And TileY = 2 Then Return Tile02
        If TileX = 0 And TileY = 3 Then Return Tile03

        If TileX = 1 And TileY = 0 Then Return Tile10
        If TileX = 1 And TileY = 1 Then Return Tile11
        If TileX = 1 And TileY = 2 Then Return Tile12
        If TileX = 1 And TileY = 3 Then Return Tile13

        If TileX = 2 And TileY = 0 Then Return Tile20
        If TileX = 2 And TileY = 1 Then Return Tile21
        If TileX = 2 And TileY = 2 Then Return Tile22
        If TileX = 2 And TileY = 3 Then Return Tile23

        If TileX = 3 And TileY = 0 Then Return Tile30
        If TileX = 3 And TileY = 1 Then Return Tile31
        If TileX = 3 And TileY = 2 Then Return Tile32
        If TileX = 3 And TileY = 3 Then Return Tile33

        Return generateTileObject(TileX, TileY)
    End Function

    Private Sub NewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameButton.Click
        If playing And sender IsNot Nothing Then
            If MsgBox("Are you sure you want to start a new game? You will lose the current game.", MsgBoxStyle.YesNo, "Warning") = MsgBoxResult.Yes Then
                playing = False
            Else
                Exit Sub
            End If
        End If
        If GameMode = "Classic" Or GameMode = "Practice" Then
            Banner.Image = New Bitmap("data/gui/banner.png")
            Me.BackColor = BGColor
            Grid.BackColor = BGColor
            Banner.BackColor = BGColor
            ScoreBox.BackColor = BGColor
            BestBox.BackColor = BGColor
            NewGameButton.BackgroundImage = Nothing
            NewGameButton.Image = New Bitmap("data/gui/newgame.png")
            playing = False
            KeepGoingButton.Visible = False
            winEnvironment = False
            bestChanged = False
            score = 0
            ScoreText.Text = score
            Call clearAllTiles()
firsttilesgen:
            Dim Tile1 As System.Object = generateTileObject(CInt(Rnd() * 3), CInt(Rnd() * 3))
            Dim Tile2 As System.Object = generateTileObject(CInt(Rnd() * 3), CInt(Rnd() * 3))
            If Tile1 Is Tile2 Then GoTo firsttilesgen
            Dim Tile1Is4 As Boolean = random4()
            Dim TileX As Single = Microsoft.VisualBasic.Mid(Tile1.Name.ToString(), 5, 1)
            Dim TileY As Single = Microsoft.VisualBasic.Mid(Tile1.Name.ToString(), 6, 1)
            If Tile1Is4 = True Then
                Tile1.Image = New Bitmap("data/tiles/tile_4.png")
                TileState(TileX, TileY) = 4
            Else
                Tile1.Image = New Bitmap("data/tiles/tile_2.png")
                TileState(TileX, TileY) = 2
            End If
            Dim Tile2Is4 As Boolean = random4()
            TileX = Microsoft.VisualBasic.Mid(Tile2.Name.ToString(), 5, 1)
            TileY = Microsoft.VisualBasic.Mid(Tile2.Name.ToString(), 6, 1)
            If Tile2Is4 = True Then
                Tile2.Image = New Bitmap("data/tiles/tile_4.png")
                TileState(TileX, TileY) = 4
            Else
                Tile2.Image = New Bitmap("data/tiles/tile_2.png")
                TileState(TileX, TileY) = 2
            End If
            playing = True
            Time.Text = ""
            SurvivalTimer.Enabled = False
            gameAlreadyWon = False
        End If
        '(...)
        If GameMode = "Practice" Then
            Call saveUndo()
        End If
    End Sub

    Private Function random4...
    Private Sub generateNewTile...

    Private Sub Form1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Me.PreviewKeyDown
        Select Case (e.KeyCode)
            Case Keys.Down, Keys.Up, Keys.Left, Keys.Right
                e.IsInputKey = True
        End Select
    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If playing Then
            If movePossible() Then
                Select Case (e.KeyCode)
                    Case Keys.Left
                        Dim MoveMade As Boolean = False
                        Call saveUndo()
                        '(...)

                    Case Keys.Up
                        Dim MoveMade As Boolean = False
                        Call saveUndo()
                        '(...)

                    Case Keys.Right
                        Dim MoveMade As Boolean = False
                        Call saveUndo()
                        '(...)

                    Case Keys.Down
                        Dim MoveMade As Boolean = False
                        Call saveUndo()
                        '(...)
                End Select
                '(...)
            Else
                '(...)
            End If
        End If
    End Sub

    Private Function mergeTilesLeft...

    Private Function mergeTilesRight...

    Private Function mergeTilesUp...

    Private Function mergeTilesDown...

    Private Function movePossible()
        For y = 0 To 3
            For x = 0 To 2
                If TileState(x, y) = 0 Then
                    For k = x + 1 To 3
                        If TileState(k, y) <> 0 Then
                            Return True
                        End If
                    Next k
                End If
            Next x
        Next y

        For x = 0 To 3
            For y = 0 To 2
                If TileState(x, y) = 0 Then
                    For k = y + 1 To 3
                        If TileState(x, k) <> 0 Then
                            Return True
                        End If
                    Next k
                End If
            Next y
        Next x

        For y = 0 To 3
            For x = 3 To 1 Step -1
                If TileState(x, y) = 0 Then
                    For k = x - 1 To 0 Step -1
                        If TileState(k, y) <> 0 Then
                            Return True
                        End If
                    Next k
                End If
            Next x
        Next y

        For x = 0 To 3
            For y = 3 To 1 Step -1
                If TileState(x, y) = 0 Then
                    For k = y - 1 To 0 Step -1
                        If TileState(x, k) <> 0 Then
                            Return True
                        End If
                    Next k
                End If
            Next y
        Next x

        For y = 0 To 3
            For x = 0 To 2
                If TileState(x, y) = TileState(x + 1, y) And TileState(x, y) <> 0 Then
                    Return True
                End If
            Next
        Next

        For y = 0 To 3
            For x = 3 To 1 Step -1
                If TileState(x, y) = TileState(x - 1, y) And TileState(x, y) <> 0 Then
                    Return True
                End If
            Next x
        Next y

        For x = 0 To 3
            For y = 0 To 2
                If TileState(x, y) = TileState(x, y + 1) And TileState(x, y) <> 0 Then
                    Return True
                End If
            Next y
        Next x

        For x = 0 To 3
            For y = 3 To 1 Step -1
                If TileState(x, y) = TileState(x, y - 1) And TileState(x, y) <> 0 Then
                    Return True
                End If
            Next y
        Next x

        Return False
    End Function

    Private Sub scoreAdd...

    Private Function gameWon...

    Private Sub KeepGoingButton_Click...

    Private Sub MenuButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuButton.Click
        ContextMenu.Show(Me, MenuButton.Location.X + (MenuButton.Width / 2), MenuButton.Location.Y + (MenuButton.Height / 2))
    End Sub

    Private Sub DayToolStripMenuItem_Click...

    Private Sub NightToolStripMenuItem_Click...

    Private Sub XTileToolStripMenuItem_Click...

    Private Sub ClassicToolStripMenuItem_Click...

    Private Sub SurvivalToolStripMenuItem_Click...

    Private Sub SurvivalTimer_Tick...

    Private Sub PracticeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PracticeToolStripMenuItem.Click
        If playing Then
            If MsgBox("If you change mode, your current game will be discarded." & vbCrLf & "Are you sure you want to continue?", MsgBoxStyle.YesNo, "Warning") = MsgBoxResult.No Then
                Exit Sub
            End If
        End If
        GameMode = "Practice"
        Undo.Visible = True
        Time.Visible = False
        NewGame_Click(Nothing, EventArgs.Empty)
    End Sub

    Private Sub Undo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Undo.Click
        Call loadUndo()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call loadUndo()
    End Sub

    Private Sub saveUndo()
        Array.Copy(TileState, UndoTileState, UndoTileState.Length)
    End Sub

    Private Sub loadUndo()
        Array.Copy(UndoTileState, TileState, TileState.Length)
        For x = 0 To 3
            For y = 0 To 3
                Dim ApplyTile As Object = generateTileObject(x, y)
                ApplyTile.Image = New Bitmap("data/tiles/tile_" & TileState(x, y) & ".png")
            Next y
        Next x
    End Sub
End Class


Took me a while. I think these are the relevant ones. Although maybe I posted too much. If you need an screenshot of the GUI, just ask.

View Postastonecipher, on 06 June 2014 - 01:43 PM, said:

I don't know the values the arrays are holding, but would sorting the arrays matter? Then, your search would be more efficient. You would just break out when the values aren't equal.


Sorting does matter because every specific value is "attached" to its PictureBox, representing Tiles in-game. And yes this isn't some random code assignment I've given myself, I'm trying to develop a game.

EDIT: Just noticed that TileSize (ln 7 in code) is unused, so I've commented it.

This post has been edited by [email protected]: 06 June 2014 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

#13 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 02:07 PM

Just off the top of my head, wouldn't you want to copy the array before clearing the tileset. Otherwise, you're just copying 0 from your clear.
Was This Post Helpful? 0
  • +
  • -

#14 [email protected]   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 77
  • Joined: 13-May 14

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 02:15 PM

View PostCharlieMay, on 06 June 2014 - 02:07 PM, said:

Just off the top of my head, wouldn't you want to copy the array before clearing the tileset. Otherwise, you're just copying 0 from your clear.


Doing that so I don't need a separate code for clearing undo tileset.
Was This Post Helpful? 0
  • +
  • -

#15 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Arrays duplicating themselves?

Posted 06 June 2014 - 02:24 PM

right, but it appears you are setting the tileset to 0 and then copying it to the undo array which at that point would be 0 also. I would think you could copy the array using array.copy to retain the current tileset and then loop through the tile array and set them to 0 leaving the undo array with the original values.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2