Catherinedally 0 Unverified User
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim Dice1 As Integer
        Dim Dice2 As Integer


        Randomize()
        Dice1 = Int(Rnd() * 6) + 1  'Randomising dice 1 between 1 and 6
        Dice2 = Int(Rnd() * 6) + 1  'Randomising dice 2 between 1 and 6
        Label1.Text = Dice1         'Showing the dice number
        Label2.Text = Dice2         'Showing the dice number

        PictureBox1.Image = Image.FromFile("Dieside" & CStr(Dice1) & ".gif")
        PictureBox2.Image = Image.FromFile("Dieside" & CStr(Dice2) & ".gif")

        'Making an If statement that Dice1 and Dice 2 equal then the score is 5 and its a win
        If Dice1 = Dice2 Then
            score = score + 5
            win = win + 1
            scorecount = 5  'the score is 5 is won
            MsgBox("Luck you 5 points", MsgBoxStyle.Exclamation, "Great")
            'Else if Dice1 and Dice2 are different then its a loss and you lose 1 point
        Else
            score = score - 1
            MsgBox("You lost 1 Point", MsgBoxStyle.Critical, "Try Again")

            scorecount = -1 'the score is -1 if lost
        End If

        count = count + 1   'counting how many times the roll dice button is clicked
        'Giving instructions for the listbox so that the Dice1 and Dice2 numbers are printed and the
        'score also what the score is
        ListBox1.Items.Add(Dice1 & "    " & Dice2 & "    " & scorecount & "    " & score)

        Label3.Text = score     'shows the score in the label


    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Show()
        ListBox2.Hide()

        ListBox1.Items.Add("D1   " & "D2  " & "W/L  " & "Score")
        Label4.Text = InputBox("Please enter your name", "Name")

        PictureBox3.Image = Image.FromFile("dice" & ".gif")

        Dim Scores As String
        Dim Name As String
        Dim sr As IO.StreamReader = IO.File.OpenText("Scores.TXT")
        ListBox2.Items.Clear()
        Do While sr.Peek() <> -1
            Name = sr.ReadLine
            Scores = sr.ReadLine
            ListBox2.Items.Add(Name & "         " & Scores)
        Loop
        sr.Close()



    End Sub
    'EXIT
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        ListBox1.Hide()
        ListBox2.Show()

        Avg(Name)

            MsgBox("Your total score is " & score & vbCrLf & "You have played " & count & " time(s) and won  " & win & " times.", MsgBoxStyle.Information, "Thank You For Playing The Game")
            ListBox2.Items.Add(Label4.Text & "    " & score)

            Me.Close()
        'Closes the program when EXIT is pressed also gives a messagebox with the total score, how many times the game was played with amount of wins.

    End Sub
'New Player
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ListBox1.Show()
        ListBox2.Hide()

        Dim Scores As String
        Dim Name As String
        Dim sw As StreamWriter = IO.File.AppendText("Scores.txt")

        Name = Label4.Text
        Scores = Label3.Text
        sw.WriteLine(Name)
        sw.WriteLine(Scores)
        sw.Close()

        ListBox2.Items.Add(Name & "  " & Scores)

        Label4.Text = InputBox("Enter New Players Name", "New Player")
        ListBox1.Items.Clear()
        Label1.Text = ""
        Label2.Text = ""
        Label3.Text = ""
        score = 0
        ListBox1.Items.Clear()
        ListBox1.Items.Add("D1   " & "D2  " & "W/L  " & "Score")
    End Sub

    'New Game

    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        ListBox1.Show()
        ListBox2.Hide()
        ListBox2.Items.Clear()
        Label1.Text = ""
        Label2.Text = ""
        score = 0
        Label3.Text = ""
        Dim name As String
        Dim Scores As String
        Dim sw As StreamWriter = IO.File.AppendText("Scores.txt")

        name = Label4.Text
        Scores = Label3.Text
        sw.WriteLine(name)
        sw.WriteLine(Scores)
        sw.Close()
        ListBox1.Items.Clear()
        ListBox1.Items.Add("D1   " & "D2  " & "W/L  " & "Score")

    End Sub

    Function Avg(ByVal Name As String) As Integer
        Dim sr As IO.StreamReader = File.OpenText("Scores.TXT")
        Dim SearchName As String
        Dim Times_played, runningtotal, score As Integer

        ListBox1.Hide()
        ListBox2.Show()

        Do While sr.Peek() <> -1
            SearchName = sr.ReadLine
            score = sr.ReadLine


            If SearchName = Name Then
                ListBox2.Items.Add(SearchName & vbTab & score)
                Times_played = Times_played + 1
                runningtotal = runningtotal + score
            End If
        Loop
        sr.Close()
        Avg = runningtotal / Times_played


        ListBox2.Items.Add(Name & "Your Average is: " & Avg)

    End Function

I'm stuck with the last part of my dice game is there anyone that can help.
in the function I need to have an averge output and I can't get it to work can some one help me please

Function Avg(ByVal Name As String) As Integer
        Dim sr As IO.StreamReader = File.OpenText("Scores.TXT")
        Dim SearchName As String
        Dim Times_played, runningtotal, score As Integer

        ListBox1.Hide()
        ListBox2.Show()

        Do While sr.Peek() <> -1
            SearchName = sr.ReadLine
            score = sr.ReadLine


            If SearchName = Name Then
                ListBox2.Items.Add(SearchName & vbTab & score)
                Times_played = Times_played + 1
                runningtotal = runningtotal + score
            End If
        Loop
        sr.Close()
        Avg = runningtotal / Times_played


        ListBox2.Items.Add(Name & "Your Average is: " & Avg)

    End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.