简体   繁体   English

如何检查用户输入是否与文本文件中的单词匹配?

[英]How to check whether user input matches with a word from a text file?

using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using UnityEngine.UI;

public class LetterRandomiser : MonoBehaviour
{
    public char[] characters; 

    public Text textbox; 

    public InputField mainInputField;

    void Start() /*when the game starts*/
    {
        char c = characters[Random.Range(0,characters.Length)];
        textbox.text = c.ToString(); 
    }

    void Update()
    {
        string[] lines = System.IO.File.ReadAllLines(@"C:\Users\lluc\Downloads\words_alpha.txt");

        foreach (string x in lines)
        {
            if (Input.GetKeyDown(KeyCode.Return) 
                && mainInputField.text.Contains(textbox.text) == true
                && mainInputField.text.Contains(x) == true)
            {
                char c = characters[Random.Range(0, characters.Length)];
                textbox.text = c.ToString();
                mainInputField.text = "";
            }
            else if (Input.GetKeyDown(KeyCode.Return) 
                && mainInputField.text.Contains(textbox.text) == false
                && mainInputField.text.Contains(x) == false)
            {
                mainInputField.text = "";
            }
        }
    }
}

Reference to my game参考我的游戏

No errors but when I run the game, it's very laggy.没有错误,但是当我运行游戏时,它非常滞后。 I presume it's because the program is reading the text file, words_alpha.txt, which contains all English words.我认为这是因为程序正在读取包含所有英文单词的文本文件 words_alpha.txt。

However even when it's laggy, when I enter a completely random word which does not match with any word from the text file, the program would still accept that word.然而,即使它是滞后的,当我输入一个与文本文件中的任何词都不匹配的完全随机的词时,程序仍然会接受该词。

Something's wrong with my code...我的代码有问题...

What I want my code to do?我想让我的代码做什么?

  • Accept words which contain the randomly generated letters shown above the input box.接受包含输入框上方显示的随机生成字母的单词。 (which works) (有效)

  • Accept valid words that are in the English dictionary (in my case this is in the form of a text file called words_alpha.txt).接受英语词典中的有效单词(在我的例子中,这是一个名为 words_alpha.txt 的文本文件形式)。 (which doesn't work) (这不起作用)

You should read the file from the Start() method instead of the Update() because the file only needs to be read in once, rather than every frame.您应该从Start()方法而不是Update() Start()方法读取文件,因为文件只需要读取一次,而不是每帧。 This will remove the lag.这将消除滞后。

Also you are looping through the file on every frame which is unnecessary.此外,您还在不必要的每一帧上循环浏览文件。 You should move你应该搬家

if (Input.GetKeyDown(KeyCode.Return))

to outside the foreach loop.foreach循环之外。 And you should probably have another function that is called inside that if statement.并且您可能应该在该if语句中调用另一个函数。 Update() should probably look something more like: Update()应该看起来更像:

void Update()
{
    if (Input.GetKeyDown(KeyCode.Return) &&
        mainInputField.text.Contains(textbox.text))
    {
        wordIsInFile(mainInputField.text);
        //Remaining code that you want to use
    }

    else
    {
        //whatever needs to be done in the else statement
    }
}

Then the function that loops through the array:然后循环遍历数组的函数:

void wordIsInFile(string word)
{
    foreach (var item in lines)
    {
        if (word == item)
        {
            //Perform tasks on word
            //and whatever else
        }
    }
}

Just declare string[] lines outside of Start() , but initialize it in Start() , to ensure that is a global variable.只需在Start()之外声明string[] lines ,但在Start()对其进行初始化,以确保它是一个全局变量。 This will remove the lag, and be more efficient because you are not constantly looping through an array unless the KeyCode is pressed and mainInputField contains some string.这将消除延迟,并提高效率,因为除非按下KeyCode并且mainInputField包含一些字符串,否则您不会不断循环遍历数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM