Ds.109
Ds.109

Reputation: 730

Guessing Game not keeping score - VB.NET

So I've made a guessing game and it's working 95%. However, there are 2 problems that I just can't figure out.

  1. Whenever the supposedly-random number is between 1 - 10, it will be a "7" 90% of the time.
  2. I have a score on Form 2, but whenever a score is added, it either resets back to one or adds one to the other player and removes one from the first.

Here's the code, and thank you in advance!

Public Class Form1
  Public turn As Boolean

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim turn1 As Integer

    Form2.Show()
    Me.Hide()
    turn1 = 2 * Rnd() + 0
    If turn1 = 0 Then
      turn = True
    Else
      turn = False
    End If

    If turn = True Then
      PictureBox3.Hide()
      PictureBox2.Show()
    End If

    If turn = False Then
      PictureBox3.Show()
      PictureBox2.Hide()
    End If

    If Form2.RadioButton5.Checked = True Then
      PictureBox1.Image = My.Resources.animated_60
      PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End If

    If Form2.RadioButton6.Checked = True Then
      PictureBox1.Image = My.Resources.myanistarb
      PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End If

    If Form2.RadioButton7.Checked = True Then
      PictureBox1.Image = My.Resources.black_rain_fall_animated
      PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End If       
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim player1guess As Integer = Val(TextBox1.Text)

    Static onescore As Integer
    If turn = True Then
      PictureBox2.Show()
      PictureBox3.Hide()

      Select Case player1guess
        Case Is = Form2.x
          MsgBox("You Got It")
          My.Computer.Audio.Play(My.Resources.wahoo, AudioPlayMode.Background)
          onescore += 1
          Me.Close()
          Form2.Activate()
          Form2.Label5.Text = Val(onescore)

        Case Is > Form2.x
          MsgBox("Too High")
          Label4.Text = Label4.Text & player1guess & "  - Too High" & vbCrLf
          My.Computer.Audio.Play(My.Resources.daffy_26, AudioPlayMode.Background)

        Case Is < Form2.x
          MsgBox("Too Low")
          Label4.Text = Label4.Text & player1guess & "  - Too Low" & vbCrLf
          My.Computer.Audio.Play(My.Resources.daffy_26, AudioPlayMode.Background)
      End Select
      turn = False
    End If

    If turn = False Then
      PictureBox2.Hide()
      PictureBox3.Show()
    End If

    If turn = False Then
      Button1.BackColor = Color.Black
      Button2.BackColor = Color.White
    End If
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim player2guess As Integer = Val(TextBox2.Text)
    Static twoscore As Integer

    If turn = False Then
      PictureBox2.Hide()
      PictureBox3.Show()

      Select Case player2guess
        Case Is = Form2.x
          MsgBox("You Got It")
          Form2.Label6.Text = Val(twoscore)
          My.Computer.Audio.Play(My.Resources.wahoo, AudioPlayMode.Background)
          twoscore += 1
          Me.Close()
          Form2.Show()

        Case Is > Form2.x
          MsgBox("Too High")
          Label5.Text = Label5.Text & player2guess & "  - Too High" & vbCrLf
          My.Computer.Audio.Play(My.Resources.daffy_26, AudioPlayMode.Background)

        Case Is < Form2.x
          MsgBox("Too Low")
          Label5.Text = Label5.Text & player2guess & "  - Too Low" & vbCrLf
          My.Computer.Audio.Play(My.Resources.daffy_26, AudioPlayMode.Background)
      End Select
      turn = True
    End If

    If turn = True Then
      PictureBox2.Show()
      PictureBox3.Hide()
    End If

    If turn = True Then
      Button2.BackColor = Color.Black
      Button1.BackColor = Color.White
    End If

  End Sub
End Class

This is where the random number is being generated:

Public Class Form2
  Public x As Integer
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Form1.NAME1.Text = TextBox1.Text
    Form1.NAME2.Text = TextBox2.Text

    If RadioButton1.Checked = True Then
      x = Rnd() * 10 
    ElseIf RadioButton2.Checked = True Then
      x = Rnd() * 100
    ElseIf RadioButton3.Checked = True Then
      x = Rnd() * 1000
    ElseIf RadioButton4.Checked = True Then
      x = Rnd() * 10000
    End If

    If TextBox1.Text = Nothing Then MsgBox("Please enter a name for Player 1")
    If TextBox2.Text = Nothing Then MsgBox("Please enter a name for Player 2")
    If TextBox1.Text <> Nothing And TextBox2.Text <> Nothing Then Form1.Show()
  End Sub
End Class

Upvotes: 0

Views: 516

Answers (1)

Ds.109
Ds.109

Reputation: 730

Ok thanks to all the comments, everything is fixed.

Randomize() fixed the number.

The score was fixed by adjusting the order of execution and making the variables Public.

Upvotes: 1

Related Questions