views:

143

answers:

2

I have a simple array created for a Die roller. I want to roll 5 six sided dice, and remove the lowest 2 values. What code would help me do this. here is my basic code for the 5 dice

Public Partial Class MainForm Public Sub New()

    Me.InitializeComponent()

End Sub

Sub Button1Click(sender As Object, e As EventArgs)
    Dim d61 as Integer
    Dim d62 As Integer
    Dim d63 As Integer
    Dim d64 As Integer
    Dim d65 As Integer

    d61 = Int((6 - 1 + 1) * Rnd) + 1
    d62 = Int((6 - 1 + 1) * Rnd) + 1
    d63 = Int((6 - 1 + 1) * Rnd) + 1
    d64 = Int((6 - 1 + 1) * Rnd) + 1
    d65 = Int((6 - 1 + 1) * Rnd) + 1

    Dim Dicerolls(4) As Integer
        Dicerolls(0) = d61
        Dicerolls(1) = d62
        Dicerolls(2) = d63
        Dicerolls(3) = d64
        Dicerolls(4) = d65
+2  A: 

you could just sort the array and remove the first two elements.

RedDeckWins
If i sort it then does it rearrange the array spots or just the values?if the numbers remain the same then i can just drop the Dicerolls(0) and Dicerolls (1)
Jedin
Jedin, it's pretty obvious you are new to programming. You should try reading your textbooks and thinking about the question you just asked and see if you can figure it out yourself. Maybe try experimenting with printline if you can't reason it out.
RedDeckWins
+1  A: 

Here's code that uses generic lists to do the job.

        Imports System.Collections.Generic
        Public Function GenerateRolls() As List(Of Integer)
            Dim diceCount As Integer = 5
            Dim rolls As List(Of Integer) = New List(Of Integer)

            Randomize() 'This will randomize your numbers'
            For i As Integer = 0 To diceCount
                rolls.Add(CInt(6 * Rnd()) + 1)
            Next

            rolls.Sort() 'sorts the array in ascending order.'

            'removes the two lowest rolls'
            rolls.RemoveAt(0)
            rolls.RemoveAt(0)

            'Write out all rolls to console'
            For i As Integer = 0 To rolls.Count - 1
                Console.WriteLine(rolls(i).ToString())
            Next

            Return rolls
        End Function
Andrew M.
Perfect thank you! thats what I have been looking for. You rock man!
Jedin