-
Nov 24th, 2014, 01:10 AM
#1
Thread Starter
Addicted Member
Text Replace (If Not statement)
Hey, just thought I'd post this quick question here, I'm making a hang man game but these If Not statements don't do much... Here's the code:
vb Code:
Public Class Form1
Dim GuessedLetters As List(Of Char) = New List(Of Char)
Dim HiddenWord As String
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
HiddenWord = "ABCabc"
GuessedLetters.Add("A")
GuessedLetters.Add("a")
GuessedLetters.Add("B")
GuessedLetters.Add("b")
Label1.Text = GuessedLetters.ToArray
WriteLetters()
End Sub
Private Sub WriteLetters()
Label2.Text = HiddenWord
If Not GuessedLetters.Contains("A") Then Label2.Text.Replace("A", "_")
If Not GuessedLetters.Contains("B") Then Label2.Text.Replace("B", "_")
If Not GuessedLetters.Contains("C") Then Label2.Text.Replace("C", "_")
If Not GuessedLetters.Contains("D") Then Label2.Text.Replace("D", "_")
If Not GuessedLetters.Contains("E") Then Label2.Text.Replace("E", "_")
If Not GuessedLetters.Contains("F") Then Label2.Text.Replace("F", "_")
If Not GuessedLetters.Contains("G") Then Label2.Text.Replace("G", "_")
If Not GuessedLetters.Contains("H") Then Label2.Text.Replace("H", "_")
If Not GuessedLetters.Contains("I") Then Label2.Text.Replace("I", "_")
If Not GuessedLetters.Contains("J") Then Label2.Text.Replace("J", "_")
If Not GuessedLetters.Contains("K") Then Label2.Text.Replace("K", "_")
If Not GuessedLetters.Contains("L") Then Label2.Text.Replace("L", "_")
If Not GuessedLetters.Contains("M") Then Label2.Text.Replace("M", "_")
If Not GuessedLetters.Contains("N") Then Label2.Text.Replace("N", "_")
If Not GuessedLetters.Contains("O") Then Label2.Text.Replace("O", "_")
If Not GuessedLetters.Contains("P") Then Label2.Text.Replace("P", "_")
If Not GuessedLetters.Contains("Q") Then Label2.Text.Replace("Q", "_")
If Not GuessedLetters.Contains("R") Then Label2.Text.Replace("R", "_")
If Not GuessedLetters.Contains("S") Then Label2.Text.Replace("S", "_")
If Not GuessedLetters.Contains("T") Then Label2.Text.Replace("T", "_")
If Not GuessedLetters.Contains("U") Then Label2.Text.Replace("U", "_")
If Not GuessedLetters.Contains("V") Then Label2.Text.Replace("V", "_")
If Not GuessedLetters.Contains("W") Then Label2.Text.Replace("W", "_")
If Not GuessedLetters.Contains("X") Then Label2.Text.Replace("X", "_")
If Not GuessedLetters.Contains("Y") Then Label2.Text.Replace("Y", "_")
If Not GuessedLetters.Contains("Z") Then Label2.Text.Replace("Z", "_")
If Not GuessedLetters.Contains("a") Then Label2.Text.Replace("a", "_")
If Not GuessedLetters.Contains("b") Then Label2.Text.Replace("b", "_")
If Not GuessedLetters.Contains("c") Then Label2.Text.Replace("c", "_")
If Not GuessedLetters.Contains("d") Then Label2.Text.Replace("d", "_")
If Not GuessedLetters.Contains("e") Then Label2.Text.Replace("e", "_")
If Not GuessedLetters.Contains("f") Then Label2.Text.Replace("f", "_")
If Not GuessedLetters.Contains("g") Then Label2.Text.Replace("g", "_")
If Not GuessedLetters.Contains("h") Then Label2.Text.Replace("h", "_")
'and do on... (In my actual code I have the full alphabet twice.)
End Sub
End Class
Thanks in advance. P.S. I"m not using any tutorials so I'm pretty sure this is not the most efficient way. Thanks for your patience.
-
Nov 24th, 2014, 02:15 AM
#2
Re: Text Replace (If Not statement)
If Not GuessedLetters.Contains("A") Then Label2.Text = Label2.Text.Replace("A", "_")
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 24th, 2014, 02:16 AM
#3
Re: Text Replace (If Not statement)
hve a look at my vb.net hangman in my signature
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 24th, 2014, 06:33 AM
#4
Lively Member
Re: Text Replace (If Not statement)
Code:
Dim Alphabet As String = "abcdefghijklmnopqrstuvwxyz"
For i As Integer = 0 To Alphabet.Length - 1
If Not GuessedLetters.Contains(Char.ToUpper(Alphabet(i))) Then Label2.Text = Label2.Text.Replace(Char.ToUpper(Alphabet(i)), "_")
If Not GuessedLetters.Contains(Char.ToLower(Alphabet(i))) Then Label2.Text = Label2.Text.Replace(Char.ToLower(Alphabet(i)), "_")
Next
-
Nov 24th, 2014, 06:56 AM
#5
Re: Text Replace (If Not statement)
Wait... hold on one second... your code doesn't make sense....
If Not GuessedLetters.Contains("A") Then Label2.Text.Replace("A", "_")
If the GuessedLetters list doesn't contain "A" ... then show "A" in the label? Wait... no... that's reversing it...
Ah, there's a FAR easier way to deal with this:
Code:
Private Sub WriteLetters()
For each C as char in GuessedLetters
Label2.Text.Replace(C,"_")
NExt
End Sub
You don't need to check for each letter in the list... just loop through the list, replacing the character found with the underscore.
-tg
-
Nov 24th, 2014, 03:29 PM
#6
Hyperactive Member
Re: Text Replace (If Not statement)
@tg, I too was confused, but I think the logic is if a letter is not one of the letters that was guessed, change any occurrence of that letter in the hidden word to an underscore (to show that that letter hasn't been guessed correctly yet). Thus the only letters that would appear would be those in the hidden word that the person has made a guess on. As such, I think that ShadowTzu has the best code.
@NinjaNic, If I understood what I told tg correctly, then use ShadowTzu's code with a little bit of a change (see my final comment). Also you may think about just using capitalized letters only to save yourself the headache of checking for both cases (and just change the user's guess to Uppercase). Finally, you are doing a lot of String manipulation on a control. It is much better to do your data manipulation on variables first and then display the final piece of data to a control. When you make a change to a Label's Text property, you keep redrawing that control to show the change which incurs a lot of overhead. But you don't care about the drawing of the Label except for the data that it contains. Therefore you should change that data first, and then post it to the control. So slightly changing ShadowTzu's solution, try:
Code:
Dim Alphabet As String = "abcdefghijklmnopqrstuvwxyz"
Dim DisplayedAnswer As String = HiddenWord ' Make a new String so that the original doesn't get corrupted
For i As Integer = 0 To Alphabet.Length - 1
If Not GuessedLetters.Contains(Char.ToUpper(Alphabet(i))) Then DisplayedAnswer = DisplayedAnswer.Replace(Char.ToUpper(Alphabet(i)), "_")
If Not GuessedLetters.Contains(Char.ToLower(Alphabet(i))) Then DisplayedAnswer = DisplayedAnswer.Replace(Char.ToLower(Alphabet(i)), "_")
Next
' After the loop that changes the data has completed, assign the Text of Label2 to the processed String
Label2.Text = DisplayedAnswer
Doing it this way will also prevent the answer to be briefly flashed on the screen as it is processing the data.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|