?
Solved

Matching Game Hall of Fame

Posted on 2003-03-03
5
Medium Priority
?
228 Views
Last Modified: 2010-05-01
 In continuation with my Matching Game program I would like to figure out a way to load another form after the user wins my matching game. On this form the user will enter their name and the program will appropriately place the user's name and score among 1 of the 5 labels. The highest score will be in the top label and remain in the memory of the program to be viewed at the end of each winning game, regardless if you have a higher score than any of the previous 5 players or not. At the end of the script of my Matching Game form I want to then load the Hall of Fame form and only if the user has a higher score than any of the five already listed will the user be able to enter their name and thus submit their score. How should I go about doing this? I have thought of a few ways and at each I run into a wall. I'm trying to gain experience with Visual Basic by working on these small programs but it always helps to see ideas of others to learn how to program correctly.  Any help would be great. I'm offering lots of points for this one.
Here is the code for the first part of the game. (Form1) I'll need to insert some code in this to subsequently load The Hall of Fame (Form2)

Thanks

Form1 code:

Dim clckcnt As Integer
Dim buttonsDown As Integer 'Contains how many buttons pressed per turn
Dim buttonSequence(2) As Integer 'Contains the identity of the 1st and 2nd buttons

Private Sub Command17_Click()
clckcnt = 0
Label3.Caption = clckcnt
buttonsDown = 0

For n = 0 To 15
Command1(n).Visible = True
Next n
Frame2.Visible = False
Randomize Timer
mystring = "1122334455667788"
For n = 0 To 15
whichnumber = 1 + Int(Rnd * Len(mystring))
Label1(n).Caption = Mid$(mystring, whichnumber, 1)
mystring = Mid$(mystring, 1, whichnumber - 1) + Mid$(mystring, whichnumber + 1)
Next n
End Sub

Private Sub Command18_Click()
End
End Sub

Private Sub Command1_Click(Index As Integer)
Dim howMany As Integer 'Keeps the score

Label2.Caption = ""

buttonsDown = buttonsDown + 1 'Keep track of how many buttons have been pressed

If buttonsDown > 2 Then Exit Sub 'Do nothing if the user tries to click more than 2

clckcnt = clckcnt + 1 'Add one to the click count
Label3.Caption = clckcnt

buttonSequence(buttonsDown) = Index 'Store two buttons clicked
Command1(Index).Visible = False

If buttonsDown <> 2 Then Exit Sub 'Only one button down, so exit

If Label1(buttonSequence(1)).Caption = Label1(buttonSequence(2)).Caption Then
Label2.Caption = "MATCH !"
buttonsDown = 0

For n = 0 To 15 'Check the score
If Command1(n).Visible = False Then howMany = howMany + 1
Next n
If howMany = 16 Then Label2.Caption = "You win!"
Show Form2
Exit Sub
End If

Timer1.Interval = 2500
Timer1.Enabled = True
End Sub


Private Sub Timer1_Timer()
Command1(buttonSequence(1)).Visible = True
Command1(buttonSequence(2)).Visible = True
buttonsDown = 0
Timer1.Enabled = False
End Sub
0
Comment
Question by:Rocket8411
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Rhaedes
ID: 8060181
Hello again... First I have a question for you: presumably the best scores are the lowest ones (with the lowest clckcnt). Is this correct?

Rhaedes
0
 

Author Comment

by:Rocket8411
ID: 8060226
Ooop sorry about that. When I said ranked by the highest score I did mean in ascending order; the lowest amount of clicks being first place and then so on to the 5th label. So the lowest score is the winner.

Thanks
Rocket8411
0
 
LVL 5

Expert Comment

by:Rhaedes
ID: 8063869
Okay... There are many ways to go about this, so here's one:
In Form1, I have made a few modifications:
(i) There is now a Form_Load sub, which creates a Best Scores file if there is none. This file is referenced in the variable HighScoresFile, which you should change to where you want to keep it.
(ii)clckcnt is now declared as public so that it's value can be seen from other forms. HighScoresFile is also public for the same reason.
(iii) In your Command18_Click, you had the 'end' command. NEVER use this since it won't clean up your system when you exit the program. I've changed it to 'Unload me'.
(iv)In the same 'if' statement that show the 'You win' message, I've added a Form2.Show statement. This loads the Best Scores form and sets it running.

Here is the complete code for Form1:

Public HighScoresFile As String
Public clckcnt As Integer
Dim buttonsDown As Integer 'Contains how many buttons pressed per turn
Dim buttonSequence(2) As Integer 'Contains the identity of the 1st and 2nd buttons

Private Sub Command17_Click()
clckcnt = 0
Label3.Caption = clckcnt
buttonsDown = 0

For n = 0 To 15
Command1(n).Visible = True
Next n
Frame2.Visible = False
Randomize Timer
mystring = "1122334455667788"
For n = 0 To 15
whichnumber = 1 + Int(Rnd * Len(mystring))
Label1(n).Caption = Mid$(mystring, whichnumber, 1)
mystring = Mid$(mystring, 1, whichnumber - 1) + Mid$(mystring, whichnumber + 1)
Next n
End Sub

Private Sub Command18_Click()
Unload Me
End Sub

Private Sub Command1_Click(Index As Integer)
Dim howMany As Integer 'Keeps the score

Label2.Caption = ""

buttonsDown = buttonsDown + 1 'Keep track of how many buttons have been pressed

If buttonsDown > 2 Then Exit Sub 'Do nothing if the user tries to click more than 2

clckcnt = clckcnt + 1 'Add one to the click count
Label3.Caption = clckcnt

buttonSequence(buttonsDown) = Index 'Store two buttons clicked
Command1(Index).Visible = False

If buttonsDown <> 2 Then Exit Sub 'Only one button down, so exit

If Label1(buttonSequence(1)).Caption = Label1(buttonSequence(2)).Caption Then
Label2.Caption = "MATCH !"
buttonsDown = 0

For n = 0 To 15 'Check the score
If Command1(n).Visible = False Then howMany = howMany + 1
Next n
If howMany = 16 Then
 Label2.Caption = "You win!"
 Form2.Show 'Show best scores
End If
Exit Sub
End If

Timer1.Interval = 5000
Timer1.Enabled = True
End Sub

Private Sub Form_Load()
HighScoresFile = "c:\WHEREVER\Matching Game High-Scores.txt"

If Dir(HighScoresFile) = "" Then 'No high scores file exists, so make one
Open HighScoresFile For Output As #1
Print #1, "Mary"
Print #1, "104"
Print #1, "Fred"
Print #1, "189"
Print #1, "June"
Print #1, "209"
Print #1, "Mike"
Print #1, "210"
Print #1, "Bill"
Print #1, "389"
Close #1
End If
End Sub

Private Sub Timer1_Timer()
Command1(buttonSequence(1)).Visible = True
Command1(buttonSequence(2)).Visible = True
buttonsDown = 0
Timer1.Enabled = False
End Sub

***************
Now for form2. This form should contain 11 labels in an array. Label1(0) is not used at runtime, but you can it as a title by setting the caption to 'HIGH SCORES' or something at design time.
The remaining 10 labels are set out in two columns. The labels on the left should be longish, and will display the players names. The ones on the right can be shorter, since they will only display the scores. They should be set out as follows on the form:

label1(1)         label1(2)
label1(3)         label1(4)
label1(5)         label1(6)
label1(7)         label1(8)
label1(9)         label1(10)

Beneath them, place a commandbutton (command1), which is simply to close the form.
You also need to add a textbox (text1). The size/position doesnot matter, since the program will put it in the correct place according to where the player's name needs to be added.
There is more info in the comments in the code below.

Kindest regards,
Rhaedes

Code for form2:

Private Sub Form_Load()

Form2.Text1.Visible = False 'Make textbox for name input invisible

Open Form1.HighScoresFile For Input As #1 'Put the best scores in the Form2 Labels
For players = 1 To 5
 Line Input #1, aPlayer
 Line Input #1, aScore
 Label1(players * 2 - 1).Caption = aPlayer
 Label1(players * 2).Caption = aScore
Next players
Close #1


'Now check this games' score against the best scores

For players = 1 To 5
 If Form1.clckcnt <= Val(Label1(players * 2)) Then
'The value of 'players' is where the new score should appear.
  Position = players
'...So first move all the worse scores down one the list.
  For worsePlayers = 4 To Position Step -1 'Start at the end and work up
  Label1((worsePlayers + 1) * 2).Caption = Label1(worsePlayers * 2).Caption
  Label1((worsePlayers + 1) * 2 - 1).Caption = Label1(worsePlayers * 2 - 1).Caption
  Next worsePlayers

'...Update score
  Label1(Position * 2).Caption = Form1.clckcnt

'...Now place the TextBox over the caption to be rewritten
'(If you line these up correctly at designtime, only the height needs to be changed)
  With Text1
  .Top = Label1(Position * 2 - 1).Top
  .Left = Label1(Position * 2 - 1).Left
  .Width = Label1(Position * 2 - 1).Width
  .Height = Label1(Position * 2 - 1).Height
  .Visible = True 'Make textbox visible
  .Text = "ENTER YOUR NAME"
  .SelLength = 15 'Make text 'selected'
  End With
  Exit For
 End If
Next players
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Label1(Position * 2 - 1).Caption = Text1.Text 'Make caption like textbox
If KeyCode = 13 Then 'User has hit return
Command1_Click 'write scores and exits
End If
End Sub

Private Sub WriteScores()
'rewrite best scores file:
Open Form1.HighScoresFile For Output As #1
For players = 1 To 5
Print #1, Label1(players * 2 - 1)
Print #1, Label1(players * 2)
Next players
Close #1
End Sub
0
 

Author Comment

by:Rocket8411
ID: 8073689
Hey,
I set up the form2 just like you said and upon running the program, after completing the matching game, the form2 popped up and the name box was located in the appropriate label compared to the existing scores. The first letter I press in the text box results in error 341 (Invalid control array index) coming up. When I go to debug this line is highlighted.

Label1(Position * 2 - 1).Caption = Text1.Text 'Make caption like textbox

Any ideas? By any chance did you try running this yourself. I may have messed up on one of your steps along the way. I hope that is the case.
Thanks
Rocket8411
0
 
LVL 5

Accepted Solution

by:
Rhaedes earned 400 total points
ID: 8074324
Ooops - my mistake: I didn't cut'n'paste the whole form! I left out the code for the command1 button, and also the Dim statement for the Position Integer. The lack of the Dim statement was what was producing the error, since VB didn't recognise the variable (or thought its value was 0) outside of the sub where it was established.
Here is the complete code for form2:

Dim Position As Integer
Private Sub Command1_Click() 'This is the close button
WriteScores
Unload Me
End Sub

Private Sub Form_Load()

Form2.Text1.Visible = False 'Make textbox for name input invisible

Open Form1.HighScoresFile For Input As #1 'Put the best scores in the Form2 Labels
For players = 1 To 5
 Line Input #1, aPlayer
 Line Input #1, aScore
 Label1(players * 2 - 1).Caption = aPlayer
 Label1(players * 2).Caption = aScore
Next players
Close #1


'Now check this games' score against the best scores

For players = 1 To 5
 If Form1.clckcnt <= Val(Label1(players * 2)) Then
'The value of 'players' is where the new score should appear.
  Position = players
'...So first move all the worse scores down one the list.
  For worsePlayers = 4 To Position Step -1 'Start at the end and work up
  Label1((worsePlayers + 1) * 2).Caption = Label1(worsePlayers * 2).Caption
  Label1((worsePlayers + 1) * 2 - 1).Caption = Label1(worsePlayers * 2 - 1).Caption
  Next worsePlayers

'...Update score
  Label1(Position * 2).Caption = Form1.clckcnt

'...Now place the TextBox over the caption to be rewritten
'(If you line these up correctly at designtime, only the height needs to be changed)
  With Text1
  .Top = Label1(Position * 2 - 1).Top
  .Left = Label1(Position * 2 - 1).Left
  .Width = Label1(Position * 2 - 1).Width
  .Height = Label1(Position * 2 - 1).Height
  .Visible = True 'Make textbox visible
  .Text = "ENTER YOUR NAME"
  .SelLength = 15 'Make text 'selected'
  End With
  Exit For
 End If
Next players
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Label1(Position * 2 - 1).Caption = Text1.Text 'Make caption like textbox
If KeyCode = 13 Then 'User has hit return
Command1_Click 'write scores and exits
End If
End Sub

Private Sub WriteScores()
'rewrite best scores file:
Open Form1.HighScoresFile For Output As #1
For players = 1 To 5
Print #1, Label1(players * 2 - 1)
Print #1, Label1(players * 2)
Next players
Close #1
End Sub
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question