Search code examples
c#unity-game-engineinputcpu-wordcard

How to deal with multiple string values and compare those string values with the user input?


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;

    public string[] lines;

    void Start() 
    {
        char c = characters[Random.Range(0, characters.Length)];
        char d = characters[Random.Range(0, characters.Length)];

        textbox.text = c.ToString() + d.ToString(); 

        lines = System.IO.File.ReadAllLines(@"C:\Users\lluc\Downloads\corncob_lowercase.txt");
    }

    void wordIsInFile(string word)
    {
        foreach (var item in lines)
        {
            if (word == item)
            {
                char c = characters[Random.Range(0, characters.Length)];
                char d = characters[Random.Range(0, characters.Length)];
                textbox.text = c.ToString() + d.ToString();
                mainInputField.text = "";
            }
            else
            {
                mainInputField.text = "";
            }
        }
    }

    void Update()
    { 
        if (Input.GetKeyDown(KeyCode.Return) && mainInputField.text.Contains(textbox.text) == true)
        {
            wordIsInFile(mainInputField.text);
        }
        else if (Input.GetKeyDown(KeyCode.Return) && mainInputField.text.Contains(textbox.text) == false)
        {
            wordIsInFile(mainInputField.text);
        }
    }
}

Reference to my game

My problem:

I want to make it so that the player has to guess a word that contains the two randomly generated letters (shown above the input field).

However as of right now, the player can input a word containing only one out of the two randomly generated letters and the program would still accept their input.

I don't want that to happen. I want to program the game so that it wouldn't accept the player's input until they include both randomly generated letters, not just one.

For example, in the image I am showing you, I can type in 'joker' and it would accept the input and generate a new set of randomly generated letters. However joker only has a 'j' and not a 'z' so it shouldn't be correct. The only words that should be accepted are words containing BOTH J AND Z, i.e. jazz


Solution

  • You can use String.Contains().

    Assuming a group of letters to compare in a string:

    string lettersToCompare;
    

    And that your user input is another string like:

    string userInput;
    

    You can do something like:

    string lettersToCompare;
    string userInput;
    
    bool isMatchingAllCharacters = true;
    //Compare each character of the string
    foreach (char c in lettersToCompare.ToCharArray())
    {
        //if the userInput do not contain this character, you already now it won't match every character.
        if (!userInput.Contains(c.ToString()))
        {
            isMatchingAllCharacters = false;
            break;
        }
    }
    
    if (isMatchingAllCharacters)
    {
        Debug.Log("All matched");
    }
    else
    {
        Debug.Log("Something do not match");
    }
    

    You can avoid the char-string conversions if you use LinQ:

    using System.Linq;
    
    string lettersToCompare;
    string userInput;
    
    bool isMatchingAllCharacters = true;
    //Compare each character of the string
    foreach (char c in lettersToCompare)
    {
        //if the userInput do not contain this character, you already now it won't match every character.
        if (!userInput.Contains(c))
        {
            isMatchingAllCharacters = false;
            break;
        }
    }
    
    if (isMatchingAllCharacters)
    {
        Debug.Log("All matched");
    }
    else
    {
        Debug.Log("Something do not match");
    }