help with using an array in a 'hangman' game...

vinyl409

Member
Joined
Mar 16, 2005
Messages
6
Programming Experience
Beginner
i am currently working on a 'Hangman' program that internally stores 10 different words. each time a new game is started, a different word is to be randomly selected. placeholders are required for the letters (i used labels), and for each wrong guess another peice of the picture should show. used (wrong) letters should display as well. i've been working on it for quite a while, and finally got the code working. however, i just found out yesterday that we are required to use arrays. so for the part of my program where I used a Select Case to randomly select the words, I'm supposed to use an array instead. i'm stuck on how to go about doing this. here's the code (private sub) for selecting the word:

VB.NET:
 Private Sub SelectWord()
		Dim x As Integer
		Dim RandomOrder As Random = New Random
		x = RandomOrder.Next(1, 10) 'Assigns a random Integer 1-10

		If z = x Then
			x = RandomOrder.Next(1, 10) 'Prevents the same word coming up twice.
		End If

		Select Case x
			Case 1
				txtWord.Text = "yams"
			Case 2
				txtWord.Text = "baseball"
			Case 3
				txtWord.Text = "computer"
			Case 4
				txtWord.Text = "jacket"
			Case 5
				txtWord.Text = "keyboard"
			Case 6
				txtWord.Text = "guitar"
			Case 7
				txtWord.Text = "billboard"
			Case 8
				txtWord.Text = "highway"
			Case 9
				txtWord.Text = "italian"
			Case 10
				txtWord.Text = "projector"
		End Select
		z = x
		Call AllLetters()

	End Sub


how would i do this differently, using an array?

attached is a zip file of my program in case you need to see all of the code to my program in order to help me out.


also, another thing i'm stuck on is - when the user chooses to exit, i'm supposed to display the number of games played and the percentage of wins. any help would be appreciated. thanks a lot.


edit: the file i tried to attach was too large. let me know which code you may need and i can copy+paste, or send you the program via email or AIM. thanks
 

vinyl409

Member
Joined
Mar 16, 2005
Messages
6
Programming Experience
Beginner
i think i figured it out [i hope]...

VB.NET:
		Dim strWords() As String = {"yams", "baseball", "computer", "jacket", "keyboard", "guitar", "billboard", "highway", "italian", "projector"} 'array for storing the 10 words.
		x = RandomOrder.Next(0, strWords.Length)
		txtWord.Text = strWords(x)
		Call AllLetters()

does this look right? it seems to work correctly...


still having problems on the msgbox upon exit. any help would be appreciated.
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
To display the number of games played and percentage,... you'll need to define a couple of class level variables to keep track of the wins and losses. Then it's just a matter of using the MessageBox class with a simple formula to show that information.
 
Top Bottom