Friday, May 6, 2011

Methods 2 - Yahtzee

 Public Class Form1
  
   'Dim variables used in different Methods
  
   Dim lblDice1, lblDice2, lblDice3, lblDice4, lblDice5 As New Label
  
   Dim WithEvents butRoll As New Button
  
   Dim nYatzee, nFourOfAKind, nThreeOfAKind As New Integer
  
   Dim lblYatzee, lblFourOfAKind, lblThreeOfAKind As New TextBox
  
   Dim rnd As New Random
  
   Private Sub AddDice(ByRef lbl As Label, ByVal x As Integer, ByVal y As Integer)
  
     'Method for adding the differe labels to display the numbers rolled
  
     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
  
   Private Sub AddOutput(ByRef lbl As TextBox, ByVal outcome As String, ByVal x As Integer, ByVal y As Integer)
  
     'Method of adding the output from the roll into the label
  
     lbl.Text = outcome
  
     lbl.Location = New Point(x, y)
  
     lbl.Width = 150
  
     Me.Controls.Add(lbl)
  
   End Sub
  
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
     'Adding the labels to display the dice
  
     AddDice(lblDice1, 10, 20)
  
     AddDice(lblDice2, 70, 20)
  
     AddDice(lblDice3, 130, 20)
  
     AddDice(lblDice4, 190, 20)
  
     AddDice(lblDice5, 250, 20)
  
     'Adding the actual numbers that have been rolled into the dice labels
  
     AddOutput(lblYatzee, "Yahtzee: 0", 20, 140)
  
     AddOutput(lblFourOfAKind, "Four Of A Kind: 0", 20, 180)
  
     AddOutput(lblThreeOfAKind, "Three Of A Kind: 0", 20, 220)
  
     'Creating the Roll button
  
     butRoll.Text = "Roll"
  
     butRoll.Location = New Point(100, 90)
  
     'Adding the roll button and output lables
  
     Me.Controls.Add(butRoll)
  
     Me.Controls.Add(lblFourOfAKind)
  
     Me.Controls.Add(lblThreeOfAKind)
  
   End Sub
  
   Private Sub roll(ByRef lbl As Label)
  
     'Method telling how to roll the dice
  
     lbl.Text = rnd.Next(1, 7)
  
   End Sub
  
   Private Sub RollDice() Handles butRoll.Click
  
     'Rolling the Dice
  
     roll(lblDice1)
  
     roll(lblDice2)
  
     roll(lblDice3)
  
     roll(lblDice3)
  
     roll(lblDice4)
  
     roll(lblDice5)
  
     'Getting the stats from rolling the dice
  
     stats()
  
     'Updating the textboxes as to what was rolled
  
     UpdateTextboxes()
  
   End Sub
  
   Private Sub stats()
  
     'Creating an array to keep track of what was rolled and deciding if a three of a kind, four of a kind, or yahtzee
  
     Dim arrNumbers() As Integer = {0, 0, 0, 0, 0, 0}
  
     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 UpdateTextboxes()
  
     'Method stating to update the text boxes after each roll
  
     lblYatzee.Text = "Yatzees: " & nYatzee
  
     lblFourOfAKind.Text = "Four Of A Kind: " & nFourOfAKind
  
     lblThreeOfAKind.Text = "Three Of A Kind: " & nThreeOfAKind
  
   End Sub
  
 End Class
  

No comments:

Post a Comment