webForumDet fria alternativet

Trubbel med tärningsfunktion

.NET

4 svar · 312 visningar · startad av monAmour

Medlem sedan dec. 2002258 inlägg
Frågan#1

Halloj!
Håller i min VB-kurs på Malmöhögskola på att programmera en tärningsfunktion.
De hela bygger egentligen på två tärningar vars värden slumpas fram med hjälp av Rnd().

Användaren av programmet ska också kunna ange hur många gånger de två tärningarna ska kastas och därefter få ett svar på hur många gånger värdena på de två tärningarna blev likadana och hur många gånger de fått dubbel sexor.

Mitt program genererar dessvärre 0 i de båda resultat rutorna hela tiden :(
Så här ser min kod ut:

Option Explicit 

' Read indata 
Function readData(ByVal intAmount As Integer) As Boolean 
    Dim txtRawValue As String 
    Dim blnOkey As Boolean 
     
    txtRawValue = txtDiceAmount.Text 
    If IsNumeric(txtRawValue) Then 
        intAmount = Int(txtRawValue) 
        blnOkey = True 
    Else 
        MsgBox "Du måste ange ett tal i siffror!" 
        txtDiceAmount.SetFocus 
        txtDiceAmount.SelStart = 0 
        txtDiceAmount.SelLength = Len(txtDiceAmount.Text) 
         
        blnOkey = False 
    End If 
     
    readData = blnOkey 
End Function 

' Hit the dice! 
Function hitDice(ByVal rangeStart As Integer, ByVal rangeEnd As Integer) As Integer 
    Dim rndAmount As Integer 
    rndAmount = Int(Rnd * (rangeStart - rangeEnd + 1) + rangeEnd) 
     
    hitDice = rndAmount 
End Function 

' Get dice results 
Function getResults(ByVal int6 As Integer, ByVal intSame As Integer, ByRef intAmount As Integer) As Boolean 
    Dim rndNumber1 As Integer 
    Dim rndNumber2 As Integer 
    Dim i As Integer 
    Dim tmp6 As Integer 
    Dim tmpSame As Integer 
     
    i = 1 
    tmp6 = 0 
    tmpSame = 0 
     
    Do While i <= intAmount 
        rndNumber1 = hitDice(1, 6) 
        rndNumber2 = hitDice(1, 6) 
         
        ' Check Amounts 
        If rndNumber1 = rndNumber2 Then 
             
            ' Same amount 
            If tmpSame = 0 Then 
                tmpSame = 1 
            Else 
                tmpSame = tmpSame + 1 
            End If 
             
            ' Both 6? 
            If rndNumber1 = 6 And rndNumber2 = 6 Then 
                 
                ' Both 6, count 
                If tmp6 = 0 Then 
                    tmp6 = 1 
                Else 
                    tmp6 = tmp6 + 1 
                End If 
                 
            End If 
        End If 
                 
        i = i + 1 
    Loop 
     
    int6 = tmp6 
    intSame = tmpSame 
     
    getResults = True 
         
End Function 

Sub displayResult(ByRef intB6 As Integer, ByRef intS As Integer) 
    lblResultSame.Caption = CStr(intS) 
    lblResult6.Caption = CStr(intB6) 
     
    ' Reset amount field 
    txtDiceAmount.Text = "" 
End Sub 

Private Sub cmdDice_Click() 
    Dim intDiceAmount As Integer 
    Dim intBoth6 As Integer 
    Dim intBothSame As Integer 
         
    ' Read indata 
    If readData(intDiceAmount) Then 
        ' Get result data 
        If getResults(intBoth6, intBothSame, intDiceAmount) Then 
            ' Display results 
            Call displayResult(intBoth6, intBothSame) 
        End If 
    End If 
End Sub 

Private Sub Form_Load() 
    Randomize 
End Sub
Medlem sedan juli 200012 978 inlägg
#2

Måste man inte ge Seed ett värde?

Medlem sedan dec. 2002258 inlägg
#3

Seed?

Medlem sedan juli 200012 978 inlägg
#4

Ja varje RND() måste väl ha ett startvärde ett så kallat frö!

Medlem sedan dec. 2002258 inlägg
#5

Det har fixat sig hade vänt lite upp och ned på Rnd funktionen :D
Här är lösningen:

Option Explicit

' Read indata
Function readData(ByRef intAmount As Integer) As Boolean
    Dim txtRawValue As String
    Dim blnOkey As Boolean
    
    txtRawValue = txtDiceAmount.Text
    If IsNumeric(txtRawValue) Then
        intAmount = Int(txtRawValue)
        blnOkey = True
    Else
        MsgBox "Du måste ange ett tal i siffror!"
        txtDiceAmount.SetFocus
        txtDiceAmount.SelStart = 0
        txtDiceAmount.SelLength = Len(txtDiceAmount.Text)
        
        blnOkey = False
    End If
    
    readData = blnOkey
End Function

' Hit the dice!
Function hitDice(ByVal rangeStart As Integer, ByVal rangeEnd As Integer) As Integer
    Dim rndAmount As Integer
    rndAmount = Int(Rnd * (rangeEnd - rangeStart + 1) + rangeEnd)
    
    hitDice = rndAmount
End Function

' Get dice results
Function getResults(ByRef int6 As Integer, ByRef intSame As Integer, ByRef intAmount As Integer) As Boolean
    Dim rndNumber1 As Integer
    Dim rndNumber2 As Integer
    Dim i As Integer
    Dim tmp6 As Integer
    Dim tmpSame As Integer
    
    i = 1
    tmp6 = 0
    tmpSame = 0
    
    Do While i <= intAmount
        rndNumber1 = hitDice(1, 6)
        rndNumber2 = hitDice(1, 6)
        
        ' Check Amounts
        If rndNumber1 = rndNumber2 Then
            
            ' Same amount
            If tmpSame = 0 Then
                tmpSame = 1
            Else
                tmpSame = tmpSame + 1
            End If
            
            ' Both 6?
            If rndNumber1 = 6 And rndNumber2 = 6 Then
                
                ' Both 6, count
                If tmp6 = 0 Then
                    tmp6 = 1
                Else
                    tmp6 = tmp6 + 1
                End If
                
            End If
        End If
                
        i = i + 1
    Loop
    
    int6 = tmp6
    intSame = tmpSame
    
    getResults = True
        
End Function

' Function that displays the results
Sub displayResult(ByRef intB6 As Integer, ByRef intS As Integer)
    lblResultSame.Caption = CStr(intS)
    lblResult6.Caption = CStr(intB6)
    
    ' Reset amount field
    txtDiceAmount.Text = ""
End Sub

' Command button - put it all together
Private Sub cmdDice_Click()
    Dim intDiceAmount As Integer
    Dim intBoth6 As Integer
    Dim intBothSame As Integer
        
    ' Read indata
    If readData(intDiceAmount) Then
        ' Get result data
        If getResults(intBoth6, intBothSame, intDiceAmount) Then
            ' Display results
            Call displayResult(intBoth6, intBothSame)
        End If
    End If
End Sub

' Start randomize function when form loads
Private Sub Form_Load()
    Randomize
End Sub
257 ms totalt · 4 externa anrop · v20260731065814-full.6fe65c25
121 ms — deklarationer (db)
0 ms — hämta statistik (cache)
133 ms — hämta tråd, inlägg och bilagor (db)
121 ms — ändringar (db)