Quantcast
Channel: Visual Studio General Questions forum
Viewing all articles
Browse latest Browse all 21115

help with yahtzee game. ANSWERED

$
0
0

Help!!! I'm trying to make a yathzee game with visual basic 2010 and it works for the first round (time you enter a score) but the second time no matter what i try it all ways enters a 0. it's a problem with resetting the variables but i don't know what. there are 4 forms (1 start page (form3), 1 for rolling the dice(form1), 1 for the score card(form2), and 1 for showing the rules(form4)).  

How it is supposed to work:
1. click play on start page 
2. form1 (roller) and form2 (score) are loaded
3. click roll dice on form1
4. check the boxes for the dice to NOT reroll or click score card
5. (repeat lines 3 and 4, 2 more times)
6. then messagebox pops up saying you are out of rolls
7. shows score card
8. click where you want to enter the score (only "3 of a kind" and up work right now)
9. then click end turn 
10. this closes and reopens form1 (to reset it)
(repeat lines 3-10)

the (logic) error occurs at step 8 on the second time through. (figured this out with breakpoints)

Here is my Code:

~~~~~~~~~~~~~~~~~ (Form 1)~~~~~~~~~~~~~~~~

Public Class Form1
    'Note: '--------------... = section seperator

    Dim blnTurnStart As Boolean = True
    Dim blnGameStart As Boolean = False
    Dim Gen As New System.Random    'Random # maker
    Dim intRandom As Integer    'Random # Variable 
    Dim intCount As Integer     'counts # of Dice rolled
    Public intRollsLeft As Integer     '# of rolls left
    Public intDice1 As Integer
    Public intDice2 As Integer
    Public intDice3 As Integer
    Public intDice4 As Integer
    Public intDice5 As Integer
    Dim intTurnChooser As Integer
    Public blnPlayTurnEnd As Boolean = False
    Public blnRestart As Boolean = False
    Public blnScored As Boolean = False
    Public PlayArr(16) As Integer
    Public PlayArr2 As Array
    Public blnHasEnteredScore As Boolean = False

    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Array.Clear(PlayArr, 0, 16)
        intRollsLeft = 2
        blnHasEnteredScore = False
        If blnRestart = True Then
            Call ResetAll()
        End If
    End Sub
    '----------------------------------------------
    Public Sub DiceReset()          'makes all dice pics not visible again
        If chkDice1.Checked = False Then
            shpDice11.Visible = False
            shpDice12.Visible = False
            shpDice13.Visible = False
            shpDice14.Visible = False
            shpDice15.Visible = False
            shpDice16.Visible = False
        End If
        If chkDice2.Checked = False Then
            shpDice21.Visible = False
            shpDice22.Visible = False
            shpDice23.Visible = False
            shpDice24.Visible = False
            shpDice25.Visible = False
            shpDice26.Visible = False
        End If
        If chkDice3.Checked = False Then
            shpDice31.Visible = False
            shpDice32.Visible = False
            shpDice33.Visible = False
            shpDice34.Visible = False
            shpDice35.Visible = False
            shpDice36.Visible = False
        End If
        If chkDice4.Checked = False Then
            shpDice41.Visible = False
            shpDice42.Visible = False
            shpDice43.Visible = False
            shpDice44.Visible = False
            shpDice45.Visible = False
            shpDice46.Visible = False
        End If
        If chkDice5.Checked = False Then
            shpDice51.Visible = False
            shpDice52.Visible = False
            shpDice53.Visible = False
            shpDice54.Visible = False
            shpDice55.Visible = False
            shpDice56.Visible = False
        End If
    End Sub
    '----------------------------------------------
    Public Sub DiceRoller()             'rolls dice
        For intCount As Integer = 1 To 5

            If chkDice1.Checked = False Then
                If intCount = 1 Then
                    intRandom = Gen.Next(1, 6)
                    If intRandom = 1 Then
                        shpDice11.Visible = True
                        intDice1 = 1
                    ElseIf intRandom = 2 Then
                        shpDice12.Visible = True
                        intDice1 = 2
                    ElseIf intRandom = 3 Then
                        shpDice13.Visible = True
                        intDice1 = 3
                    ElseIf intRandom = 4 Then
                        shpDice14.Visible = True
                        intDice1 = 4
                    ElseIf intRandom = 5 Then
                        shpDice15.Visible = True
                        intDice1 = 5
                    ElseIf intRandom = 6 Then
                        shpDice16.Visible = True
                        intDice1 = 6
                    End If
                End If
            End If
            If chkDice2.Checked = False Then
                If intCount = 2 Then
                    intRandom = Gen.Next(1, 6)
                    If intRandom = 1 Then
                        shpDice21.Visible = True
                        intDice2 = 1
                    ElseIf intRandom = 2 Then
                        shpDice22.Visible = True
                        intDice2 = 2
                    ElseIf intRandom = 3 Then
                        shpDice23.Visible = True
                        intDice2 = 3
                    ElseIf intRandom = 4 Then
                        shpDice24.Visible = True
                        intDice2 = 4
                    ElseIf intRandom = 5 Then
                        shpDice25.Visible = True
                        intDice2 = 5
                    ElseIf intRandom = 6 Then
                        shpDice26.Visible = True
                        intDice2 = 6
                    End If
                End If
            End If
            If chkDice3.Checked = False Then
                If intCount = 3 Then
                    intRandom = Gen.Next(1, 6)
                    If intRandom = 1 Then
                        shpDice31.Visible = True
                        intDice3 = 1
                    ElseIf intRandom = 2 Then
                        shpDice32.Visible = True
                        intDice3 = 2
                    ElseIf intRandom = 3 Then
                        shpDice33.Visible = True
                        intDice3 = 3
                    ElseIf intRandom = 4 Then
                        shpDice34.Visible = True
                        intDice3 = 4
                    ElseIf intRandom = 5 Then
                        shpDice35.Visible = True
                        intDice3 = 5
                    ElseIf intRandom = 6 Then
                        shpDice36.Visible = True
                        intDice3 = 6
                    End If
                End If
            End If
            If chkDice4.Checked = False Then
                If intCount = 4 Then
                    intRandom = Gen.Next(1, 6)
                    If intRandom = 1 Then
                        shpDice41.Visible = True
                        intDice4 = 1
                    ElseIf intRandom = 2 Then
                        shpDice42.Visible = True
                        intDice4 = 2
                    ElseIf intRandom = 3 Then
                        shpDice43.Visible = True
                        intDice4 = 3
                    ElseIf intRandom = 4 Then
                        shpDice44.Visible = True
                        intDice4 = 4
                    ElseIf intRandom = 5 Then
                        shpDice45.Visible = True
                        intDice4 = 5
                    ElseIf intRandom = 6 Then
                        shpDice46.Visible = True
                        intDice4 = 6
                    End If
                End If
            End If
            If chkDice5.Checked = False Then
                If intCount = 5 Then
                    intRandom = Gen.Next(1, 6)
                    If intRandom = 1 Then
                        shpDice51.Visible = True
                        intDice5 = 1
                    ElseIf intRandom = 2 Then
                        shpDice52.Visible = True
                        intDice5 = 2
                    ElseIf intRandom = 3 Then
                        shpDice53.Visible = True
                        intDice5 = 3
                    ElseIf intRandom = 4 Then
                        shpDice54.Visible = True
                        intDice5 = 4
                    ElseIf intRandom = 5 Then
                        shpDice55.Visible = True
                        intDice5 = 5
                    ElseIf intRandom = 6 Then
                        shpDice56.Visible = True
                        intDice5 = 6
                    End If
                End If
            End If
        Next intCount
    End Sub
    '----------------------------------------------
    Public Sub RollLimiter()                    'limits # of rolls
        If intRollsLeft = 2 Then
            Call DiceReset()
            Call DiceRoller()
            lblRollsLeft.Text = Convert.ToString(intRollsLeft)
            intRollsLeft = Convert.ToInt32(intRollsLeft)
            intRollsLeft = 1
        ElseIf intRollsLeft = 1 Then
            Call DiceReset()
            Call DiceRoller()
            lblRollsLeft.Text = Convert.ToString(intRollsLeft)
            intRollsLeft = Convert.ToInt32(intRollsLeft)
            intRollsLeft = 0
        ElseIf intRollsLeft = 0 Then
            Call DiceReset()
            Call DiceRoller()
            lblRollsLeft.Text = Convert.ToString(intRollsLeft)
            intRollsLeft = Convert.ToInt32(intRollsLeft)
            intRollsLeft = 3
        ElseIf intRollsLeft = 3 Then

            MessageBox.Show("Sorry, You are out of rolls. Please enter your score into the score chart.")
            Form2.Show()
            Form2.BringToFront()
            blnPlayTurnEnd = True
            blnTurnStart = False
            blnScored = False
        End If
    End Sub
    '----------------------------------------------
    Public Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click
        Form2.Show()
        Form2.BringToFront()
        blnScored = False
    End Sub
    '----------------------------------------------
    Public Sub ResetAll()               'resets varibles upon reload
        blnTurnStart = True
        blnGameStart = False
        intRandom = 0
        intCount = 0
        intRollsLeft = 2
        intDice1 = 0
        intDice2 = 0
        intDice3 = 0
        intDice4 = 0
        intDice5 = 0
        intTurnChooser = 0
        blnPlayTurnEnd = False
        blnRestart = False
        blnHasEnteredScore = False
        blnScored = False
    End Sub
    '----------------------------------------------
    Private Sub btnRoll_Click(sender As System.Object, e As System.EventArgs) Handles btnRoll.Click
        Call DiceReset()
        Call RollLimiter()

    End Sub
    '----------------------------------------------
    Private Sub btnShowRules_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowRules.Click
        Form4.Show()
    End Sub

    Public Sub CounterAce()         'sets the dice values to a variable and into an array
        If intDice1 = 1 Then
            PlayArr(0) = PlayArr(0) + 1
        End If
        If intDice2 = 1 Then
            PlayArr(0) = PlayArr(0) + 1
        End If
        If intDice3 = 1 Then
            PlayArr(0) = PlayArr(0) + 1
        End If
        If intDice4 = 1 Then
            PlayArr(0) = PlayArr(0) + 1
        End If
        If intDice5 = 1 Then
            PlayArr(0) = PlayArr(0) + 1
        End If
        blnHasEnteredScore = True
    End Sub
    Public Sub Counter2()           'sets the dice values to a variable and into an array
        If intDice1 = 2 Then
            PlayArr(1) = PlayArr(1) + 2
        End If
        If intDice2 = 2 Then
            PlayArr(1) = PlayArr(1) + 2
        End If
        If intDice3 = 2 Then
            PlayArr(1) = PlayArr(1) + 2
        End If
        If intDice4 = 2 Then
            PlayArr(1) = PlayArr(1) + 2
        End If
        If intDice5 = 2 Then
            PlayArr(1) = PlayArr(1) + 2
        End If
        blnHasEnteredScore = True
    End Sub
    Public Sub Counter3()           'sets the dice values to a variable and into an array
        If intDice1 = 3 Then
            PlayArr(2) = PlayArr(2) + 3
        End If
        If intDice2 = 3 Then
            PlayArr(2) = PlayArr(2) + 3
        End If
        If intDice3 = 3 Then
            PlayArr(2) = PlayArr(2) + 3
        End If
        If intDice4 = 3 Then
            PlayArr(2) = PlayArr(2) + 3
        End If
        If intDice5 = 3 Then
            PlayArr(2) = PlayArr(2) + 3
        End If
        blnHasEnteredScore = True
    End Sub
    Public Sub Counter4()           'sets the dice values to a variable and into an array
        If intDice1 = 4 Then
            PlayArr(3) = PlayArr(3) + 4
        End If
        If intDice2 = 4 Then
            PlayArr(3) = PlayArr(3) + 4
        End If
        If intDice3 = 4 Then
            PlayArr(3) = PlayArr(3) + 4
        End If
        If intDice4 = 4 Then
            PlayArr(3) = PlayArr(3) + 4
        End If
        If intDice5 = 4 Then
            PlayArr(3) = PlayArr(3) + 4
        End If
        blnHasEnteredScore = True
    End Sub
    Public Sub Counter5()           'sets the dice values to a variable and into an array
        If intDice1 = 5 Then
            PlayArr(4) = PlayArr(4) + 5
        End If
        If intDice2 = 5 Then
            PlayArr(4) = PlayArr(4) + 5
        End If
        If intDice3 = 5 Then
            PlayArr(4) = PlayArr(4) + 5
        End If
        If intDice4 = 5 Then
            PlayArr(4) = PlayArr(4) + 5
        End If
        If intDice5 = 5 Then
            PlayArr(4) = PlayArr(4) + 5
        End If
        blnHasEnteredScore = True
    End Sub
    Public Sub Counter6()           'sets the dice values to a variable and into an array
        If intDice1 = 6 Then
            PlayArr(5) = PlayArr(5) + 6
        End If
        If intDice2 = 6 Then
            PlayArr(5) = PlayArr(5) + 6
        End If
        If intDice3 = 6 Then
            PlayArr(5) = PlayArr(5) + 6
        End If
        If intDice4 = 6 Then
            PlayArr(5) = PlayArr(5) + 6
        End If
        If intDice5 = 6 Then
            PlayArr(5) = PlayArr(5) + 6
        End If
        blnHasEnteredScore = True
    End Sub
    '----------------------------------------------

End Class

~~~~~~~~~~~~~~~~~~~~~(Form 2)~~~~~~~~~~~~~~~~~~~~~

Public Class Form2 Public blnTurnStart As Boolean = Form1.blnPlayTurnEnd Public intDice1 As Integer = Form1.intDice1 'Dice 1 Value (From Form 1) Public intDice2 As Integer = Form1.intDice2 'Dice 2 Value ( " " " ) Public intDice3 As Integer = Form1.intDice3 'Dice 3 Value ( " " " ) Public intDice4 As Integer = Form1.intDice4 'Dice 4 Value ( " " " ) Public intDice5 As Integer = Form1.intDice5 'Dice 5 Value ( " " " ) Public blnRestart As Boolean = Form1.blnRestart Public PlayArr As Array = Form1.PlayArr Public blnScored As Boolean = Form1.blnScored Public blnHasEnteredScore As Boolean = Form1.blnHasEnteredScore Public Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click 'close score card button (if you open it and then decide to reroll without entering a score) Me.Hide() End Sub '---------------------------------------------- Public Sub btnPlayEndToBe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlayEndToBe.Click 'End turn button 'blnTurnStart = True 'blnRestart = True blnHasEnteredScore = False Form1.Close() Form1.Show() Me.Hide() End Sub '---------------------------------------------- '#############(Moved code below (between the lines of "#####") to form 1)############## 'Public Sub CounterAce() ' If intDice1 = 1 Then ' PlayArr(0) = PlayArr(0) + 1 ' End If ' If intDice2 = 1 Then ' PlayArr(0) = PlayArr(0) + 1 ' End If ' If intDice3 = 1 Then ' PlayArr(0) = PlayArr(0) + 1 ' End If ' If intDice4 = 1 Then ' PlayArr(0) = PlayArr(0) + 1 ' End If ' If intDice5 = 1 Then ' PlayArr(0) = PlayArr(0) + 1 ' End If 'End Sub 'Public Sub Counter2() ' If blnHasEnteredScore = False Then ' If lbl2SP.Text = Nothing Then ' If intDice1 = 2 Then ' PlayArr(1) += 2 ' End If ' If intDice2 = 2 Then ' PlayArr(1) += 2 ' End If ' If intDice3 = 2 Then ' PlayArr(1) += 2 ' End If ' If intDice4 = 2 Then ' PlayArr(1) += 2 ' End If ' If intDice5 = 2 Then ' PlayArr(1) += 2 ' End If ' End If ' blnHasEnteredScore = True ' End If 'End Sub 'Public Sub Counter3() ' If blnHasEnteredScore = False Then ' If lbl3SP.Text = Nothing Then ' If intDice1 = 3 Then ' PlayArr(2) += 3 ' End If ' If intDice2 = 3 Then ' PlayArr(2) += 3 ' End If ' If intDice3 = 3 Then ' PlayArr(2) += 3 ' End If ' If intDice4 = 3 Then ' PlayArr(2) += 3 ' End If ' If intDice5 = 3 Then ' PlayArr(2) += 3 ' End If ' End If ' blnHasEnteredScore = True ' End If 'End Sub 'Public Sub Counter4() ' If blnHasEnteredScore = False Then ' If lbl4SP.Text = Nothing Then ' If intDice1 = 4 Then ' PlayArr(3) += 4 ' End If ' If intDice2 = 4 Then ' PlayArr(3) += 4 ' End If ' If intDice3 = 4 Then ' PlayArr(3) += 4 ' End If ' If intDice4 = 4 Then ' PlayArr(3) += 4 ' End If ' If intDice5 = 4 Then ' PlayArr(3) += 4 ' End If ' End If ' blnHasEnteredScore = True ' End If 'End Sub 'Public Sub Counter5() ' If blnHasEnteredScore = False Then ' If lbl5SP.Text = Nothing Then ' If intDice1 = 5 Then ' PlayArr(4) += 5 ' End If ' If intDice2 = 5 Then ' PlayArr(4) += 5 ' End If ' If intDice3 = 5 Then ' PlayArr(4) += 5 ' End If ' If intDice4 = 5 Then ' PlayArr(4) += 5 ' End If ' If intDice5 = 5 Then ' PlayArr(4) += 5 ' End If ' End If ' blnHasEnteredScore = True ' End If 'End Sub 'Public Sub Counter6() ' If blnHasEnteredScore = False Then ' If lbl6SP.Text = Nothing Then ' If intDice1 = 6 Then ' PlayArr(5) += 6 ' End If ' If intDice2 = 6 Then ' PlayArr(5) += 6 ' End If ' If intDice3 = 6 Then ' PlayArr(5) += 6 ' End If ' If intDice4 = 6 Then ' PlayArr(5) += 6 ' End If ' If intDice5 = 6 Then ' PlayArr(5) += 6 ' End If ' End If ' blnHasEnteredScore = True ' End If 'End Sub '############################################################ Public Sub lbl1SP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl1SP.Click 'score inserter for Aces If lbl1SP.Text = Nothing And blnHasEnteredScore = False Then Dim intDice1 As Integer = Form1.intDice1 Call Form1.CounterAce() lbl1SP.Text = (Convert.ToString(PlayArr(0))) 'MessageBox.Show("Keep this Score?"), (MessageBoxButtons.OKCancel) 'If MsgBoxResult.Cancel = True Then ' lbl1SP.Text = "" 'End If End If blnHasEnteredScore = True End Sub Public Sub lbl2SP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl2SP.Click 'score inserter for 2's If lbl2SP.Text = Nothing And blnHasEnteredScore = False Then Dim intDice2 As Integer = Form1.intDice2 Call Form1.Counter2() lbl2SP.Text = (Convert.ToString(PlayArr(1))) 'MessageBox.Show("Keep this Score?"), (MessageBoxButtons.OKCancel) 'If MsgBoxResult.Cancel = True Then ' lbl1SP.Text = "" 'End If End If blnHasEnteredScore = True End Sub Public Sub lbl3SP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl3SP.Click 'score inserter for 3's If lbl3SP.Text = Nothing And blnHasEnteredScore = False Then Dim intDice3 As Integer = Form1.intDice3 Call Form1.Counter3() lbl3SP.Text = (Convert.ToString(PlayArr(2))) 'MessageBox.Show("Keep this Score?"), (MessageBoxButtons.OKCancel) 'If MsgBoxResult.Cancel = True Then ' lbl1SP.Text = "" 'End If End If blnHasEnteredScore = True End Sub Public Sub lbl4SP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl4SP.Click 'score inserter for 4's If lbl4SP.Text = Nothing And blnHasEnteredScore = False Then Dim intDice4 As Integer = Form1.intDice4 Call Form1.Counter4() lbl4SP.Text = (Convert.ToString(PlayArr(3))) 'MessageBox.Show("Keep this Score?"), (MessageBoxButtons.OKCancel) 'If MsgBoxResult.Cancel = True Then ' lbl1SP.Text = "" 'End If End If blnHasEnteredScore = True Me.Hide() End Sub Public Sub lbl5SP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl5SP.Click 'score inserter for 5's If lbl5SP.Text = Nothing And blnHasEnteredScore = False Then Dim intDice5 As Integer = Form1.intDice5 Call Form1.Counter5() lbl5SP.Text = (Convert.ToString(PlayArr(4))) 'MessageBox.Show("Keep this Score?"), (MessageBoxButtons.OKCancel) 'If MsgBoxResult.Cancel = True Then ' lbl1SP.Text = "" 'End If End If blnHasEnteredScore = True Me.Hide() End Sub Public Sub lbl6SP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl6SP.Click 'score inserter for 6's If lbl6SP.Text = Nothing And blnHasEnteredScore = False Then 'Dim intDice6 As Integer = Form1.intDice6 Call Form1.Counter6() lbl6SP.Text = (Convert.ToString(PlayArr(5))) 'MessageBox.Show("Keep this Score?"), (MessageBoxButtons.OKCancel) 'If MsgBoxResult.Cancel = True Then ' lbl1SP.Text = "" 'End If End If blnHasEnteredScore = True Me.Hide() End Sub Private Sub lbl3KindSP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl3KindSP.Click 'score inserter for 3 of a kind If lbl3KindSP.Text = Nothing And blnHasEnteredScore = False Then Dim intDice1 As Integer = Form1.intDice1 Dim intDice2 As Integer = Form1.intDice2 Dim intDice3 As Integer = Form1.intDice3 Dim intDice4 As Integer = Form1.intDice4 Dim intDice5 As Integer = Form1.intDice5 If intDice1 = intDice2 = intDice3 Then lbl3KindSP.Text = (Convert.ToString(intDice1 + intDice2 + intDice3)) ElseIf intDice1 = intDice2 = intDice4 Then lbl3KindSP.Text = (Convert.ToString(intDice1 + intDice2 + intDice4)) ElseIf intDice1 = intDice2 = intDice5 Then lbl3KindSP.Text = (Convert.ToString(intDice1 + intDice2 + intDice5)) ElseIf intDice1 = intDice3 = intDice4 Then lbl3KindSP.Text = (Convert.ToString(intDice1 + intDice3 + intDice4)) ElseIf intDice1 = intDice3 = intDice5 Then lbl3KindSP.Text = (Convert.ToString(intDice1 + intDice3 + intDice5)) ElseIf intDice1 = intDice4 = intDice5 Then lbl3KindSP.Text = (Convert.ToString(intDice1 + intDice4 + intDice5)) ElseIf intDice2 = intDice3 = intDice4 Then lbl3KindSP.Text = (Convert.ToString(intDice2 + intDice3 + intDice4)) ElseIf intDice2 = intDice3 = intDice5 Then lbl3KindSP.Text = (Convert.ToString(intDice2 + intDice3 + intDice5)) ElseIf intDice2 = intDice4 = intDice5 Then lbl3KindSP.Text = (Convert.ToString(intDice2 + intDice4 + intDice5)) ElseIf intDice3 = intDice4 = intDice5 Then lbl3KindSP.Text = (Convert.ToString(intDice3 + intDice4 + intDice5)) End If blnHasEnteredScore = True End If End Sub End Class

Forms 3 and 4 are so simple I'm positive they aren't the problem (plus i've commented the out and the problem still occurs)

PLEASE HELP ME!!!!  ITS DUE THIS WEDS.  (JUNE 4TH)   AND IT IS WORTH 4 TEST GRADES!!!



Viewing all articles
Browse latest Browse all 21115

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>