Wednesday, April 20, 2011

Programming 2 */ Methods Assignment */

Public Class Form1

    '1. Create a single method that will be responsible for creating the place holder for each of the dice numbers.
    'a. You will likely need to pass the label in as an attribute in the method call
    '2. Create a single method that will be responsible for adding the button and 3 stats text boxes
    '3. Add a method who's job is just to roll the dice.
    '4. Add a method who's job is to collect the stats of how many there are of each number
    '5. Add a method who's job is to update the stats textboxes
    '6. Add comments to the code blocks to explain what is going on.
    'CODE:
    'Declaring variables
    Dim lblDice1, lblDice2, lblDice3, lblDice4, lblDice5 As New Label
    'To add events to the button
    Dim WithEvents butRoll As New Button
    'Declaring variables
    Dim nYatzee, nFourOfAKind, nThreeOfAKind As New Integer
    Dim lblYatzee, lblFourOfAKind, lblThreeOfAKind As New TextBox
    'Declaring random points
    Dim rnd As New Random

    'This sub is to add the dice numbering and randomization. Also used for the font and size of the label. And to add the controls.
    Private Sub addDice(ByRef lbl As Label, ByVal x As Integer, ByVal y As Integer)
        lbl.Text = 0
        lbl.Location = New Point(x, y)
        lbl.Font = New Drawing.Font("Microsoft Sans Serif", 28.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
        lbl.Height = 40
        lbl.Width = 40
        Me.Controls.Add(lbl)
    End Sub

    'This sub is to add the hand to the textboxes. Also for the size and to add the controls.
    Private Sub addHand(ByRef txt1 As TextBox, ByRef txt As String, ByVal x As Integer, ByVal y As Integer)
        txt1.Text = txt
        txt1.Location = New Point(x, y)
        Me.Controls.Add(txt1)
        txt1.Width = 150
    End Sub

    'This sub adds the roll button onto the form.
    Private Sub addRoll(ByRef btn As Button, ByVal txt As String, ByVal x As Integer, ByVal y As Integer)
        btn.Text = txt
        btn.Location = New Point(x, y)
        Me.Controls.Add(btn)
    End Sub

    'When the button is clicked it randomizes the numbers
    Private Sub addDi(ByVal lblDice As Label)
        lblDice.Text = rnd.Next(1, 7)
    End Sub

    Private Sub addText(ByRef txt As TextBox, ByVal txt1 As String)
        'This sub is used to add the calculation to the textboxes in the form
        txt.Text = txt1
    End Sub
    Private Sub addCalculation()
        'Declaring the variable
        Dim arrNumbers() As Integer = {0, 0, 0, 0, 0, 0}
        'For loop to randomize the numbers and determine what is a yahtzee, four of a kind, or three of a kind.
        For Each lbl As Label In Me.Controls.OfType(Of Label)()
            arrNumbers(lbl.Text - 1) += 1
        Next

        For Each i As Integer In arrNumbers
            If i = 5 Then
                nYatzee += 1
            ElseIf i = 4 Then
                nFourOfAKind += 1
            ElseIf i = 3 Then
                nThreeOfAKind += 1
            End If
        Next
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'These are linked to the subs and then added to the form1load.
        addDice(lblDice1, 10, 20)
        addDice(lblDice2, 70, 20)
        addDice(lblDice3, 130, 20)
        addDice(lblDice4, 190, 20)
        addDice(lblDice5, 250, 20)

        addHand(lblYatzee, "Yahtzees: 0", 20, 140)
        addHand(lblFourOfAKind, "Four Of A Kind: 0", 20, 180)
        addHand(lblThreeOfAKind, "Three Of A Kind: 0", 20, 220)

        addRoll(butRoll, "Roll", 100, 90)

    End Sub

    Private Sub RollDice() Handles butRoll.Click
        'all of these calculate when the button is clicked.
        addDi(lblDice1)
        addDi(lblDice2)
        addDi(lblDice3)
        addDi(lblDice4)
        addDi(lblDice5)

        addCalculation()

        addText(lblYatzee, "Yahtzee: " & nYatzee)
        addText(lblFourOfAKind, "Four Of A Kind: " & nFourOfAKind)
        addText(lblThreeOfAKind, "Three Of A Kind: " & nThreeOfAKind)

    End Sub
End Class

No comments:

Post a Comment

Classes

Programming II

Advanced Relational Database

Followers