|
-
Dec 15th, 2011, 11:29 AM
#1
Thread Starter
New Member
Can someone help with the hangman code?
i'm trying to make a hangman game yet i'm getting errors in these parts:
System.Text.RegularExpressions.Regex.ism...rgx, that it is not part of system .text and also the rgx bit is claiming to need a '<' for an xml tag. can someone help me with those errors and quickly run it on their vb please? this is because i do not have vb on my computer. thanks in anticipation
Code:
Module Module1
Sub Main()
'First thing is to load a dictionary, we'll presume this is a text file with a list of words, where each word is on a separate line (you can download plenty of these kinds of lists from the internet)..
Console.WriteLine("Please enter the file location of the dictionary")
Dim dict As New List(Of String)
dict = System.IO.File.ReadAllLines(Console.ReadLine()).ToList()
' Nowwe 'll find out the maximum number of guesses before the man is hanged, and start the gam with that dict and those rules...
Console.WriteLine("Enter the maxium number of guesses")
Dim si As String = Console.ReadLine()
Dim i As Integer = Convert.ToInt16(si)
Dim guess As Integer
Dim guesses As Integer
Dim fileloc As String
Dim play As String = "y"
While play = "y"
Console.WriteLine("Type 'p' to play again, 't' to teach the ai a new word, or anything else to quit")
Dim inp As String = Console.ReadLine()
If inp = "p" Then
Guess(dict, i)
play = "y"
ElseIf inp = "t" Then
Console.WriteLine("Enter the word to teach the ai")
System.IO.File.AppendAllText(fileloc, vbCrLf & Console.ReadLine())
play = "y"
dict = System.IO.File.ReadAllLines(fileloc).ToList
Else
play = "exit"
End If
End While
Console.WriteLine("Starting game play...! Think of a word...")
Console.WriteLine("Enter the length of the word you are thinking of")
Dim wordLength As Integer = Convert.ToInt16(Console.ReadLine())
'Now we'll remove all the words which aren't that length from the list of possible words...
Dim poss As New List(Of String)
For Each isposs As String In dict
If isposs.Length = wordLength Then
poss.Add(isposs)
End If
Next isposs
'Now we'll start guessing
Dim calp As String() = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
Dim alp As New List(Of String)
alp = calp.ToList()
Dim curWord As New List(Of String)
For len As Integer = 0 To wordLength - 1
curWord.Add("")
Next
For count As Integer = 0 To guesses 'while we still have a guess left
Console.WriteLine("I'm thinking...")
'figure out the most common letter in the remaining poss. words
Dim mostinstances As Integer = 0, mostloc As Integer = 0
Dim instances As Integer = 0, curloc As Integer = -1
For Each az As String In alp
instances = 0
curloc = curloc + 1
Dim caz As Char = Convert.ToChar(az)
For Each w As String In poss
For Each c As Char In w
If c = caz Then
instances = instances + 1
End If
Next c
Next w
If instances > mostinstances Then
mostinstances = instances
mostloc = curloc
End If
Next az
'that means that alp(mostloc) is the most common letter, and therefore what we will guess
Console.WriteLine("I guess the letter " + alp.ElementAt(mostloc) + ". Is this letter in the word [type 'y'], or not [type anything else]?")
If Console.ReadLine() = "y" Then
'the letter is in the string!
Dim morein As Boolean = True
While morein
Console.WriteLine("Please enter the location of the next instance of this letter in the word (where the first letter is 1), or type 0 is there are no more instances")
Dim loc As Integer = Convert.ToInt16(Console.ReadLine())
If loc = 0 Then
morein = False
Else
curWord(loc - 1) = alp.ElementAt(mostloc)
End If
End While
alp.RemoveAt(mostloc)
Dim rgx As String = ""
For Each letter As String In curWord
If (letter = "") Then
rgx = rgx + "[a-zA-Z]"
Else
rgx = rgx + "[" + letter + "]"
End If
Next
Dim newPoss As New List(Of String)
For c As Integer = 0 To poss.Count() - 1
'does this word work with the guessed letters..?
If System.Text.RegularExpressions.Regex.ism...rgx, Text.RegularExpressions.RegexOptions.Ign… Then
newPoss.Add(poss.ElementAt(c))
End If
Next
poss = newPoss
count = count - 1
Else
'the character isn't in the word, so remove all words with the char
Dim newPoss As New List(Of String)
For c As Integer = 0 To poss.Count() - 1
'does this word work with the guessed letters..?
If System.Text.RegularExpressions.Regex.ism… alp.ElementAt(mostloc), Text.RegularExpressions.RegexOptions.Ign… = False Then
newPoss.Add(poss.ElementAt(c))
End If
Next
poss = newPoss
End If
If poss.Count() = 1 Then
'make a guess!
Console.WriteLine("I guess... " + poss.ElementAt(0) + ". Is this correct [tpye 'y'], or incorrect [type anyhing else]")
Dim isCorrect As String = Console.ReadLine()
If isCorrect = "y" Then
Console.WriteLine("I win!")
Return
Else
Console.WriteLine("Bugger!")
Return
End If
ElseIf poss.Count() = 0 Then
Console.WriteLine("I give up")
Return
End If
Dim prev As String = ""
For Each s As String In curWord
If s = "" Then
prev = prev + "_"
Else
prev = prev + s
End If
Next
Console.WriteLine("So far... " + prev)
Next
Console.WriteLine("I lose!")
Return
End Sub
End Module
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
|