Hi, this is my first post in this site. I am having trouble with a hangman program that I've writen. I have got allmost every thing working fine, but I am having trouble ensuring that a word is not used twice. The way in which, my program works is that I have a record array that one past holds words called "word"(filled from a file) and another part stores if that word has been used called "used". I should not need to write to an output file as its only required that the word not be used more than one in one istance of the program. Also when, the user has used all words in the array "words" the program is to display a mesage delcaring this.

Here is sections of my code ( I deleted what is not realed to the problem, to shorten / make the program easier to read). I would apreacete any help:

Global variables :
Const maxwords = 100 ' set the maximum amount of words that can be stored to 100

Structure tthemecont 'declare a record blueprint
Dim words As String ' this array will hold words to guess from a file
Dim used As Boolean ' this array will be used to detrmin if a word has all readt been used
End Structure

Dim generaltheme As String 'these variables are used to store the name of the file to be read from and also used to populate a combo box
Dim userword As String 'this is stored and updated as the user quesses letters
Dim word As String ' This stores the word that has been selected to be guessed
Dim theme(maxwords) As tthemecont 'array of records
Dim lifes As Integer ' this varible will store the amount of guesses the user has to guess the word

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer ' i is a loop control variable

Randomize() ' Initalize the random generator

For i = 0 To maxwords ' loop's through all locations in an array at sets their value to false
theme(i).used = False

FileOpen(1, generaltheme, OpenMode.Input) ' reads words from a file a stores them in an array
i = 0
Do While Not EOF(1) ' Add each line from the file to an arrray
theme(i).words = LineInput(1)
i = i + 1
FileClose(1) 'Close the file
End Sub

Private Sub Btnstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnstart.Click
Dim i As Integer ' i is a loop control variable
Dim randomnumber As Integer ' this variable will store a random number that is generated

randomnumber = Int(Rnd() * 10) ' this is used to generate a random number

If theme(randomnumber).used = False Then ' Used to determin if a word has been not used all ready
word = theme(randomnumber).words 'adds the word at the current random location in an array into a variable called word
theme(randomnumber).used = True ' changes the uses vaule to true at the current random location in a array
End If

From the code, what i understand is when u click the button, the random number is generated and that particular word is pulled out. If it is NOT used then you are taking that word. But what if it is used?? u want the user to click the button again to get "again" a random number? so if all the words are "used" - the user will be kept clicking the button forever wondering why no word is appearing right? I think u need to run a loop here to get the word that is not used along with a condition that "all words" are not yet used. Say if 100 words are there and if all of them are not used then only u should run this loop.

By the way, do remember to check your word file for duplicate words ;)))

This article has been dead for over six months. Start a new discussion instead.