locked
Randomly display strings in an array RRS feed

  • Question

  • So I have an 8x8 array named 'grid', and I need to be able to put in 10 strings named "b" in random coordinates apart from (0,7) 

    I am really stuck on this and I have looked on the forums for other help but it has not helped. Any help? :3

    Saturday, November 5, 2016 2:01 PM

Answers

  • Is there a way to get these 'b' values displayed by using the button in a normal label array, without using a labelgrid?

    Why are you determined not to use LabelGrid?   That's what gives you access to your labels using the row and column numbers.  Without that grid you would have to refer to your labels by name, and apart from the fact that you have not provided any information about what those names are, that would need someone to sit down and write out 64 lines of code, all nearly identical.  Use the grid of labels and keep the references to your labels in the simple and convenient 'labelgrid(I,J)' format. Remember that using a 2-dimensional array is probably the point of this exercise.

    • Marked as answer by coder442 Sunday, November 6, 2016 8:09 PM
    Sunday, November 6, 2016 8:04 PM

All replies

  • Try one of the solutions:

    Dim grid(7, 7) As String
    
    Dim rnd As New Random
    Enumerable.Range(0, 8 * 8).OrderBy(Function(i) rnd.Next).Take(10).ToList.ForEach(Sub(i) grid(i \ 8, i Mod 8) = "b")
    
    For i = 0 To 7
        For j = 0 To 7
            Console.Write(If(String.IsNullOrEmpty(grid(i, j)), ".", grid(i, j)))
        Next
        Console.WriteLine()
    Next
    

    Saturday, November 5, 2016 5:59 PM
  • I have used this code and I can see where you are coming from, but it just doesn't want to display on my form. My array is displayed as a label, so would I need to change the console.writeline bit? It looks like this on the form as a label.

    [][][][][][][][]        

    [][][][][][][][]

    [][][][][][][][]

    [][][][][][][][]

    [][][][][][][][]

    [][][][][][][][]

    [][][][][][][][]

    Saturday, November 5, 2016 9:25 PM
  • Try one of the solutions:

    Dim grid(7, 7) As String
    
    Dim rnd As New Random
    Enumerable.Range(0, 8 * 8).OrderBy(Function(i) rnd.Next).Take(10).ToList.ForEach(Sub(i) grid(i \ 8, i Mod 8) = "b")
    
    For i = 0 To 7
        For j = 0 To 7
            Console.Write(If(String.IsNullOrEmpty(grid(i, j)), ".", grid(i, j)))
        Next
        Console.WriteLine()
    Next

    Nice, but may need some tweaking to meet the requirement:

    "...  in random coordinates apart from (0,7) "

    - Wayne

    Saturday, November 5, 2016 9:34 PM
  • My array is displayed as a label, so would I need to change the console.writeline bit? It looks like this on the form as a label.

    How are your labels created and named?   You can arrange your labels as a 2-dimensional array.

      Dim LabelGrid(7,7) as Label

    and put the labels into the grid in code
      LabelGrid(0,0) = Label1
      LabelGrid(0,1) = Label2

    or you can create the labels using code and add them to the grid as you create each one.

    Once you have your labels arranged in an array, copying the grid elements to the corresponding label element is a nested loop.

    Public Class Form1
    
        Dim LabelGrid(7, 7) As Label
        Dim Grid(7, 7) As String
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For I As Integer = 0 To 7
                For j As Integer = 0 To 7
                    Dim thisLabel As New Label
                    thisLabel.Size = New Point(50, 50)
                    thisLabel.Location = New Point(I * 50 + 50, j * 50 + 50)
                    thisLabel.BorderStyle = BorderStyle.Fixed3D
                    Me.Controls.Add(thisLabel)
                    LabelGrid(I, j) = thisLabel
                Next
            Next
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim Rand As New Random
            Dim Count As Integer = 0
            Do
                Dim I As Integer = Rand.Next(0, 8)
                Dim j As Integer = Rand.Next(0, 8)
                If Grid(I, j) <> "b" Then
                    If Not (I = 0 AndAlso j = 7) Then
                        Grid(I, j) = "b"
                        Count += 1
                    End If
                End If
            Loop Until Count = 8
    
            For i = 0 To 7
                For j = 0 To 7
                    LabelGrid(i, j).Text = Grid(i, j)
                Next
            Next
    
        End Sub
    End Class


    • Edited by Acamar Saturday, November 5, 2016 10:49 PM sp
    Saturday, November 5, 2016 10:48 PM
  • So I have an 8x8 array named 'grid', and I need to be able to put in 10 strings named "b" in random coordinates apart from (0,7) 

    I am really stuck on this and I have looked on the forums for other help but it has not helped. Any help? :3


    Is that some kind of extra credit for a school assignment or something?

    La vida loca

    Saturday, November 5, 2016 11:34 PM
  • Yeah it is, just really stuck and I've been trying to do this for a couple of weeks
    Sunday, November 6, 2016 10:39 AM
  • So this is how my form currently looks like:

    https://gyazo.com/ae3c54ba5e399e318c92977555488b64

    https://gyazo.com/9004c04d8a08abf421804220fef03fac

    So I just need the string "b" to be randomly allocated to an of the elements in this array apart from the bottom left. I've tried your code and I've switched some stuff around but I can't it to work. I don't need to make another grid, I am not sure that I made that clear as I already have made one. If you need some of my code I can also paste that.

    Sunday, November 6, 2016 11:07 AM
  • I've tried your code and I've switched some stuff around but I can't it to work.

    Please show exactly what you tried and describe what you mean when you say you can't get it to work.

    Sunday, November 6, 2016 11:15 AM
  • Dim Rand As New Random
            Dim Count As Integer = 0
            Do
                Dim I As Integer = Rand.Next(0, 8)
                Dim j As Integer = Rand.Next(0, 8)
                If Grid(I, j) <> "b" Then
                    If Not (I = 0 AndAlso j = 7) Then
                        Grid(I, j) = "b"
                        Count += 1
                    End If
                End If
            Loop Until Count = 8
    
            For i = 0 To 7
                For j = 0 To 7
                    Label1(i, j).Text = Grid(i, j)
                Next
            Next
    
    So since I don't need the Label grid as it just creates another grid behind my exisiting array grid, I used the part of the code that generates the random position of the string "b". Since I took out 'LabelGrid', I replaced the last line with the name of my label to make the array appear, 'Label1'
    Sunday, November 6, 2016 11:29 AM
  • Dim Rand As New Random
            Dim Count As Integer = 0
            Do
                Dim I As Integer = Rand.Next(0, 8)
                Dim j As Integer = Rand.Next(0, 8)
                If Grid(I, j) <> "b" Then
                    If Not (I = 0 AndAlso j = 7) Then
                        Grid(I, j) = "b"
                        Count += 1
                    End If
                End If
            Loop Until Count = 8
    
            For i = 0 To 7
                For j = 0 To 7
                    Label1(i, j).Text = Grid(i, j)
                Next
            Next
    So since I don't need the Label grid as it just creates another grid behind my exisiting array grid, I used the part of the code that generates the random position of the string "b". Since I took out 'LabelGrid', I replaced the last line with the name of my label to make the array appear, 'Label1'

    Well it took me a while to figure it out.

    First you only show a bit of code and not what event the code is within. I used a Button to perform the operations.

    Also using the Random class you should globally instantiate "Rand" so it doesn't malfunction by returning the same values although it still can possibly as the For/Next runs since it uses the system clock for its seed value and I don't know if that means it uses miliseconds or ticks from the clock anyhow which I suppose doesn't matter. You don't display where in your code it is instantiated but it should be global and not within the sub or function the rest of your code is in.

    I used a Label to display the arrays data. I set the Font to Lucida Console, a mono space font, and Consolas is also a mono space font and both come with Windows 7. That way the characters will space evenly when displayed as a grid.

    Within the Buttons sub I create the 8 x 8 string array every time although that is not required as the array could be globally instantiated. Then I use a For/Next with another For/Next within it to fill the string arrays indexes with a space character so when the string arrays indexed characters are displayed in the Label they space evenly and only the "b"'s are visible but the spaces space everything apart and using mono space font everything lines up in the Label. If you globally instantiate a string array then prior to doing anything with it regarding "b" I would suggest making every index equal a space character.

    So perhaps alter your code if necessary and display more of it so it can be seen where you are doing things within it.


    La vida loca

    Sunday, November 6, 2016 3:14 PM
  • Well I used a List(Of Point) to add New Points to. As a point has an x and y value. And a List(Of T) has a .Contains method to see if it already contains a type you would like to add to it.

    So in a Button click sub, after setting all indexes of the 8 x 8 array to space characters I instantiate a New List(Of Point) and For 0 To 7 add points to it creating the x and y coordinate randomly. Once those points are added to the List(Of Point) I use a For/Next for i = 0 To IndexPts.Count - 1 to set the 8 x 8 arrays indexes with a "b". Since each point in IndexPts has a 0 to 7 value for its x and y coordinate then those coordinates can be used to assign Grid(IndexPts(i).X, IndexPts(i).Y) with the value "b".

    I don't show two necessary lines of code in the below code. Also you should probably use another line for testing purposes "System.Threading.Thread.Sleep(1000)" which will slow the code down immensely. However if you don't provide the correct two lines it will allow you to exit the app by stopping the debugger in Visual Studio rather than having an infinite loop occuring. You may even want to launch Task Manager prior to running the app just in case you need to kill it using Task Manager if an infinite loop occurs.

    So what do you believe the two lines should be? Well if the List does not contain the point then it needs to contain the point. And once it does contain the point then you have to stop the loop. Do you know the correct code for stopping a do loop?

            Dim IndexPts As New List(Of Point)
            For i = 0 To 7
                If IndexPts.Count = 0 Then
                    IndexPts.Add(New Point(Rand.Next(0, 8), Rand.Next(0, 8)))
                Else
                    Do
                        Dim pt As Point = New Point(Rand.Next(0, 8), Rand.Next(0, 8))
                        If IndexPts.Contains(pt) = False Then
                            ' Two lines of code go here.
                        End If
                    Loop
                End If
            Next

    So I ended up with this.


    La vida loca

    Sunday, November 6, 2016 4:02 PM
  • Well I have only been studying this for a year, but I'm guessing you need a .length - 1 at the end of the code, to be able to prevent it from continuously trying to find a non-existing element. 
    Sunday, November 6, 2016 7:24 PM
  • Ok so I see what you have done with this code. You have made another grid to display the random 'b' values. Is there a way to get these 'b' values displayed by using the button in a normal label array, without using a labelgrid? This is just because I have already used a normal label for all the other bits of my assignment, and I need to be able to make a string 'x' to go onto the string 'b' in an element. So is there a way of making this in a normal label array?
    Sunday, November 6, 2016 7:45 PM
  • So since I don't need the Label grid as it just creates another grid behind my exisiting array grid, I used the part of the code that generates the random position of the string "b". Since I took out 'LabelGrid', I replaced the last line with the name of my label to make the array appear, 'Label1'

    You do need the label grid, as that is what enables you to reference your labels using the index notation (I,J).  Without that you need to reference each label by name, and that means one line of code for each label, instead of a simple For/Each loop.

    Sunday, November 6, 2016 7:57 PM
  • Is there a way to get these 'b' values displayed by using the button in a normal label array, without using a labelgrid?

    Why are you determined not to use LabelGrid?   That's what gives you access to your labels using the row and column numbers.  Without that grid you would have to refer to your labels by name, and apart from the fact that you have not provided any information about what those names are, that would need someone to sit down and write out 64 lines of code, all nearly identical.  Use the grid of labels and keep the references to your labels in the simple and convenient 'labelgrid(I,J)' format. Remember that using a 2-dimensional array is probably the point of this exercise.

    • Marked as answer by coder442 Sunday, November 6, 2016 8:09 PM
    Sunday, November 6, 2016 8:04 PM
  • Alright, thanks for the help I appreciate you helping me out with this as I struggled. The only problem is that I have to change all the other code that I had assigned to that one label array. It'll take me some time but I can see how this is more efficient. 
    Sunday, November 6, 2016 8:11 PM
  • I need some more help. Using this code, how can I show that when a player, labelled 'x', lands on the 'c' shows that the user gets 10 coins, and when they land on a 'b' they get 0?
    Tuesday, November 8, 2016 10:13 PM
  • I need some more help. Using this code, how can I show that when a player, labelled 'x', lands on the 'c' shows that the user gets 10 coins, and when they land on a 'b' they get 0?

    What does 'land on' mean?  Are they entering coordinates as text, or selecting something, or clicking somewhere or what?  What is a 'c'?  That hasn't been mentioned. Is it similar to a 'b'?

    As this thread is marked as answered and this seems to be a different topic, you will get better responses by starting anew thread.

    Tuesday, November 8, 2016 10:49 PM
  • Well 'c' is like the 'b' which I have declared to appear 10 times on the grid and the 'b' 5 times. The user enters the position to move across and down and where ever they mark this on the 8x8 grid, there will be an 'x'.   A 'c' refers to a chest, a 'b' refers to a bandit.
    Tuesday, November 8, 2016 10:55 PM
  • Well 'c' is like the 'b' which I have declared to appear 10 times on the grid and the 'b' 5 times. The user enters the position to move across and down and where ever they mark this on the 8x8 grid, there will be an 'x'.   A 'c' refers to a chest, a 'b' refers to a bandit.
    How does the user enter the position to move across and down?  Is this as a coordinate, or as two separate values, and is it absolute or relative to some existing position?  Is this the same as 'marking' this on the grid, or are you referring to two different procedures?
    Tuesday, November 8, 2016 11:05 PM
  • So the user enters the position like this: 

       Dim PosDown = InputBox("enter down")
            Dim PosAcross = InputBox("enter across")
            grid(PosAcross, PosDown) = "X"

    When the form loads the user is met with two input boxes. This also links in with the previous code you sent me about the label grid.

    Wednesday, November 9, 2016 8:36 AM
  • When the form loads the user is met with two input boxes. This also links in with the previous code you sent me about the label grid.

    If you convert the user input text into a pair of numeric variables (see: https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx) then you can reference your grid using the array and the index, and test the value.

    If Grid(X, Y) = "c" Then ' User has entered the coordinates of a 'c' grid element
    ' Add 10 to the user's coin count

    That assumes, of course, that the grid is tested before you update it with the "X". But if you do update it then you lose the information that the cell is a 'c'.  It's not clear whether or not that matters (does the chest diappear if the user has pocketed the coins?).   An alternative is to keep the 'c' information in the grid, but update the label:

    If Grid(X, Y) = "c" Then
        ' User has entered the coordinates of a 'c' grid element
        LabelGrid(X,Y).Text = "X"
    

    That would be OK if you change the update routine to re-enter the X each time you refresh the display:

            For i = 0 To 7
                For j = 0 To 7
                    LabelGrid(i, j).Text = Grid(i, j)
                Next
            Next
            LabelGrid(X, Y).Text = "X"

    If your grid is a numeric array instead of a string then it is possible to keep both bits of information by coding it as numbers. But the better option is to create a CELL class, so you can easily extend it with additional fields and properties for whatever bits of information you need to maintain in respect of each grid element. 

    It is not appropriate to just keep adding to a thread has already been marked as answered.  You should start a new thread for new questions.

    Wednesday, November 9, 2016 9:27 AM
  • I need some more help. Using this code, how can I show that when a player, labelled 'x', lands on the 'c' shows that the user gets 10 coins, and when they land on a 'b' they get 0?

    Well this gives the dice roller coins if the move lands on a chest. If the move lands on a pirate the dice roller loses all the coins. The player is a Joker card which moves around the grid according to a roll of the dice.

    Grid is 12x12 and allows from 1 to 72 pirates and from 1 to 72 chests.

    Option Strict On
    
    Public Class Form1
    
        Dim Die As New List(Of Bitmap)
        Dim RollDie1 As New List(Of Integer)
        Dim RollDie2 As New List(Of Integer)
    
        Dim TableLayoutPanel1 As New TableLayoutPanel
        Dim TableLayout1sPanels As New List(Of Panel())
    
        Dim Pirate As Bitmap
        Dim Chest As Bitmap
        Dim Jack As Bitmap
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Size = New Size(Screen.PrimaryScreen.WorkingArea.Height - SystemInformation.CaptionHeight - 120, Screen.PrimaryScreen.WorkingArea.Height)
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            Button1.BackColor = Color.Azure
            NumericUpDown1.Minimum = 1
            NumericUpDown1.Maximum = 72
            NumericUpDown1.Value = 5
            NumericUpDown2.Minimum = 1
            NumericUpDown2.Maximum = 72
            NumericUpDown2.Value = 20
            Button2.BackColor = Color.Azure
            Button2.Enabled = False
            PictureBox1.BackgroundImageLayout = ImageLayout.Stretch
            PictureBox2.BackgroundImageLayout = ImageLayout.Stretch
            Label1.Text = "Players Move"
            Label2.Text = "Awaiting Roll"
            Label3.Text = "Players Treasure"
            Label3.Top = Label1.Top
            Label4.Text = "0"
            Label4.TextAlign = ContentAlignment.TopCenter
            Label4.Left = CInt(Label3.Left + (Label3.Width / 2) - (Label4.Width / 2))
            Timer1.Interval = 50
            Timer2.Interval = 50
            Timer3.Interval = 400
            With CompassControl1
                .BackColor = Color.White
                .CompassStringColor = Color.DarkSlateGray
                .CompassPointerOutlineColor = Color.Black
                .CompassPointerArrowColor = Color.Lime
                .CompassPointerTailColor = Color.DarkBlue
                .CompassPointsColor = Color.DarkSlateGray
                .CompassCenterColor = Color.DarkTurquoise
                .CompassCenterOuterColor = Color.White
                .CompassRoseFirstInnerColor = Color.White
                .CompassRoseFirstOuterColor = Color.DodgerBlue
                .CompassRoseSecondInnerColor = Color.Brown
                .CompassRoseSecondOuterColor = Color.Orange
                .CompassRoseLinesColor = Color.Brown
                .CompassPoints360FalsexORx60True = True
                .CompassArrowExtender = False
            End With
            With TableLayoutPanel1
                .Size = New Size(Me.ClientRectangle.Width - 20, Me.ClientRectangle.Width - 20)
                .Location = New Point(10, 140)
                .ColumnCount = 12
                .ColumnStyles.Clear()
                .RowCount = 12
                .RowStyles.Clear()
                For i = 1 To 12
                    .ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, CSng((Me.ClientRectangle.Width - 26) / 12)))
                    .RowStyles.Add(New RowStyle(SizeType.Absolute, CSng((Me.ClientRectangle.Width - 26) / 12)))
                Next
                .CellBorderStyle = TableLayoutPanelCellBorderStyle.Single
                .BackgroundImageLayout = ImageLayout.Stretch
                .BackgroundImage = My.Resources.Forest_1
            End With
            Me.Controls.Add(TableLayoutPanel1)
            Dim CountIt As Integer = 1
            For i = 0 To 11
                Dim Temp(11) As Panel
                For j = 0 To 11
                    Dim PN As New Panel
                    With PN
                        .Size = New Size(CInt((TableLayoutPanel1.ClientRectangle.Width / 12) - 2), CInt((TableLayoutPanel1.ClientRectangle.Height / 12) - 2))
                        .BackColor = Color.Transparent
                        .BackgroundImageLayout = ImageLayout.Center
                        .Name = CountIt.ToString
                        CountIt += 1
                    End With
                    TableLayoutPanel1.Controls.Add(PN, i, j) ' Add Panels horizontally row by row
                    Temp(j) = PN
                Next
                TableLayout1sPanels.Add(Temp)
            Next
            Die.Add(My.Resources.One)
            Die.Add(My.Resources.Two)
            Die.Add(My.Resources.Three)
            Die.Add(My.Resources.Four)
            Die.Add(My.Resources.Five)
            Die.Add(My.Resources.Six)
            Pirate = New Bitmap(CInt(TableLayout1sPanels(0)(0).Width / 2), CInt(TableLayout1sPanels(0)(0).Height / 2))
            Using g As Graphics = Graphics.FromImage(Pirate)
                g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                g.DrawImage(My.Resources.Crossbones_Png, 0, 0, Pirate.Width, Pirate.Height)
            End Using
            Chest = New Bitmap(CInt(TableLayout1sPanels(0)(0).Width / 2), CInt(TableLayout1sPanels(0)(0).Height / 2))
            Using g As Graphics = Graphics.FromImage(Chest)
                g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                g.DrawImage(My.Resources.Treasure, 0, 0, Chest.Width, Chest.Height)
            End Using
            Jack = New Bitmap(23, 32)
            Using g As Graphics = Graphics.FromImage(Jack)
                g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                g.DrawImage(My.Resources.jr, 0, 0, Jack.Width, Jack.Height)
            End Using
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    
            Using p As New Pen(Brushes.DarkSlateGray, 2)
                e.Graphics.DrawLine(p, Button1.Left - 3, Button1.Top - 2, NumericUpDown1.Right + 3, Button1.Top - 2)
                e.Graphics.DrawLine(p, NumericUpDown1.Right + 2, Button1.Top - 3, NumericUpDown1.Right + 2, NumericUpDown2.Bottom + 3)
                e.Graphics.DrawLine(p, NumericUpDown1.Right + 3, NumericUpDown2.Bottom + 2, Label6.Left - 4, NumericUpDown2.Bottom + 2)
                e.Graphics.DrawLine(p, Label6.Left - 3, NumericUpDown2.Bottom + 3, Label6.Left - 3, Button1.Bottom + 2)
                e.Graphics.DrawLine(p, Label6.Left - 2, Button1.Bottom + 2, Button1.Left - 3, Button1.Bottom + 2)
                e.Graphics.DrawLine(p, Button1.Left - 2, Button1.Bottom + 3, Button1.Left - 2, Button1.Top - 3)
            End Using
    
            Using p As New Pen(Brushes.DodgerBlue, 2)
                e.Graphics.DrawLine(p, PictureBox1.Left - 2, Button2.Top - 2, PictureBox2.Right + 2, Button2.Top - 2)
                e.Graphics.DrawLine(p, PictureBox2.Right + 2, Button2.Top - 3, PictureBox2.Right + 2, PictureBox2.Bottom + 3)
                e.Graphics.DrawLine(p, PictureBox2.Right + 3, PictureBox2.Bottom + 2, PictureBox1.Left - 3, PictureBox2.Bottom + 2)
                e.Graphics.DrawLine(p, PictureBox1.Left - 3, PictureBox2.Bottom + 3, PictureBox1.Left - 3, Button2.Top - 3)
            End Using
    
            Using p As New Pen(Brushes.DarkViolet, 2)
                e.Graphics.DrawLine(p, Label1.Left - 3, Label1.Top - 2, Label3.Right + 3, Label1.Top - 2)
                e.Graphics.DrawLine(p, Label3.Right + 3, Label1.Top - 3, Label3.Right + 3, PictureBox1.Bottom + 4)
                e.Graphics.DrawLine(p, Label3.Right + 3, PictureBox1.Bottom + 3, Label1.Left - 4, PictureBox1.Bottom + 3)
                e.Graphics.DrawLine(p, Label1.Left - 4, PictureBox1.Bottom + 3, Label1.Left - 4, Label1.Top - 3)
            End Using
    
        End Sub
    
        Dim Rand As New Random
        Dim FirstRunDone As Boolean = False
        Dim PaintJack As Boolean = False
    
        Private Sub Set_Grid_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Button2.Enabled = False AndAlso FirstRunDone = True Then Exit Sub
            PaintJack = False
            FirstRunDone = True
            Label2.Text = "Awaiting Roll"
            CoinCount = 0
            Label4.Text = CoinCount.ToString
            Label4.BackColor = Color.White
            Label4.Left = CInt(Label3.Left + (Label3.Width / 2) - (Label4.Width / 2))
            Button2.Enabled = False
    
            OldPlayerLocation = New Point(0, 0)
            NewPlayerLocation = New Point(0, 0)
    
            For i = 0 To 11
                For j = 0 To 11
                    With TableLayout1sPanels(j)(i)
                        .BackgroundImage = Nothing
                        .Tag = "Nothing"
                        RemoveHandler TableLayout1sPanels(j)(i).Paint, AddressOf Panels_Paint
                        TableLayout1sPanels(j)(i).Refresh()
                    End With
                Next
            Next
    
            Dim OrderedLocations = Enumerable.Range(1, 144)
            Dim RandomLocations = OrderedLocations.OrderBy(Function(a) Guid.NewGuid())
            Dim Temp As New List(Of Integer)
            Temp = RandomLocations.ToList
    
            For h = 1 To CInt(NumericUpDown1.Value) ' Add pirates
                For i = 0 To 11
                    For j = 0 To 11
                        If TableLayout1sPanels(i)(j).Name = Temp(h - 1).ToString Then
                            TableLayout1sPanels(i)(j).BackgroundImage = Pirate
                            TableLayout1sPanels(i)(j).Tag = "Pirate"
                            TableLayout1sPanels(i)(j).Refresh()
                        End If
                    Next
                Next
            Next
    
            For h = CInt(NumericUpDown1.Value + 1) To CInt(NumericUpDown2.Value + NumericUpDown1.Value) ' Add chests
                For i = 0 To 11
                    For j = 0 To 11
                        If TableLayout1sPanels(i)(j).Name = Temp(h - 1).ToString Then
                            TableLayout1sPanels(i)(j).BackgroundImage = Chest
                            TableLayout1sPanels(i)(j).Tag = "Chest"
                            TableLayout1sPanels(i)(j).Refresh()
                        End If
                    Next
                Next
            Next
    
            Button2.Enabled = True
        End Sub
    
        Private Sub Roll_the_Dice_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Label2.Text = "Awaiting Roll"
            Label2.Refresh()
            RollDie1.Clear()
            RollDie2.Clear()
            Counter1 = 0
            Counter2 = 0
            Button2.Text = "Dice rolling"
            Button2.BackColor = Color.OrangeRed
            Button2.Enabled = False
            Timer1.Interval = 50
            Timer2.Interval = 50
    
            For i = 0 To Rand.Next(4, 7)
                Dim Die1Ints = Enumerable.Range(0, 6)
                Dim Die1Roll = Die1Ints.OrderBy(Function(a) Guid.NewGuid())
                For Each Item In Die1Roll
                    RollDie1.Add(Item)
                Next
            Next
    
            For i = 0 To Rand.Next(4, 7)
                Dim Die2Ints = Enumerable.Range(0, 6)
                Dim Die2Roll = Die2Ints.OrderBy(Function(a) Guid.NewGuid())
                For Each Item In Die2Roll
                    RollDie2.Add(Item)
                Next
            Next
    
            Timer1.Start()
            Timer2.Start()
        End Sub
    
        Dim Counter1 As Integer = 0
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Interval += 5
            PictureBox1.BackgroundImage = Die(RollDie1(Counter1))
            PictureBox1.Refresh()
            Counter1 += 1
            If Counter1 = RollDie1.Count Then
                Timer1.Stop()
                If Timer2.Enabled = False Then
                    Button2.Enabled = True
                    Button2.BackColor = Color.Azure
                    Button2.Text = "Roll the Dice"
                    MovePlayer()
                End If
            End If
        End Sub
    
        Dim Counter2 As Integer = 0
    
        Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
            Timer2.Interval += 5
            PictureBox2.BackgroundImage = Die(RollDie2(Counter2))
            PictureBox2.Refresh()
            Counter2 += 1
            If Counter2 = RollDie2.Count Then
                Timer2.Stop()
                If Timer1.Enabled = False Then
                    Button2.Enabled = True
                    Button2.BackColor = Color.Azure
                    Button2.Text = "Roll the Dice"
                    MovePlayer()
                End If
            End If
        End Sub
    
        Dim OldPlayerLocation As Point
        Dim NewPlayerLocation As Point
    
        Dim HorizontalMovement As String = ""
        Dim VerticalMovement As String = ""
    
        Dim CoinCount As Integer = 0
    
        Private Sub MovePlayer()
    
            PaintJack = False
            OldPlayerLocation = NewPlayerLocation
            StartPT = OldPlayerLocation
    
            PlayerDirectionAndLocation(RollDie1(RollDie1.Count - 1) + 1, RollDie2(RollDie2.Count - 1) + 1, NewPlayerLocation)
    
            Label2.Text = "From posit " & OldPlayerLocation.X.ToString & " " & OldPlayerLocation.Y.ToString & vbCrLf & _
                HorizontalMovement & " to " & NewPlayerLocation.X.ToString & vbCrLf & VerticalMovement & " to " & NewPlayerLocation.Y.ToString
            Label2.Refresh()
    
            If OldPlayerLocation.X <> 0 AndAlso OldPlayerLocation.Y <> 0 Then
                RemoveHandler TableLayout1sPanels(OldPlayerLocation.X - 1)(OldPlayerLocation.Y - 1).Paint, AddressOf Panels_Paint
                TableLayout1sPanels(OldPlayerLocation.X - 1)(OldPlayerLocation.Y - 1).Refresh()
                RemoveHandler TableLayout1sPanels(OldPlayerLocation.X - 1)(OldPlayerLocation.Y - 1).Paint, AddressOf Panels_Paint
            End If
    
            For i = 0 To 11
                For j = 0 To 11
                    RemoveHandler TableLayout1sPanels(i)(j).Paint, AddressOf Panels_Paint
                Next
            Next
    
            Timer3.Start()
        End Sub
    
        Private Sub PlayerDirectionAndLocation(ByRef FirstDie As Integer, ByRef SecondDie As Integer, ByRef PlayersLocal As Point)
            If PlayersLocal.X + FirstDie <= 12 AndAlso PlayersLocal.Y + SecondDie <= 12 Then
                ' Right, Down
                PlayersLocal.X += FirstDie
                HorizontalMovement = "Right"
                PlayersLocal.Y += SecondDie
                VerticalMovement = "Down"
            ElseIf PlayersLocal.X + FirstDie <= 12 AndAlso PlayersLocal.Y + SecondDie > 12 Then
                ' Right, Up
                PlayersLocal.X += FirstDie
                HorizontalMovement = "Right"
                PlayersLocal.Y -= SecondDie
                VerticalMovement = "Up"
            ElseIf PlayersLocal.X + FirstDie > 12 AndAlso PlayersLocal.Y + SecondDie <= 12 Then
                ' Left, Down
                PlayersLocal.X -= FirstDie
                HorizontalMovement = "Left"
                PlayersLocal.Y += SecondDie
                VerticalMovement = "Down"
            ElseIf PlayersLocal.X + FirstDie > 12 AndAlso PlayersLocal.Y + SecondDie > 12 Then
                ' Left, Up
                PlayersLocal.X -= FirstDie
                HorizontalMovement = "Left"
                PlayersLocal.Y -= SecondDie
                VerticalMovement = "Up"
            End If
        End Sub
    
        Private Sub Panels_Paint(sender As Object, e As PaintEventArgs)
            If PaintJack = True Then
                e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.Half
                e.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceCopy
                e.Graphics.CompositingQuality = Drawing2D.CompositingQuality.GammaCorrected
                e.Graphics.DrawImage(Jack, CInt((TableLayout1sPanels(0)(0).Width / 2) - (Jack.Width / 2)), CInt((TableLayout1sPanels(0)(0).Height / 2) - (Jack.Height / 2)))
            End If
            If PaintMove = True Then
                e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                e.Graphics.PixelOffsetMode = Drawing2D.PixelOffsetMode.Half
                e.Graphics.CompositingMode = Drawing2D.CompositingMode.SourceCopy
                e.Graphics.CompositingQuality = Drawing2D.CompositingQuality.GammaCorrected
                e.Graphics.FillEllipse(Brushes.Red, New Rectangle(10, 10, TableLayout1sPanels(StartPT.X - 1)(StartPT.Y - 1).Width - 20, TableLayout1sPanels(NewPlayerLocation.X - 1)(NewPlayerLocation.Y - 1).Height - 20))
            End If
        End Sub
    
    
        Dim StartPT As Point
        Dim OldStartPt As Point
        Dim PaintMove As Boolean = False
    
        Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
            OldStartPt = StartPT
            If StartPT.X <> NewPlayerLocation.X Then
                If StartPT.X < NewPlayerLocation.X Then
                    StartPT.X += 1
                    PaintMove = False
                    If OldStartPt.X > 0 AndAlso OldStartPt.Y > 0 Then
                        RemoveHandler TableLayout1sPanels(OldStartPt.X - 1)(OldStartPt.Y - 1).Paint, AddressOf Panels_Paint
                        TableLayout1sPanels(OldStartPt.X - 1)(OldStartPt.Y - 1).Refresh()
                    End If
                    If StartPT.X > 0 AndAlso StartPT.Y > 0 Then
                        AddHandler TableLayout1sPanels(StartPT.X - 1)(StartPT.Y - 1).Paint, AddressOf Panels_Paint
                        PaintMove = True
                        TableLayout1sPanels(StartPT.X - 1)(StartPT.Y - 1).Refresh()
                    End If
                ElseIf StartPT.X > NewPlayerLocation.X Then
                    StartPT.X -= 1
                    PaintMove = False
                    If OldStartPt.X > 0 AndAlso OldStartPt.Y > 0 Then
                        RemoveHandler TableLayout1sPanels(OldStartPt.X - 1)(OldStartPt.Y - 1).Paint, AddressOf Panels_Paint
                        TableLayout1sPanels(OldStartPt.X - 1)(OldStartPt.Y - 1).Refresh()
                    End If
                    If StartPT.X > 0 AndAlso StartPT.Y > 0 Then
                        AddHandler TableLayout1sPanels(StartPT.X - 1)(StartPT.Y - 1).Paint, AddressOf Panels_Paint
                        PaintMove = True
                        TableLayout1sPanels(StartPT.X - 1)(StartPT.Y - 1).Refresh()
                    End If
                End If
            ElseIf StartPT.X = NewPlayerLocation.X AndAlso StartPT.Y <> NewPlayerLocation.Y Then
                If StartPT.Y < NewPlayerLocation.Y Then
                    StartPT.Y += 1
                    PaintMove = False
                    If OldStartPt.X > 0 AndAlso OldStartPt.Y > 0 Then
                        RemoveHandler TableLayout1sPanels(OldStartPt.X - 1)(OldStartPt.Y - 1).Paint, AddressOf Panels_Paint
                        TableLayout1sPanels(OldStartPt.X - 1)(OldStartPt.Y - 1).Refresh()
                    End If
                    If StartPT.X > 0 AndAlso StartPT.Y > 0 Then
                        AddHandler TableLayout1sPanels(StartPT.X - 1)(StartPT.Y - 1).Paint, AddressOf Panels_Paint
                        PaintMove = True
                        TableLayout1sPanels(StartPT.X - 1)(StartPT.Y - 1).Refresh()
                    End If
                ElseIf StartPT.Y > NewPlayerLocation.Y Then
                    StartPT.Y -= 1
                    PaintMove = False
                    If OldStartPt.X > 0 AndAlso OldStartPt.Y > 0 Then
                        RemoveHandler TableLayout1sPanels(OldStartPt.X - 1)(OldStartPt.Y - 1).Paint, AddressOf Panels_Paint
                        TableLayout1sPanels(OldStartPt.X - 1)(OldStartPt.Y - 1).Refresh()
                    End If
                    If StartPT.X > 0 AndAlso StartPT.Y > 0 Then
                        AddHandler TableLayout1sPanels(StartPT.X - 1)(StartPT.Y - 1).Paint, AddressOf Panels_Paint
                        PaintMove = True
                        TableLayout1sPanels(StartPT.X - 1)(StartPT.Y - 1).Refresh()
                    End If
                End If
            ElseIf StartPT = NewPlayerLocation Then
                Timer3.Stop()
                If TableLayout1sPanels(NewPlayerLocation.X - 1)(NewPlayerLocation.Y - 1).Tag.ToString = "Pirate" Then
                    CoinCount = 0
                    Label4.Text = "You landed on a" & vbCrLf & "Pirate." & vbCrLf & "Your coin count" & vbCrLf & "> " & CoinCount.ToString & " <"
                    Label4.Left = CInt(Label3.Left + (Label3.Width / 2) - (Label4.Width / 2))
                    Label4.BackColor = Color.Red
                ElseIf TableLayout1sPanels(NewPlayerLocation.X - 1)(NewPlayerLocation.Y - 1).Tag.ToString = "Chest" Then
                    CoinCount += 10
                    Label4.Text = "You landed on a" & vbCrLf & "treasure chest." & vbCrLf & "Your coin count" & vbCrLf & "> " & CoinCount.ToString & " <"
                    Label4.Left = CInt(Label3.Left + (Label3.Width / 2) - (Label4.Width / 2))
                    Label4.BackColor = Color.Lime
                Else
                    Label4.Text = "Your coin count" & vbCrLf & "> " & CoinCount.ToString & " <"
                    Label4.Left = CInt(Label3.Left + (Label3.Width / 2) - (Label4.Width / 2))
                    Label4.BackColor = Color.White
                End If
                PaintMove = False
                TableLayout1sPanels(OldStartPt.X - 1)(OldStartPt.Y - 1).Refresh()
                RemoveHandler TableLayout1sPanels(OldStartPt.X - 1)(OldStartPt.Y - 1).Paint, AddressOf Panels_Paint
                AddHandler TableLayout1sPanels(NewPlayerLocation.X - 1)(NewPlayerLocation.Y - 1).Paint, AddressOf Panels_Paint
                PaintJack = True
                TableLayout1sPanels(NewPlayerLocation.X - 1)(NewPlayerLocation.Y - 1).Refresh()
            End If
        End Sub
    
    End Class


    La vida loca

    Thursday, November 10, 2016 2:09 AM