Results 1 to 10 of 10

Thread: Arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Posts
    5

    Question

    Good afternoon,

    Could someone help me with my first array. What this program is suppose to do is:

    Keeps track of how many games you play and how many guesses each game took. I am able to get the number of guesses for each game to refill in the list box. However, I want to store the number of guesses for each game in an array for future calculations. Could you please take a look at the code below to give me some assistance. If you notice if anything could be coded much similar please don't hesitate....suggestions and opinions always welcomed.

    Thanks.



    Option Explicit

    Dim intAnswer As Integer
    Dim intGuessArray(0 To 9) As Integer
    Dim intHigh As Integer
    Dim inLow As Integer
    Dim intTotalGuess As Integer
    Dim intTotalGames As Integer
    Private Sub cmdGuess_Click()

    If txtGuess.Text = "" Then
    MsgBox "Guess is Required", vbOKOnly, "Game Program"
    txtGuess.SetFocus
    Exit Sub
    End If

    If IsNumeric(txtGuess.Text) = False Then
    MsgBox "The guess must be numeric", vbOKOnly, "Game Program"
    txtGuess.SetFocus
    Exit Sub
    End If

    If Val(txtGuess.Text) > 10 Then
    MsgBox "The guess must be from 1 to 10", vbOKOnly, "Game Program"
    txtGuess.SetFocus
    Exit Sub
    End If

    'OLD ONE
    'If Val(txtGuess.Text) < intAnswer Then
    ' lblAnswer.Caption = " The Answer is: Higher"
    ' intTotalGuess = intTotalGuess + 1
    ' Exit Sub
    'End If
    '
    'If Val(txtGuess.Text) > intAnswer Then
    ' lblAnswer.Caption = " The Answer is: Lower"
    ' intTotalGuess = intTotalGuess + 1
    ' Exit Sub
    'End If
    '
    'If Val(txtGuess.Text) = intAnswer Then
    ' intTotalGuess = intTotalGuess + 1
    ' lblAnswer.Caption = "You are correct, it took you only " & intTotalGuess & " time(s)."
    ' Exit Sub
    'End If

    'NEW ONE

    intTotalGuess = intTotalGuess + 1

    If Val(txtGuess.Text) < intAnswer Then
    lblAnswer.Caption = " The Answer is: Higher"
    ElseIf Val(txtGuess.Text) > intAnswer Then
    lblAnswer.Caption = " The Answer is: Lower"
    ElseIf Val(txtGuess.Text) = intAnswer Then
    lblAnswer.Caption = "You are correct, it took you only " & intTotalGuess & " time(s)."
    End If

    End Sub

    Private Sub cmdNew_Click()

    'Assigns a new answer by generating a random number from 1 - 10 and
    'displays the answer to a label
    intAnswer = Int(10 * Rnd + 1)
    lblmsg.Caption = intAnswer

    intTotalGames = intTotalGames + 1

    'Clears the Guess box and the Caption Box
    txtGuess.Text = ""
    lblAnswer.Caption = ""
    End Sub
    Private Sub cmdshow_Click()

    lstGuess.AddItem intTotalGames & " " & intTotalGuess
    intTotalGuess = 0

    End Sub

  2. #2
    Junior Member
    Join Date
    Nov 2000
    Location
    South Carolina
    Posts
    16

    This is how I would do it....

    What this program does is when you click command1, it starts the new game, assigns a value to the game, and ends the game (little example, I know). But what this shows is that you can ReDim the array, keeping the information already in there, and just keep a running count of the games. (Instead of putting the number of games in there, you can put the number of guesses upon being correct). The second command button just adds up the array for you.

    Hope this helps....

    Option Explicit

    Dim this_array() As Integer
    Dim games As Integer

    Private Sub Command1_Click()
    'Start a new game
    games = games + 1
    'Makes sure there is room for the next game in the array
    'while keeping all data in the array
    ReDim Preserve this_array(games)
    'puts a value in the array in last posistion
    this_array(games) = games

    End Sub

    Private Sub Command2_Click()
    Dim i As Integer
    Dim tot As Integer
    'totals up the total of the array
    For i = 1 To games
    tot = tot + this_array(i)
    Next
    'shows number of games and the total
    MsgBox Str(games) & " " & Str(tot)

    End Sub

    Private Sub Form_Load()

    games = 0


    End Sub
    Jonathan

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    'slow day at the office...
    'I use only one label...lblAnswer
    'forgive me if I have butchered your game beyond the point of good taste

    Code:
    Option Explicit
    
    'have made the variables accessable from all subs in this application
    
    Public intTotalGames As Integer
    Public intTotalGuess As Integer
    Public intGuessCountTotals As Integer
    Public intAnswer As Integer
    Public Answer As Integer
    
    Private Sub cmdGuess_Click()
    'need data
        If txtGuess.Text = "" Then
            MsgBox "Guess is Required", vbOKOnly, "Game Program"
            txtGuess.SetFocus
                Exit Sub
        End If
    'must be a number between 1 and 10
        If Val(txtGuess) > 10 Or Val(txtGuess) < 1 Then
            MsgBox "The guess must be from 1 to 10", vbOKOnly, "Game Program"
            txtGuess.Text = ""
            txtGuess.SetFocus
                Exit Sub
        End If
        
    'set value of Answer
        If Val(txtGuess) < intAnswer Then Answer = 1
        If Val(txtGuess) > intAnswer Then Answer = 2
        If Val(txtGuess) = intAnswer Then Answer = 3
        
    'increment the guess count
        intTotalGuess = intTotalGuess + 1
    '
    'act according to the value of Answer
        Select Case Answer
        Case 1
            lblAnswer.Caption = " The Answer is: Higher"
            txtGuess = ""
            txtGuess.SetFocus
        Case 2
            lblAnswer.Caption = " The Answer is: Lower"
            txtGuess = ""
            txtGuess.SetFocus
        Case 3
            lblAnswer.Caption = "You are correct, it took you only " & intTotalGuess & " time(s)."
            txtGuess = ""
            txtGuess.SetFocus
            Call Form_Load
            cmdShow.Enabled = True
            'keep a running total of all guesses  'no need for array if all you want is a total
            'an array is like a variable and loses it's life when the application dies
            intGuessCountTotals = intGuessCountTotals + intTotalGuess
            'reset the guess count
            intTotalGuess = 0
        End Select
    
    End Sub
    
    Private Sub cmdNew_Click()
    
    'Assigns a new answer by generating a random number from 1 - 10 and
    'displays the answer to a label
    Randomize
    intAnswer = CInt(10 * Rnd + 1)
    
    intTotalGames = intTotalGames + 1
    
    'Clears the Guess box and the Caption and sets focus
        txtGuess.Enabled = True
        txtGuess.Text = ""
        lblAnswer.Caption = ""
        txtGuess.SetFocus
    End Sub
    
    Private Sub cmdshow_Click()
    'display the game session stats
        lstguess.Clear
        lstguess.AddItem " Game  Total = " & intTotalGames
        lstguess.AddItem " Guess Total =  " & intTotalGuess
        lstguess.AddItem "_________________________________"
    
    End Sub
    
    Private Sub Form_Load()
    'no need for these till new is selected
        txtGuess.Enabled = False
        cmdShow.Enabled = False
        cmdGuess.Enabled = False
    End Sub
    
    Private Sub txtGuess_KeyPress(KeyAscii As Integer)
    'allow only numeric data to enter textbox
        If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 13
    'enable the guess command button as you have data to guess on
     cmdGuess.Enabled = True
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Cool Make it shorter

    You can make it shorter HeSaidJoe:

    Why are you using this:
    Code:
    'set value of Answer
        If Val(txtGuess) < intAnswer Then Answer = 1
        If Val(txtGuess) > intAnswer Then Answer = 2
        If Val(txtGuess) = intAnswer Then Answer = 3
    it is not necessary, you can use this instead of it:
    Code:
    Select Case Val(txtGuess)
    Case Is < intAnswer
       ...
    Case Is > intAnswer
       ...
    Case intAnswer
       ...
    End Select
    Am I right?

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    WP You are most certainly right...playing on work time does not always constitue good code. I also missed on the total guess count displayed in the listbox. I used the game one and not the total used in all games.

    Here is the corrected version.
    Code:
    Option Explicit
    
    'have made the variables accessable from all subs in this application
    
    Public intTotalGames As Integer
    Public intTotalGuess As Integer
    Public intGuessCountTotals As Integer
    Public intAnswer As Integer
    
    Private Sub cmdGuess_Click()
    'need data
        If txtguess.Text = "" Then
            MsgBox "Guess is Required", vbOKOnly, "Game Program"
            txtguess.SetFocus
                Exit Sub
        End If
    'must be a number between 1 and 10
        If Val(txtguess) > 10 Or Val(txtguess) < 1 Then
            MsgBox "The guess must be from 1 to 10", vbOKOnly, "Game Program"
            txtguess.Text = ""
            txtguess.SetFocus
                Exit Sub
        End If
        
    'increment the guess count
        intTotalGuess = intTotalGuess + 1
    '
    'act according to the value of Answer
        
        Select Case Val(txtguess)
        
       Case Is < intAnswer
            lblanswer.Caption = " The Answer is: Higher"
            txtguess = ""
            txtguess.SetFocus
       Case Is > intAnswer
            lblanswer.Caption = " The Answer is: Lower"
            txtguess = ""
            txtguess.SetFocus
       Case intAnswer
            lblanswer.Caption = "You are correct, it took you only " & intTotalGuess & " time(s)."
            txtguess = ""
            txtguess.SetFocus
            Call Form_Load
            cmdShow.Enabled = True
            'keep a running total of all guesses  'no need for array if all you want is a total
            'an array is like a variable and loses it's life when the application dies
            intGuessCountTotals = intGuessCountTotals + intTotalGuess
            'reset the guess count
            intTotalGuess = 0
        End Select
    
    End Sub
    
    Private Sub cmdNew_Click()
    
    'Assigns a new answer by generating a random number from 1 - 10 and
    'displays the answer to a label
    Randomize
    intAnswer = CInt(10 * Rnd + 1)
    
    intTotalGames = intTotalGames + 1
    
    'Clears the Guess box and the Caption and sets focus
        txtguess.Enabled = True
        txtguess.Text = ""
        lblanswer.Caption = ""
        txtguess.SetFocus
    End Sub
    
    Private Sub cmdshow_Click()
    'display the game session stats
        lstguess.Clear
        lstguess.AddItem " Game  Total = " & intTotalGames
        lstguess.AddItem " Guess Total =  " & intGuessCountTotals
        lstguess.AddItem "_________________________________"
    
    End Sub
    
    Private Sub Command1_Click()
    
    End Sub
    
    Private Sub Form_Load()
    'no need for these till new is selected
        txtguess.Enabled = False
        cmdShow.Enabled = False
        cmdGuess.Enabled = False
    End Sub
    
    Private Sub txtGuess_KeyPress(KeyAscii As Integer)
    'allow only numeric data to enter textbox
        If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 13
    'enable the guess command button as you have data to guess on
     cmdGuess.Enabled = True
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  6. #6
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Question Question to HeSaidJoe

    What does Randomize mean? is it re-generating all random numbers, or what?
    What is CInt? is it setting the data as an integer, so there are no numbers after the comma?

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  7. #7
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496

    Cool

    What randomize does is ressed the random number generator, based on the system clock. Otherwords, starts a new sequence of random numbers, This alows for the numbers not to be the same, each time the program is run

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Randomize is as above..if you leave it out you lose the randomness of generations so it should be used before your Rnd function is invoked.

    Cint is as you say...makes a numer an integer..thus removing decimal places...Fix would do the same job for random numbers...Int rounds up Fix rounds down..

    Under Functions in Help.

    Cint...integer
    CLng...long
    CDbl...Double
    CSng...Singel
    Cstr...String
    and on and on
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Wink Yeah

    Thanks

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  10. #10
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    If you did want to use the array:

    Code:
    Option Explicit
    
    'have made the variables accessable from all subs in this application
    
    Public intTotalGames As Integer
    Public intTotalGuess As Integer
    Public intGuessCountTotals As Integer
    Public intAnswer As Integer
    
    Private Sub cmdGuess_Click()
    'need data
        If txtGuess.Text = "" Then
            MsgBox "Guess is Required", vbOKOnly, "Game Program"
            txtGuess.SetFocus
                Exit Sub
        End If
    'must be a number between 1 and 10
        If Val(txtGuess) > 10 Or Val(txtGuess) < 1 Then
            MsgBox "The guess must be from 1 to 10", vbOKOnly, "Game Program"
            txtGuess.Text = ""
            txtGuess.SetFocus
                Exit Sub
        End If
        
    'increment the guess count
        intTotalGuess = intTotalGuess + 1
    '
    'act according to the value of Answer
        
        Select Case Val(txtGuess)
        
       Case Is < intAnswer
            lblAnswer.Caption = " The Answer is: Higher"
            txtGuess = ""
            txtGuess.SetFocus
       Case Is > intAnswer
            lblAnswer.Caption = " The Answer is: Lower"
            txtGuess = ""
            txtGuess.SetFocus
       Case intAnswer
            lblAnswer.Caption = "You are correct, it took you only " & intTotalGuess & " time(s)."
            txtGuess = ""
            txtGuess.SetFocus
            Call Form_Load
            cmdShow.Enabled = True
            
            ReDim Preserve myArray(intTotalGames - 1)
             myArray(intTotalGames - 1) = intTotalGuess
                             
                intTotalGuess = 0
        End Select
    
    End Sub
    
    Private Sub cmdNew_Click()
    
    'Assigns a new answer by generating a random number from 1 - 10 and
    'displays the answer to a label
    Randomize
    intAnswer = CInt(10 * Rnd + 1)
    MsgBox intAnswer
    intTotalGames = intTotalGames + 1
    
    'Clears the Guess box and the Caption and sets focus
        txtGuess.Enabled = True
        txtGuess.Text = ""
        lblAnswer.Caption = ""
        txtGuess.SetFocus
    End Sub
    
    Private Sub cmdshow_Click()
    'display the game session stats
        lstGuess.Clear
        lstGuess.Font = "Courier"
            For intTotalGames = LBound(myArray) To UBound(myArray)
                    lstGuess.AddItem "Games  Played: " & intTotalGames + 1
                    lstGuess.AddItem "Guess Counted: " & myArray(intTotalGames)
             Next intTotalGames
    
    End Sub
    
    Private Sub Command1_Click()
    
    End Sub
    
    Private Sub Form_Load()
    'no need for these till new is selected
        txtGuess.Enabled = False
        cmdShow.Enabled = False
        cmdGuess.Enabled = False
    End Sub
    
    Private Sub txtGuess_KeyPress(KeyAscii As Integer)
    'allow only numeric data to enter textbox
        If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 13
    'enable the guess command button as you have data to guess on
     cmdGuess.Enabled = True
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width