Text book code is incorrect, little help please
- Hi folks,
I'm a networking student and hardware is my forte, but am required to do this VB Programming class and this particular project is just about got me ready to pull my hair out! I inputted this code verbatem from the text entitled, "Programming w/Microsoft Visual Basic 2008" by Diane Zak have double & triple checked & this is exactly as is printed in the book on pages 471-473. But it comes up with errors, and then we're supposed to modify this thing so it will accept any word up to 10 letters and only allow letters A-Z to be entered by player 2. But if the original code doesn't work, makes it pretty hard to modify it & make it work! If someone could please show me how to fix the original and some suggestions as to how to make the required modifications I'd be very, very grateful!! Thanks in advance for the help.
Option Explicit On Option Strict On Option Infer Off Public Class frmMain Private Sub mnuFileExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click Me.Close() End Sub Private Sub mnuFileNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileNew.Click 'simulates the Hangman Game Dim strWord As String Dim strLetter As String Dim blnValidWord As Boolean Dim blnDashReplaced As Boolean Dim blnGameOver As Boolean Dim intIncorrect As Integer 'hide the picture boxes picBottom.Visible = False picPost.Visible = False picTop.Visible = False picRope.Visible = False picHead.Visible = False picBody.Visible = False picRightArm.Visible = False picLeftArm.Visible = False picRightLeg.Visible = False picLeftLeg.Visible = False 'get a 5-letter word from player 1, convert to uppercase strWord = InputBox("Enter a 5-letter word:", _ "Hangman Game").ToUpper 'determine whether the word contains 5 letters blnValidWord = True ' assume the word is valid If strWord.Length <> 5 Then blnValidWord = False Else Dim intIndex As Integer Do While intIndex < 5 AndAlso blnValidWord = True If strWord.Substring(intIndex, 1) Like "(!A-Z)" Then blnValidWord = False End If intIndex = intIndex + 1 Loop End If 'if the word is not valid, display a message If blnValidWord = False Then MessageBox.Show("5 letters are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else 'display five dashes in lblWord and clear lblIncorrect lblWord.Text = "-----" lblIncorrect.Text = String.Empty 'get a letter from player 2, convert to uppercase strLetter = InputBox("Enter a letter:", _ "Letter", "", 820, 590).ToUpper End If ' verify that player 2 entered a letter ' and that the game is not over Do While strLetter <> String.Empty _ AndAlso blnGameOver = False 'search the word for the letter For intIndex As Integer = 0 To 4 'if the letter appears int the word, then 'replace the dash in lblWord and 'indicate that a replacement was made If strWord.Substring(intIndex, 1) = strLetter Then Mid(lblWord.Text, intIndex = 1) = strLetter blnDashReplaced = True End If Next intIndex 'determine whether a dash was replaced If blnDashReplaced Then 'if the word does not contain any dashes, 'the game is over because, player 2 'guessed the word; otherwise, reset the 'blnDashReplaced variable for the next search If lblWord.Text.Contains("-") = False Then blnGameOver = True MessageBox.Show("Great guessing", _ "Game Over", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else blnDashReplaced = False End If Else 'processed when no dash was replaced 'display the incorrect letter, then update 'the intIncorrect variable, then show 'the approriate picture box lblIncorrect.Text = _lblIncorrect.Text & " " & strLetter intIncorrect = intIncorrect + 1 Select Case intIncorrect Case 1 picBottom.Visible = True Case 2 picPost.Visible = True Case 3 picTop.Visible = True Case 4 picRope.Visible = True Case 5 picHead.Visible = True Case 6 picBody.Visible = True Case 7 picRightArm.Visible = True Case 8 picLeftArm.Visible = True Case 9 picRightLeg.Visible = True Case 10 picLeftLeg.Visible = True blnGameOver = True MessageBox.Show("Sorry, the word is" _ & strWord & ".", "Game Over", _ MessageBoxButtons.OK, _ MessageBoxIcon.Information) End Select End If ' determine whether to get another letter If blnGameOver = False Then strLetter = InputBox("Enter a letter:", _ "Letter", " ", 820, 590).ToUpper End If Loop End Sub End Class
Answers
- Hi,
at the top of the mnuFileNew_Click method remove the Dim intIndex As String that will eleminate error 1
change this:
If blnGameOver = False Then 'search the word for the letter For intIndex As Integer = 0 To 9 Next 'if the letter appears int the word, then 'replace the dash in lblWord and 'indicate that a replacement was made End If If strWord.Substring(CInt(intIndex), 1) = strLetter Then Mid(lblWord.Text, CInt(CDbl(intIndex) + 1)) = strLetter blnDashReplaced = True End If Next intIndex
to this:
If blnGameOver = False Then 'search the word for the letter For intIndex As Integer = 0 To 9 If strWord.Substring(CInt(intIndex), 1) = strLetter Then Mid(lblWord.Text, CInt(CDbl(intIndex) + 1)) = strLetter blnDashReplaced = True End If Next 'if the letter appears int the word, then 'replace the dash in lblWord and 'indicate that a replacement was made End If
If you have got questions about this, just ask.
Mark the thread as answered if the answer helps you. This helps others who have the same problem !
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/- Marked As Answer byjdrew62 Wednesday, November 04, 2009 8:26 PM
- Hi,
change this:
For intIndex As Integer = 0 To 9 'if the letter appears in the word, then 'replace the dash in lblWord & 'indicate that a replacement was made If strWord.Substring(intIndex, 1) = strLetter Then Mid(lblWord.Text, intIndex + 1) = strLetter blnDashReplaced = True End If Next intIndex
to this:
For intIndex As Integer = 0 To strWord.Length -1 'if the letter appears in the word, then 'replace the dash in lblWord & 'indicate that a replacement was made If strWord.Substring(intIndex, 1) = strLetter Then Mid(lblWord.Text, intIndex + 1) = strLetter blnDashReplaced = True End If Next intIndex
If you have got questions about this, just ask.
Mark the thread as answered if the answer helps you. This helps others who have the same problem !
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/- Marked As Answer byjdrew62 Wednesday, November 04, 2009 8:27 PM
- Just a follow up, I think I have this correct per textbook now. I think you had a error on you first post, I got the code to work per textbook pages, you need to start on page 464 and go through 468 for first version and check it against pages 471 thru 473.
Here is the full cord what I have for the modified lession C #3 I think it is correct:
Option Explicit On Option Strict On Option Infer Off Public Class frmMain Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click Me.Close() End Sub Private Sub mnuFileNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileNew.Click ' simulates the Hangman game Dim strWord As String Dim strLetter As String Dim blnValidWord As Boolean Dim blnDashReplaced As Boolean Dim blnGameOver As Boolean Dim intIncorrect As Integer Dim blnValidLetter As Boolean Dim strWord2 As String ' added to count & change player 1 entry to dashes ' hide the picture boxes picBottom.Visible = False picPost.Visible = False picTop.Visible = False picRope.Visible = False picHead.Visible = False picBody.Visible = False picRightArm.Visible = False picLeftArm.Visible = False picRightLeg.Visible = False picLeftLeg.Visible = False Do ' get a 10-letter word from player 1, convert to uppercase strWord = InputBox("Enter up to a 10-letter word:", _ "Hangman Game").ToUpper 'determine whether the word contains up to 10 letters blnValidWord = True 'assume the word is valid If strWord.Length < 1 Or strWord.Length > 10 Then blnValidWord = False Else Dim intIndex As Integer Do While intIndex < strWord.Length AndAlso blnValidWord = True If strWord.Substring(intIndex, 1) Like "[!A-Z]" Then blnValidWord = False End If intIndex = intIndex + 1 Loop End If 'if the word is not valid, display a message If blnValidWord = False Then MessageBox.Show("1 to 10 letters are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) End If Loop While blnValidWord = False 'Else 'display up to 10 dashes in lblWord and clear lblIncorrect strWord2 = strWord Mid(strWord2, 1, 10) = "----------" lblWord.Text = strWord2 lblIncorrect.Text = String.Empty 'get a letter from player 2, convert to uppercase 'blnValidLetter = True 'assume the letter is valid Do strLetter = InputBox("Enter a letter:", _ "Letter", "", 820, 590).ToUpper blnValidLetter = True 'assume the letter is valid If strLetter Like "[!A-Z]" Then blnValidLetter = False End If 'if the letter is not valid, display a message If blnValidLetter = False Then MessageBox.Show("Letters are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) End If Loop While blnValidLetter = False 'Else 'verify that player 2 entered a letter 'and that the game is not over Do While strLetter <> String.Empty _ AndAlso blnGameOver = False 'search the word for the letter For intIndex As Integer = 0 To strWord.Length - 1 'if the letter appears in the word, then 'replace the dash in lblWord and 'indicate that a replacement was made If strWord.Substring(intIndex, 1) = strLetter Then Mid(lblWord.Text, intIndex + 1) = strLetter blnDashReplaced = True End If Next intIndex 'determine whether a dash was replaced If blnDashReplaced Then 'if the word does not contain any dashes, 'the game is over because player 2 'guessed the word; otherwise, reset the 'blnDashReplaced variable for the next search If lblWord.Text.Contains("-") = False Then blnGameOver = True MessageBox.Show("Great guessing!", _ "Game Over", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else blnDashReplaced = False End If Else 'processed when no dash was replaced 'display the incorrect letter, then update 'the intlncorrect variable, then show ' the appropriate picture box lblIncorrect.Text = _ lblIncorrect.Text & "" & strLetter intIncorrect = intIncorrect + 1 Select Case intIncorrect Case 1 picBottom.Visible = True Case 2 picPost.Visible = True Case 3 picTop.Visible = True Case 4 picRope.Visible = True Case 5 picHead.Visible = True Case 6 picBody.Visible = True Case 7 picRightArm.Visible = True Case 8 picLeftArm.Visible = True Case 9 picRightLeg.Visible = True Case 10 picLeftLeg.Visible = True blnGameOver = True MessageBox.Show("Sorry, the word is " _ & strWord & ".", "Game Over", _ MessageBoxButtons.OK, _ MessageBoxIcon.Information) End Select End If ' determine whether to get another letter If blnGameOver = False Then 'get a letter from player 2, convert to uppercase 'blnValidLetter = True 'assume the letter is valid Do strLetter = InputBox("Enter a letter:", _ "Letter", "", 820, 590).ToUpper blnValidLetter = True 'assume the letter is valid If strLetter Like "[!A-Z]" Then blnValidLetter = False End If 'if the letter is not valid, display a message If blnValidLetter = False Then MessageBox.Show("Letters are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) End If Loop While blnValidLetter = False End If Loop 'End If End Sub Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub End Class
There is a loop error on you last posted version
if player 1 enters an incorrect entry it goes to player 2.
I fixed that.
hope this helps- Marked As Answer byjdrew62 Monday, November 16, 2009 12:16 AM
All Replies
- You haven't mentioned what the error is, so it's a bit hard to offer advice. I can see that
Mid(lblWord.Text, intIndex = 1) = strLetter
Won't compile properly. I presume it shoud be
Mid(lblWord.Text, intIndex - 1) = strLetter
But without knowing what the forms designer components need to be, and without the images for all those picture boxes and without knowing what problems you are seeing, there's not much else to offer. The rest of the code looks OK. - Acamar,
Very sorry, I was so burnt on that last night that I forgot to say what the error was, but you hit it right on the button! Let me give that a try & see if that deals with the issue. Thanks very much for the reply.
Jim - Back again, tried your suggestion and the app did start & showed no errors, which was a huge improvement over what I'd been able to accomplish after about 5 hrs of tinkering with the code! But when I attempted to play the game I came up with the following scenario. I entered "happy" as the 5 letter word, all worked fine. Then entered "p" as first letter guess, the app displayed "pp" as being a correct guess & prompted me to choose another letter, but displayed the "pp" as the first two letters in the word. Next I entered "h" and got this error;
{"Argument 'Start' is not a valid value."} Referring to the line that I had changed.
System.ArgumentException was unhandled Message="Argument 'Start' is not a valid value." Source="Microsoft.VisualBasic" StackTrace: at Microsoft.VisualBasic.CompilerServices.StringType.MidStmtStr(String& sDest, Int32 StartPosition, Int32 MaxInsertLength, String sInsert) at Hangman_Project.frmMain.mnuFileNew_Click(Object sender, EventArgs e) in C:\Documents and Settings\Jim\My Documents\Visual Studio 2008\Projects\VB2008\Chap08\Hangman Game Solution\Hangman Game Project\Main Form.vb:line 87 at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) at Hangman_Project.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:Honestly most of this is greek to me, and my instructor provides little instruction. When I clicked on help for the error in question, I got this window
Troubleshooting Exceptions: System.ArgumentException Updated: July 2008 An ArgumentException exception is thrown when at least one of the arguments provided to a method does not meet the specifications of the parameters of the method.
Still pretty confused & needing help.
Thanks,
Jim
- I read the book I did not have any problem with it, works fine for me...are you using Vista, XP or 7? did you download the picture files? I advice you to try with error and see what happen..........error resume next
Don't judge me, just Upgrade me. Thanks! I'll point out two related problems...
For intIndex As Integer = 0 To 4
becuase intIndex = 0 during the first loop mid is going to try to use startindex = 0
...but there is no zero index in a lenght property, they always start with 1
Mid(lblWord.Text, intIndex - 1) = strLetter
Because you want to select only one char in the string - the syntax you need to use for mid is: mid(string, startindex, lenght) but you are only providing string and startindex arguments (no lenght argument) because thre is an operator rather than a 'delimiter between intIndex and 1 (so your startindex = (intIndex -1)...whcih is especially bad in the first loop when intIndex = 0 *giggle*). You could fix it this way: mid(lblWord.Text, intIndex, 1)- I'm using win xp pro sp3
- Hi jdrew, Acamar,
actually this should probably be <Mid(lblWord.Text, intIndex + 1) = strLetter> (plus instead of minus) as this is the current iteration's addend. Using "-" instead will trigger a runtime error for the first iteration (attempting to change the letter at position -1).
Cheers,
Olaf Hi jdrew, Acamar,
actually this should probably be <Mid(lblWord.Text, intIndex + 1) = strLetter> (plus instead of minus) as this is the current iteration's addend. Using "-" instead will trigger a runtime error for the first iteration (attempting to change the letter at position -1).
Cheers,
Olaf
I hate to do this...but the above comment is not correct.
Mid works like this Mid("string", StartIndex, Lenght)
* The "string" argument lets you specify the string you will work with
* The StartIndex argument is where the work begins. A startIndex of 1 is like putting your curser befor the first char in a string.
* The Lenght argument is how many chars will be selected after StartIndex.
if StartIndex = 1 and Lenght = 1 then you select only the first char.
if StartIndex = 2 and Lenght = 1 then you select only the second char.
if StartIndex = 1 and Lenght = 2 then you select the first two chars.
if the Lenght argument is not specified, all chars after StartIndex are selected.
Here are details on the Mid function: http://msdn.microsoft.com/en-us/library/05e63829.aspx- Hi Derek,
I hate to do this...but the above comment is not correct.
no problem at all here with correcting me when I went wrong - I'm far from perfect and make mistakes all the time! :-)
Mid works like this Mid("string", StartIndex, Lenght)
* The "string" argument lets you specify the string you will work with
* The StartIndex argument is where the work begins. A startIndex of 1 is like putting your curser befor the first char in a string.
* The Lenght argument is how many chars will be selected after StartIndex.
if StartIndex = 1 and Lenght = 1 then you select only the first char.
if StartIndex = 2 and Lenght = 1 then you select only the second char.
if StartIndex = 1 and Lenght = 2 then you select the first two chars.
if the Lenght argument is not specified, all chars after StartIndex are selected.
Here are details on the Mid function: http://msdn.microsoft.com/en-us/library/05e63829.aspx
Alright. Mid() is expecting the StartIndex arg to be 1-based (as opposed to 0-based), thus, the loop in the OP's sample (iterating with 0-4) would throw the reported runtime-error due to the fact that the <Mid(lblWord.Text, intIndex - 1)> (as suggested by Acamar), would result in a -1 for the first iteration's StartIndex-parameter and 0 for the second, both being invalid. With <Mid(lblWord.Text, intIndex + 1)> this will target the first letter for iteration #1 a.s.o.
Hmm. So I read and re-read your post, but I can't really seem to be seeing the difference between what I stated and what you did - help me out, please! :-)
FWIW,
Dim str As String = "12345" MessageBox.Show("Original: " & str & Environment.NewLine & "With Index=2: " & Mid(str, 2))
will give you a "2345" and would give you a runtime-error if it called <Mid(str,0)>.
Cheers,
Olaf Hmm. So I read and re-read your post, but I can't really seem to be seeing the difference between what I stated and what you did - help me out, please! :-)
Sounds like you understand perfectly Olaf!
Bottom line: Mid(lblWord.Text, intIndex + 1) will fix the invalid index exception....but will it make the program work as jdrew62 specified?- Hi Derek,
Sounds like you understand perfectly Olaf!
alright, so - out of curiosity - may I ask exactly what was incorrect in my posting ..?
but will it make the program work as jdrew62 specified?
Since the code is from a book, there's a good chance that this was the only typo, but that's up to jdrew62 to test.
Pretty lengthy thread for such a little typo, too! :-)
Cheers,
Olaf - Mistyping '=' for '+' is much more likely than '=' for '-'.
If blnDashReplaced Then 'if the word doesn't contain any dashes, 'the game is over because player 2 'guessed the word, otherwise, reset the 'blnDashReplaced variable for the next search If lblWord.Text.Contains("-") = False Then blnGameOver = True MessageBox.Show("Great guessing!", _ "Game Over", MessageBoxButtons.OK, _ MessageBoxIcon.Information)At this spot in this code I am getting this error message, {" ArguementException was unhandled.}
(Argument 'Start' is not a valid value.) What does that mean & how can I fix it? Apparently its bad because if I go ahead & run the app, enter a word & then try to select a word it causes a fatal error and crashes the program.
Also at this point shown below, I have an error that says:
Error 1 Value of type 'String' cannot be converted to 'System.Windows.Forms.Label'. 158 25 Hangman Game Project
lblIncorrect.Text & " " & strLetter
Now that one just shows up right away and its just below the other error in the next Else segment. Got any more magic for me Acamar?? Please a little more help!
Thanks in advance!
JimIs the second problem this line:
lblIncorrect.Text = _lblIncorrect.Text & " " & strLetter
If so, it should be :
lblIncorrect.Text = _
lblIncorrect.Text & " " & strLetter
or, better,
lblIncorrect.Text = lblIncorrect.Text & " " & strLetter
For the first one, can you quote the code that uses "Start", becasue I can't find it in what you have posted. The message indicates that you have used the variable in some function where the current value of that variable is not valid. For instance:
lblCorrectText.Text = "Correct Letter = " & " " & Word.Substring(Start,1)
where the current value of Start is -1. That value is meaningless in a Substring function, and would throw that sort of error.- ok here's the two errors that show up initially
' verify that player 2 entered a letter ' and that the game is not over Do While strLetter <> String.Empty If blnGameOver = False Then 'search the word for the letter For intIndex As Integer = 0 To 9 Next 'if the letter appears int the word, then 'replace the dash in lblWord and 'indicate that a replacement was made End IfThe problem here it says is "Error 1 Variable 'intIndex' hides a variable in an enclosing block. " and specifically is with the line that says {For intIndex As Integer = 0 To 9}
The second error that its showing is with this section
If strWord.Substring(CInt(intIndex), 1) = strLetter Then Mid(lblWord.Text, CInt(CDbl(intIndex) + 1)) = strLetter blnDashReplaced = True End If Next intIndex 'determine whether a dash was replaced If blnDashReplaced Then 'if the word does not contain any dashes, 'the game is over because, player 2 'guessed the word; otherwise, reset the 'blnDashReplaced variable for the next search If lblWord.Text.Contains(" - ") = False ThenSpecifically its with this part {Next intIndex} & it says "Error 2 'Next' must be preceded by a matching 'For'."
Does that explain it better?
Here is the complete code as I have it now. The game should accept up to a 10 letter word. The other problem I'm seeing when I run debugging on it is that the number of dashes does coincide with the number of letters in the inputted word.Option Explicit On Option Strict On Option Infer Off Public Class frmMain Private Sub mnuFileExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click Me.Close() End Sub Private Sub mnuFileNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileNew.Click 'simulates the Hangman Game Dim strWord As String Dim strLetter As String Dim blnValidWord As Boolean Dim blnDashReplaced As Boolean Dim blnGameOver As Boolean Dim intIncorrect As Integer Dim intIndex As String 'hide the picture boxes picBottom.Visible = False picPost.Visible = False picTop.Visible = False picRope.Visible = False picHead.Visible = False picBody.Visible = False picRightArm.Visible = False picLeftArm.Visible = False picRightLeg.Visible = False picLeftLeg.Visible = False 'get a 5-letter word from player 1, convert to uppercase strWord = InputBox("Enter up to a 10-letter word:", _ "Hangman Game").ToUpper 'determine whether the word contains 5 letters blnValidWord = True ' assume the word is valid If blnValidWord = False Then MessageBox.Show("10 letters or less are required.", "Hangman Game", MessageBoxButtons.OK, MessageBoxIcon.Information) Else 'display 5 dashes in lblWord and clear lblIncorrect Dim intLen As Integer = strWord.Length 'lblWord.Text = "-----" '<==================================================================== End If If strWord.Length = 10 Then lblWord.Text = "----------" ElseIf strWord.Length = 9 Then lblWord.Text = "---------" ElseIf strWord.Length = 8 Then lblWord.Text = "--------" ElseIf strWord.Length = 7 Then lblWord.Text = "-------" ElseIf strWord.Length = 6 Then lblWord.Text = "------" ElseIf strWord.Length = 5 Then lblWord.Text = "-----" ElseIf strWord.Length = 4 Then lblWord.Text = "----" ElseIf strWord.Length = 3 Then lblWord.Text = "---" ElseIf strWord.Length = 2 Then lblWord.Text = "--" ElseIf strWord.Length = 1 Then lblWord.Text = "-" End If 'lblWord.Text = ("-") 'lblWord.Text = CStr(intLen,"-") lblIncorrect.Text = String.Empty 'if the word is not valid, display a message If blnValidWord = False Then MessageBox.Show("Up to 10 letters are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else 'display five dashes in lblWord and clear lblIncorrect lblWord.Text = "-----" lblIncorrect.Text = String.Empty 'get a letter from player 2, convert to uppercase strLetter = InputBox("Enter a letter:", _ "Letter", "", 820, 590).ToUpper End If ' verify that player 2 entered a letter ' and that the game is not over Do While strLetter <> String.Empty If blnGameOver = False Then 'search the word for the letter For intIndex As Integer = 0 To 9 Next 'if the letter appears int the word, then 'replace the dash in lblWord and 'indicate that a replacement was made End If If strWord.Substring(CInt(intIndex), 1) = strLetter Then Mid(lblWord.Text, CInt(CDbl(intIndex) + 1)) = strLetter blnDashReplaced = True End If Next intIndex 'determine whether a dash was replaced If blnDashReplaced Then 'if the word does not contain any dashes, 'the game is over because, player 2 'guessed the word; otherwise, reset the 'blnDashReplaced variable for the next search If lblWord.Text.Contains(" - ") = False Then blnGameOver = True MessageBox.Show("Great guessing", _ "Game Over", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else blnDashReplaced = False End If Else 'processed when no dash was replaced 'display the incorrect letter, then update 'the intIncorrect variable, then show 'the approriate picture box lblIncorrect.Text = _lblIncorrect.Text & " " & strLetter intIncorrect = intIncorrect + 1 Select Case intIncorrect Case 1 picBottom.Visible = True Case 2 picPost.Visible = True Case 3 picTop.Visible = True Case 4 picRope.Visible = True Case 5 picHead.Visible = True Case 6 picBody.Visible = True Case 7 picRightArm.Visible = True Case 8 picLeftArm.Visible = True Case 9 picRightLeg.Visible = True Case 10 picLeftLeg.Visible = True blnGameOver = True MessageBox.Show("Sorry, the word is" _ & strWord & ".", "Game Over", _ MessageBoxButtons.OK, _ MessageBoxIcon.Information) End Select End If ' determine whether to get another letter If blnGameOver = False Then strLetter = InputBox("Enter a letter:", _ "Letter", " ", 820, 590).ToUpper End If Loop End Sub End ClassThanks again for all your help, you're a life saver on this for me!
Jim
- Hi,
at the top of the mnuFileNew_Click method remove the Dim intIndex As String that will eleminate error 1
change this:
If blnGameOver = False Then 'search the word for the letter For intIndex As Integer = 0 To 9 Next 'if the letter appears int the word, then 'replace the dash in lblWord and 'indicate that a replacement was made End If If strWord.Substring(CInt(intIndex), 1) = strLetter Then Mid(lblWord.Text, CInt(CDbl(intIndex) + 1)) = strLetter blnDashReplaced = True End If Next intIndex
to this:
If blnGameOver = False Then 'search the word for the letter For intIndex As Integer = 0 To 9 If strWord.Substring(CInt(intIndex), 1) = strLetter Then Mid(lblWord.Text, CInt(CDbl(intIndex) + 1)) = strLetter blnDashReplaced = True End If Next 'if the letter appears int the word, then 'replace the dash in lblWord and 'indicate that a replacement was made End If
If you have got questions about this, just ask.
Mark the thread as answered if the answer helps you. This helps others who have the same problem !
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/- Marked As Answer byjdrew62 Wednesday, November 04, 2009 8:26 PM
- Ok I finally have this thing where it shows no errors, until you try to run it and then I have an error with this part here
If blnValidWord = False Then MessageBox.Show("10 letters or less are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else Dim intIndex As Integer Do While intIndex <= 10 AndAlso blnValidWord = True If strWord.Substring(intIndex, 1) Like "[!A-Z]" Then blnValidWord = False End If intIndex = intIndex + 1 Loop End If 'if the word isn't valid, display a messageIt's showing up the the line that contains the SubString syntax, I've tried puttin a "+" & a "-" in front of that 1 & I am still getting the same results. Need someone sharper than me to help figure it out, I'm sure its just a matter of a character or two now.
Thanks to all who have assisted here!
Jim - Did you try changing this line:
Do While intIndex <= 10 AndAlso blnValidWord = True
To this?
Do While intIndex < 10 AndAlso blnValidWord = True
As pointed out before (Heslacher and jdrew62 have it correct just above these posts.), indices are zero based, so a string that is 10 characters long has indices 0 - 9. - Hi Jim,
phew, it seems like whoever wrote the book actually did a bad job with the samples, huh?
Even though there probably is some code that checks to assure there isn't more than 10 characters, there might not be anything that assures that the string has a certain length. Also, even though blnValidWord could be set to False at any time, the loop continues through all letters. A simpler (and IMHO more transparent) approach could be:
If blnValidWord = False Then MessageBox.Show("10 letters or less are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else for each c as char in strWord if c like "[!A-Z]" then blnValidWord = False 'No need to do further iterations, so exit the for-next loop Exit For end if Next End If '...
Cheers,
Olaf - Thanks Olaf & I agree that is a simpler way of doing that part & I did use it.
But it still hangs up when you try to run the app at this point right here
If strWord.Substring(intIndex, 1) = strLetter Then
and when I click on the help for it I'm referred to this page
http://msdn.microsoft.com/en-us/library/wkd6khbd.aspx
I don't know, at this point I'm just going to turn it in as is and move forward, have a new assignment due Monday night that I haven't started on & a test in Linux admin as well to get accomplished this weekend. Will check back if anyone comes up with a fix. I do appreciate everybody's help.
I'll go ahead & post the final(as I have it) full code here now. Thanks again to all for your help!
Jim
Option Explicit On Option Strict On Option Infer Off Public Class frmMain Private Sub mnuExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Me.Close() End Sub Private Sub mnuFileNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileNew.Click ' plays hangman game Dim strWord As String Dim strLetter As String Dim blnValidWord As Boolean Dim blnDashReplaced As Boolean Dim blnGameOver As Boolean Dim intIncorrect As Integer 'hides the picture boxes picBottom.Visible = False picPost.Visible = False picTop.Visible = False picRope.Visible = False picHead.Visible = False picBody.Visible = False picRightArm.Visible = False picLeftArm.Visible = False picLeftLeg.Visible = False picRightLeg.Visible = False 'get a secret word of up to 10-letters from player 1, convert to uppercase strWord = InputBox("Enter a word with up to 10 letters:", _ "Hangman Game").ToUpper 'detemine whether the word contains 10 letters maximum blnValidWord = True 'assume the word is valid 'If strWord.Length <> 5 Then '<================================================================ If strWord.Length > 9 Or strWord.Length < 1 Then blnValidWord = False Else Dim intIndex As Integer 'Do While intIndex < 5 AndAlso blnValidWord = True '<==================== Do While intIndex < strWord.Length AndAlso blnValidWord = True If strWord.Substring(intIndex, 1) Like "[!A-Z]" Then blnValidWord = False End If intIndex = intIndex + 1 Loop End If 'if the word is not valid, display a message If blnValidWord = False Then MessageBox.Show("10 letters or less are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else For Each c As Char In strWord If c Like "[!A-Z]" Then blnValidWord = False 'No need to do further iterations, so exit the for-next loop Exit For End If Next End If 'if the word isn't valid, display a message If blnValidWord = False Then MessageBox.Show("10 letters or less are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else 'disdplay correct number of dashes for word inputted in lblWord 'and clear lblIncorrect Dim intLen As Integer = strWord.Length 'lblWord.Text = "----------" '<==================================================================== If strWord.Length = 10 Then lblWord.Text = "----------" ElseIf strWord.Length = 9 Then lblWord.Text = "---------" ElseIf strWord.Length = 8 Then lblWord.Text = "--------" ElseIf strWord.Length = 7 Then lblWord.Text = "-------" ElseIf strWord.Length = 6 Then lblWord.Text = "------" ElseIf strWord.Length = 5 Then lblWord.Text = "-----" ElseIf strWord.Length = 4 Then lblWord.Text = "----" ElseIf strWord.Length = 3 Then lblWord.Text = "---" ElseIf strWord.Length = 2 Then lblWord.Text = "--" ElseIf strWord.Length = 1 Then lblWord.Text = "-" End If 'lblWord.Text = ("-") 'lblWord.Text = CStr(intLen,"-") lblIncorrect.Text = String.Empty End If 'get a letter from player 2, convert to uppercase strLetter = InputBox("Enter a letter:", _ "Letter", "", 820, 590).ToUpper 'verify that player 2 entered a letter ' and that the game isn't over Do While strLetter <> String.Empty _ AndAlso blnGameOver = False 'search the word for the letter For intIndex As Integer = 0 To 9 'if the letter appears in the word, then 'replace the dash in lblWord & 'indicate that a replacement was made If strWord.Substring(intIndex, 1) = strLetter Then Mid(lblWord.Text, intIndex + 1) = strLetter blnDashReplaced = True End If Next intIndex 'determine if a dash was replaced If blnDashReplaced Then 'if the word doesn't contain any dashes, 'the game is over because player 2 'guessed the word; otherwise, reset the 'blnDashReplaced variable for the next search If lblWord.Text.Contains("-") = False Then blnGameOver = True MessageBox.Show("Great guessing!", _ "Game Over", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else blnDashReplaced = False End If Else 'processed when do dash is replaced 'display the incorrect letter, then update 'the intIncorrect variable, then show 'the appropriate picture box lblIncorrect.Text = _ lblIncorrect.Text & " " & strLetter intIncorrect = intIncorrect + 1 Select Case intIncorrect Case 1 picBottom.Visible = True Case 2 picPost.Visible = True Case 3 picTop.Visible = True Case 4 picRope.Visible = True Case 5 picHead.Visible = True Case 6 picBody.Visible = True Case 7 picRightArm.Visible = True Case 8 picLeftArm.Visible = True Case 9 picRightLeg.Visible = True Case 10 picLeftLeg.Visible = True blnGameOver = True MessageBox.Show("Sorry, the word is " _ & strWord & ".", "Game Over", _ MessageBoxButtons.OK, _ MessageBoxIcon.Information) End Select End If 'determine whether to get another letter If blnGameOver = False Then strLetter = InputBox("Enter a letter:", _ "Letter", "", 820, 590).ToUpper End If Loop End Sub End Class - Hi,
change this:
For intIndex As Integer = 0 To 9 'if the letter appears in the word, then 'replace the dash in lblWord & 'indicate that a replacement was made If strWord.Substring(intIndex, 1) = strLetter Then Mid(lblWord.Text, intIndex + 1) = strLetter blnDashReplaced = True End If Next intIndex
to this:
For intIndex As Integer = 0 To strWord.Length -1 'if the letter appears in the word, then 'replace the dash in lblWord & 'indicate that a replacement was made If strWord.Substring(intIndex, 1) = strLetter Then Mid(lblWord.Text, intIndex + 1) = strLetter blnDashReplaced = True End If Next intIndex
If you have got questions about this, just ask.
Mark the thread as answered if the answer helps you. This helps others who have the same problem !
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/- Marked As Answer byjdrew62 Wednesday, November 04, 2009 8:27 PM
- A very BIG Thank YOU to
Heslacher,
For finally figuring this out for me!!!
That was the magic bullet that put this one to bed for me!!
Many thanks to everyone for their help, support, & suggestions!
Jim
- I'm working on the same textbook problem, I've got a shorter way to enter to correct dashes:
add a variable & change your case statement to thisDim strWord2 As String ' added Else 'display up to 10 dashes in lblWord and clear lblIncorrect strWord2 = strWord Mid(strWord2, 1, 10) = "----------" lblWord.Text = strWord2 lblIncorrect.Text = String.Empty End IfMy question is:
Did you get the second part of the excercise #3 figured out where you have to check player 2 entry to be a letter??? I'm stuck on that part. You need a message box to come up with "letters are req'd" with out adding anything to the error count. this is what I have so foar but can't get it to work correctly.Dim blnValidLetter As Boolean 'added 'get a letter from player 2, convert to uppercase strLetter = InputBox("Enter a letter:", _ "Letter", "", 820, 590).ToUpper blnValidLetter = True 'assume the letter is valid If strLetter Like "[!A-Z]" Then blnValidLetter = False End If 'if the letter is not valid, display a message If blnValidLetter = False Then MessageBox.Show("Letters are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) End If
- Just a follow up, I think I have this correct per textbook now. I think you had a error on you first post, I got the code to work per textbook pages, you need to start on page 464 and go through 468 for first version and check it against pages 471 thru 473.
Here is the full cord what I have for the modified lession C #3 I think it is correct:
Option Explicit On Option Strict On Option Infer Off Public Class frmMain Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click Me.Close() End Sub Private Sub mnuFileNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileNew.Click ' simulates the Hangman game Dim strWord As String Dim strLetter As String Dim blnValidWord As Boolean Dim blnDashReplaced As Boolean Dim blnGameOver As Boolean Dim intIncorrect As Integer Dim blnValidLetter As Boolean Dim strWord2 As String ' added to count & change player 1 entry to dashes ' hide the picture boxes picBottom.Visible = False picPost.Visible = False picTop.Visible = False picRope.Visible = False picHead.Visible = False picBody.Visible = False picRightArm.Visible = False picLeftArm.Visible = False picRightLeg.Visible = False picLeftLeg.Visible = False Do ' get a 10-letter word from player 1, convert to uppercase strWord = InputBox("Enter up to a 10-letter word:", _ "Hangman Game").ToUpper 'determine whether the word contains up to 10 letters blnValidWord = True 'assume the word is valid If strWord.Length < 1 Or strWord.Length > 10 Then blnValidWord = False Else Dim intIndex As Integer Do While intIndex < strWord.Length AndAlso blnValidWord = True If strWord.Substring(intIndex, 1) Like "[!A-Z]" Then blnValidWord = False End If intIndex = intIndex + 1 Loop End If 'if the word is not valid, display a message If blnValidWord = False Then MessageBox.Show("1 to 10 letters are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) End If Loop While blnValidWord = False 'Else 'display up to 10 dashes in lblWord and clear lblIncorrect strWord2 = strWord Mid(strWord2, 1, 10) = "----------" lblWord.Text = strWord2 lblIncorrect.Text = String.Empty 'get a letter from player 2, convert to uppercase 'blnValidLetter = True 'assume the letter is valid Do strLetter = InputBox("Enter a letter:", _ "Letter", "", 820, 590).ToUpper blnValidLetter = True 'assume the letter is valid If strLetter Like "[!A-Z]" Then blnValidLetter = False End If 'if the letter is not valid, display a message If blnValidLetter = False Then MessageBox.Show("Letters are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) End If Loop While blnValidLetter = False 'Else 'verify that player 2 entered a letter 'and that the game is not over Do While strLetter <> String.Empty _ AndAlso blnGameOver = False 'search the word for the letter For intIndex As Integer = 0 To strWord.Length - 1 'if the letter appears in the word, then 'replace the dash in lblWord and 'indicate that a replacement was made If strWord.Substring(intIndex, 1) = strLetter Then Mid(lblWord.Text, intIndex + 1) = strLetter blnDashReplaced = True End If Next intIndex 'determine whether a dash was replaced If blnDashReplaced Then 'if the word does not contain any dashes, 'the game is over because player 2 'guessed the word; otherwise, reset the 'blnDashReplaced variable for the next search If lblWord.Text.Contains("-") = False Then blnGameOver = True MessageBox.Show("Great guessing!", _ "Game Over", MessageBoxButtons.OK, _ MessageBoxIcon.Information) Else blnDashReplaced = False End If Else 'processed when no dash was replaced 'display the incorrect letter, then update 'the intlncorrect variable, then show ' the appropriate picture box lblIncorrect.Text = _ lblIncorrect.Text & "" & strLetter intIncorrect = intIncorrect + 1 Select Case intIncorrect Case 1 picBottom.Visible = True Case 2 picPost.Visible = True Case 3 picTop.Visible = True Case 4 picRope.Visible = True Case 5 picHead.Visible = True Case 6 picBody.Visible = True Case 7 picRightArm.Visible = True Case 8 picLeftArm.Visible = True Case 9 picRightLeg.Visible = True Case 10 picLeftLeg.Visible = True blnGameOver = True MessageBox.Show("Sorry, the word is " _ & strWord & ".", "Game Over", _ MessageBoxButtons.OK, _ MessageBoxIcon.Information) End Select End If ' determine whether to get another letter If blnGameOver = False Then 'get a letter from player 2, convert to uppercase 'blnValidLetter = True 'assume the letter is valid Do strLetter = InputBox("Enter a letter:", _ "Letter", "", 820, 590).ToUpper blnValidLetter = True 'assume the letter is valid If strLetter Like "[!A-Z]" Then blnValidLetter = False End If 'if the letter is not valid, display a message If blnValidLetter = False Then MessageBox.Show("Letters are required.", _ "Hangman Game", MessageBoxButtons.OK, _ MessageBoxIcon.Information) End If Loop While blnValidLetter = False End If Loop 'End If End Sub Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub End Class
There is a loop error on you last posted version
if player 1 enters an incorrect entry it goes to player 2.
I fixed that.
hope this helps- Marked As Answer byjdrew62 Monday, November 16, 2009 12:16 AM


