Computer Programming

Computer Programming » An Actual Computer Program » Keep Rolling Until Lost or Win

Additional Roll for Win or Lost Calculation

Keep Rolling Until Lost or Win

We need to keep on rolling if we did not get a win or lost message.



Public Class Form1

    Private Sub GoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click
        ' Declare the variables
        Dim Dice1 As Integer
        Dim Dice2 As Integer
        Dim ComeOut As Integer
        Dim NextRoll As Integer

        ' Produce the random dice rolls
        Randomize()
        Dice1 = CInt(Int(6 * Rnd()) + 1)
        Dice2 = CInt(Int(6 * Rnd()) + 1)

        ' Display the result
        DiceRoll1.Text = Dice1
        DiceRoll2.Text = Dice2

        ' Compute the come out roll
        ComeOut = Dice1 + Dice2

        ' Display the come out roll
        ComeOutRoll.Text = ComeOut

        ' Set it up to initially say roll again
        ComeOutResult.Text = "Need to roll again"

        ' Determine if Win via come out roll
        If ComeOut = 7 Then
            ComeOutResult.Text = "You Win!"
        End If
        If ComeOut = 11 Then
            ComeOutResult.Text = "You Win!"
        End If

        ' Determine if Lose via come out roll
        If (ComeOut = 2 Or ComeOut = 3 Or ComeOut = 12) Then
            ComeOutResult.Text = "You Lost!"
        End If

        ' Do more if we have to because we need to do more rolls
        If (ComeOut = 4 Or ComeOut = 5 Or ComeOut = 6 Or ComeOut = 8 Or ComeOut = 9 Or ComeOut = 10) Then
            Do
                ' Produce the random dice rolls
                Randomize()
                Dice1 = CInt(Int(6 * Rnd()) + 1)
                Dice2 = CInt(Int(6 * Rnd()) + 1)
                NextRoll = Dice1 + Dice2

                ' Display the result in the second dice roll area
                DiceRoll1B.Text = Dice1
                DiceRoll2B.Text = Dice2

                ' Determine if this result means correct
                If (NextRoll = 7) Then
                    FinalResult.Text = "You Lost!"
                ElseIf (NextRoll = ComeOut) Then
                    FinalResult.Text = "You Win!"
                Else
                    FinalResult.Text = "More rolls needed"
                End If
            Loop Until (NextRoll = 7 Or NextRoll = ComeOut)

        End If

    End Sub
End Class

We added one additional piece of code, for the program to continue rolling the dice until the final win or lost happens. You may notice that while we produce the statement that more rolls are needed, the user would not get that as a final result as the program would now just automatically roll again. We could take out that statement as it is no longer needed, but as that might be harder to follow than leaving it in, the statement stays. Feel free to remove it yourself.

Do
Loop Until (NextRoll = 7 Or NextRoll = ComeOut)

Run the program.


Our final code looks like this. We have added in


Public Class Form1

    Private Sub GoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click
        ' Declare the variables
        Dim Dice1 As Integer
        Dim Dice2 As Integer
        Dim ComeOut As Integer
        Dim NextRoll As Integer

        ' Clear out the initial text boxes in case we run again
        DiceRoll1.Text = ""
        DiceRoll2.Text = ""
        ComeOutRoll.Text = ""
        ComeOutResult.Text = ""
        DiceRoll1B.Text = ""
        DiceRoll2B.Text = ""
        FinalResult.Text = ""

        ' Produce the random dice rolls
        Randomize()
        Dice1 = CInt(Int(6 * Rnd()) + 1)
        Dice2 = CInt(Int(6 * Rnd()) + 1)

        ' Display the result
        DiceRoll1.Text = Dice1
        DiceRoll2.Text = Dice2

        ' Compute the come out roll
        ComeOut = Dice1 + Dice2

        ' Display the come out roll
        ComeOutRoll.Text = ComeOut

        ' Set it up to initially say roll again
        ComeOutResult.Text = "Need to roll again"

        ' Determine if Win via come out roll
        If ComeOut = 7 Then
            ComeOutResult.Text = "You Win!"
        End If
        If ComeOut = 11 Then
            ComeOutResult.Text = "You Win!"
        End If

        ' Determine if Lose via come out roll
        If (ComeOut = 2 Or ComeOut = 3 Or ComeOut = 12) Then
            ComeOutResult.Text = "You Lost!"
        End If

        ' Do more if we have to because we need to do more rolls
        If (ComeOut = 4 Or ComeOut = 5 Or ComeOut = 6 Or ComeOut = 8 Or ComeOut = 9 Or ComeOut = 10) Then
            Do
                ' Produce the random dice rolls
                Randomize()
                Dice1 = CInt(Int(6 * Rnd()) + 1)
                Dice2 = CInt(Int(6 * Rnd()) + 1)
                NextRoll = Dice1 + Dice2

                ' Display the result in the second dice roll area
                DiceRoll1B.Text = Dice1
                DiceRoll2B.Text = Dice2

                ' Determine if this result means correct
                If (NextRoll = 7) Then
                    FinalResult.Text = "You Lost!"
                ElseIf (NextRoll = ComeOut) Then
                    FinalResult.Text = "You Win!"
                Else
                    FinalResult.Text = "More rolls needed"
                End If
            Loop Until (NextRoll = 7 Or NextRoll = ComeOut)

        End If

    End Sub
End Class
We have added a final section Clear out the initial text boxes in case we run again that will make sure we can run again without any left over messages in our text boxes.

DiceRoll1.Text = ""
DiceRoll2.Text = ""
ComeOutRoll.Text = ""
ComeOutResult.Text = ""
DiceRoll1B.Text = ""
DiceRoll2B.Text = ""
FinalResult.Text = ""

Be sure to Save All to save your work. You are all done!

⇐ Prev Chapter
Additional roll for win or lost

Next Chapter ⇒
You are on the last chapter

Computer Programming for Everyday People
Michael Bigrigg

Amazon eBook

Powered by w3.css