简体   繁体   中英

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.

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). (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. 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. And you should probably have another function that is called inside that if statement. Update() should probably look something more like:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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