Please Help - Fill the Gaps Game?!
I am making a game, quite simple really, in VB.Net!
There will be a database of phrases.
A Phrase is selected. A Few of the words are RANDOMLY taken out, and the uset must type them in, him/herself. If the character is correct, then it will appear in that gap, and the cursor moves to the next character. eg.
Read Database; HELLO 'One word, from the randomly selected phrase, length: 5 characters.
_____ 'This is 5 Underscores, in place of this word.
'I press the letter G
_____ 'No change, because G is not the first character of this word.
'I press letter H
H____ 'The letter appears, because it is correct. 4 Underscores remain.
'I Press letter E
HE___ 'E appears, all good!
'Press the rest of the letters, L, L, O, etc..
HELLO 'No underscores left, move onto the next word in the phrase!
Just how do I go about this? I want the user to be able to type into text boxes!
Any Help greatly appreciated!
Chembo!!
[1091 byte] By [
chembo] at [2007-11-11 8:47:00]

# 1 Re: Please Help - Fill the Gaps Game?!
The easiest way to do it would be via a console.
The word would be stored in a variable and written and read with a loop till the end of the word.
Create a console application
Here is an example of code:
Dim strWord As String, chrKey As ConsoleKeyInfo, i As Integer = 1, x As Integer
Dim strSolved As String = Nothing
strWord = "Hello"
' loops until the end of the word
Do Until i = (Len(strWord) + 1)
' If correct letters have been guessed then
If Not strSolved = Nothing Then
' write the correct letters that have been guessed
Console.Write(strSolved)
For x = 0 To (Len(strWord) - Len(strSolved))
' Write the rest of the letters as "_"
Console.Write("_")
x = x + 1
Next
Else
For x = 0 To Len(strWord)
Console.Write("_")
x = x + 1
Next
End If
' Writes a blank line for layout purposes
Console.WriteLine()
chrKey = Console.ReadKey
' Clears all text on the console
Console.Clear()
' Checks whether the letter guessed is correct or not
If chrKey.KeyChar = Mid(strWord, i, 1) Then
i += 1
' Adds the correctley guessed letter to the variable
strSolved += chrKey.KeyChar
End If
Loop
All you would do to this code is add a section at the beginning that generates a random number (VB inbuilt function) and selects that number record from your database and assigns the value to the variable (strWord).
I'm presuming you have some experience in VB so this should not be an issue to add in, but if it is let me know
Hope this helps anyway
# 2 Re: Please Help - Fill the Gaps Game?!
If you want to create a GUI rather than a console application, you may want to use a label instead of a textbox. If you use a textbox, you'll have to prevent the user from editing the text, selecting characters, pasting text from the clipboard, etc. With a label, you can simply do this:
Private Word As String = "HELLO"
Private CharPos As Integer = 0
Private Sub frmMain_KeyPress(ByVal sender As Object, _
ByVal e As KeyPressEventArgs) Handles MyBase.KeyPress
' If character is correct for current position...
If e.KeyChar = Word.Chars(CharPos) Then
' ...replace underscore with character
' and increment character position
CharPos += 1
Mid(WordLabel.Text, CharPos, 1) = e.KeyChar
End If
End Sub