1 Replies - 1494 Views - Last Post: 08 April 2011 - 05:55 AM Rate Topic: -----

#1 borgmanjayce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 20-November 08

How do I start a new game in a random number generator?

Posted 07 April 2011 - 03:32 PM

I'm trying to teach myself how to create a Random Number Generator in Visual Basic 2008 using a menu system and I'm having difficulty with the following:

  • If I type in a number above 100, I don't get a message appearing saying that the number that I typed is too high.
  • I don't know what I need to code to start a new game.
  • How do I get messages to show up when both pressing the "Check Me" button and using the "Give Me the Number" option to say whether the answer is correct or too high or too low.


This is the code that I have so far:

Public Class GuessGame
    Private Sub GuessGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim rnd_number As Integer
        rnd_number = RandomNumber(100, 1)
    End Sub
    Public Function RandomNumber(ByVal MaxNumber As Integer, Optional ByVal MinNumber As Integer = 0) As Integer
        'initialize random number generator
        Dim r As New Random(System.DateTime.Now.Millisecond)

        'if passed incorrect arguments, swap them
        'can also throw exception or return 0

        If MinNumber > MaxNumber Then
            Dim t As Integer = MinNumber
            MinNumber = MaxNumber
            MaxNumber = True
        End If

        Return r.Next(MinNumber, MaxNumber)

    End Function
    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        AboutBox1.Show()
    End Sub
    Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.Click

    End Sub
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Close()
    End Sub
    Private Sub GiveMeTheNumberToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GiveMeTheNumberToolStripMenuItem.Click
        Dim result = MessageBox.Show("Would you like to start a new game?", "Start a New Game", MessageBoxButtons.YesNoCancel)
        If result = DialogResult.Cancel Then
            MessageBox.Show("The new game has been cancelled", "Cancel Game", MessageBoxButtons.OK)
        ElseIf result = DialogResult.No Then
            MessageBox.Show("Thank you for playing!", "Thanks for Playing", MessageBoxButtons.OK)
        ElseIf result = DialogResult.Yes Then
            MessageBox.Show("Enjoy your new game", "New Game", MessageBoxButtons.OK)
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckMe.Click
        Dim rndnumber As Random
        Dim Number As Integer
        rndnumber = New Random
        Number = rndnumber.Next(1, 101)
        txtAnswer.Text = Number
    End Sub
    Private Sub InstructionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InstructionsToolStripMenuItem.Click
        InstructionsScreen.Show()
    End Sub
    Private Sub StartNewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartNewGameToolStripMenuItem.Click

    End Sub
    Private Sub txtGuess_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAnswer.KeyPress
        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then e.Handled = True
        Dim Number As Integer
        Number = txtGuess.Text
        If Number > 101 Then
            MessageBox.Show("You have typed in a number higher than 100! Please type in a number that is less than 100!", "Please retype the number!", MessageBoxButtons.OK)
        End If
    End Sub
    Private Sub GuessGame_Closing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MessageBox.Show("Would you like to exit the game?", "Exit Game", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            Exit Sub
        End If
        e.Cancel = True
    End Sub
    Private Sub txtAnswer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAnswer.TextChanged
        txtAnswer.Enabled = False
    End Sub
End Class



If anyone has any ideas on how I can improve the code, I'd be really grateful to hear them.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I start a new game in a random number generator?

#2 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: How do I start a new game in a random number generator?

Posted 08 April 2011 - 05:55 AM

Public Class Form1

    Dim prng As New Random
    Dim numberToGuess As Integer
    Const maxN As Integer = 100

    Private Sub Button1_Click(sender As System.Object, _
                              e As System.EventArgs) Handles Button1.Click
        'guess
        Dim foo As Integer
        If Integer.TryParse(TextBox1.Text, foo) Then
            If foo = numberToGuess Then
                'winner code
                TextBox1.Text = ""
                Label1.Text = numberToGuess.ToString & " Winner"
                Button3.PerformClick()
            ElseIf foo > numberToGuess Then
                'less
                Stop
            Else
                'higher
                Stop
            End If
        Else
            Label1.Text = "Enter only numbers."
        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, _
                              e As System.EventArgs) Handles Button2.Click
        'solve
        TextBox1.Text = ""
        Label1.Text = numberToGuess.ToString
        Button3.PerformClick()
    End Sub

    Private Sub Button3_Click(sender As System.Object, _
                              e As System.EventArgs) Handles Button3.Click
        'new game
        numberToGuess = prng.Next(1, maxN + 1)
    End Sub

    Private Sub Form1_Shown(sender As Object, _
                            e As System.EventArgs) Handles Me.Shown
        Button3.PerformClick()
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, _
                                     e As System.EventArgs) Handles TextBox1.TextChanged
        Dim foo As Integer
        If Not Integer.TryParse(TextBox1.Text, foo) Then
            Label1.Text = "Enter only numbers."
        End If
    End Sub
End Class


This post has been edited by dbasnett: 08 April 2011 - 05:56 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1