拼写游戏 - 检查字符串的文本文件 [英] Spelling Game - Checking Text File for String

查看:110
本文介绍了拼写游戏 - 检查字符串的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码到目前为止的拼写游戏(在控制台中),其中生成一个随机单词然后玩家必须在5秒内拼写单词,但我的问题是我不知道如何检查我的答案保存的文本文件,看它是否与实际答案(拼写)匹配。请有人帮忙。



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.





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

推荐答案

这里有几件事会给你带来问题:

1)控制台.ReadLine是一个阻塞调用:在用户按下ENTER之前它不会返回。这意味着我只要我喜欢复制你刚刚打印的单词,这对我来说很有帮助,我可以丢失......所有的Thread.Sleep代码都会阻止应用程序做任何事情。对于这样的应用程序,你真的,真的,想要使用带有Tick事件的计时器来处理输入...



2)你确实意识到如果我在您允许的列表中输入任何单词(例如法老),您的代码会接受它作为您要求我输入的任何其他单词的正确答案吗?



我想你需要在这里思考一下:还有其他(大多数是微妙的)问题会让你以后绊倒你,但是目前这两个问题足以摧毁你的游戏...



为什么你没有使用你检查过的文件来提供输入词和答案?
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?


这篇关于拼写游戏 - 检查字符串的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆