Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi this is my code so far for a spelling game (in console) where a random word is generated and then the player has to then spell the word in 5 seconds but my problem is that I don't know how to check the text file that my answers save into to see if it matches the actual answer (spelling). Please can someone help.

VB


Option Explicit On<br />
Imports System.IO<br />
Module Example<br />
    Dim CurrentFileWriter As StreamWriter<br />
    Dim CurrentFileReader As StreamReader<br />
    Sub Main()<br />
        Dim stream As New IO.StreamWriter("C:\Users\Conor\Documents\Visual Studio 2010\Spelling Game\Spelling Game\bin\Debug\Answers.txt", False)<br />
        stream.WriteLine("")<br />
        stream.Close()<br />
        Dim FileName, Answers As String<br />
        FileName = "C:\Users\Conor\Documents\Visual Studio 2010\Spelling Game\Spelling Game\bin\Debug\Answers.txt"<br />
        CurrentFileWriter = New StreamWriter(FileName)<br />
        Console.WriteLine("Please Enter this Word:")<br />
        Call WordSpell()<br />
        Call Countdown()<br />
        Answers = Console.ReadLine<br />
        CurrentFileWriter.WriteLine(Answers)<br />
        CurrentFileWriter.Close()<br />
        Console.WriteLine("Checking Answer...")<br />
        For Each FileName As String In IO.File.ReadLines(FileName)<br />
            If FileName.Contains(Answers) Then<br />
                Console.WriteLine("Well Done!")<br />
            Else<br />
                Console.WriteLine("Unlucky Try Again!")<br />
<br />
            End If<br />
    End Sub<br />
    Sub Countdown()<br />
        Threading.Thread.Sleep(1000)<br />
        Console.WriteLine("5")<br />
        Threading.Thread.Sleep(1000)<br />
        Console.WriteLine("4")<br />
        Threading.Thread.Sleep(1000)<br />
        Console.WriteLine("3")<br />
        Threading.Thread.Sleep(1000)<br />
        Console.WriteLine("2")<br />
        Threading.Thread.Sleep(1000)<br />
        Console.WriteLine("1")<br />
        Threading.Thread.Sleep(1000)<br />
    End Sub<br />
    Sub WordSpell()<br />
        Dim rnd As New Random()<br />
        Dim Words() As String = {"accommodate", "chauffeur", "definitely", "fluorescent", "pharaoh",<br />
                                       "unfortunately", "immediately", "ecstasy", "assassination", "environment", "completely"}<br />
        Dim WordIndex As Integer = rnd.Next(Words.Length)<br />
        Console.WriteLine("{0}", Words(WordIndex))<br />
    End Sub<br />
End Module
Posted
Comments
[no name] 20-May-14 14:41pm    
Do you think that checking the name of the file for the answer is the correct place? Should you not check inside the file?

1 solution

There are a couple of things here that are going to give you problems:
1) Console.ReadLine is a blocking call: it does not return until the user presses ENTER. Which means I have as long as I like to copy the word you just printed so helpfully for me and I can;t lose...All your Thread.Sleep code does is stop the application doing anything, at all. For an application like this, you really, really, want to use a timer with a Tick event to handle the input...

2) You do realize that if I type any word in your permitted list (for example "pharaoh") your code will accept it as the correct answer to any other word you ask me to type?

I think you need to think a bit here: there are other (mostly subtle) problems that are going to trip you up later, but at the moment the two above are enough to destroy your game...

Why are you not using the file you check against to supply the input words, as well as the answers?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900