0
\$\begingroup\$

In my first game, I am trying to validate whether two words match: whether an input word matches a reference word from an array.

Unfortunately the only word for which it finds a correct match is the last value in the array I created. All the other words report an incorrect match even when input correctly.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class MatchLetters : MonoBehaviour {
    public string word = null;
    public int wordIndex = 0;
    public string alpha;
    public Text WordMatch;
    public Text Question;
    public string name;
    public static string[] quest;
    int num;
    public List<string> Words;

    void Start () {
        TextAsset wordText = Resources.Load<TextAsset> ("Words");

        quest = wordText.text.Split ("\n" [0]);
    }

    void Update () {
       //  Question.text = quest[Random.Range(0, quest.Length)];
    }
    public void AlphabeticFunction(string alphabet){
        wordIndex++;
        word = word + alphabet;
        WordMatch.text = word;
    }

    public void WordsGenerate (){
        name = Question.text = quest[Random.Range(0, quest.Length)];
    }

    public void ValidateAnswer (){

        if (name == WordMatch.text) {
            Question.color = Color.green;
            Debug.Log ("Correct");
        } else {
            Question.color = Color.red;
            Debug.Log ("Incorrect");
        }
    }

    public void ResetAnswer(){
        wordIndex = 0;
        word = "";
        WordMatch.text = word;
    }
}
\$\endgroup\$
  • \$\begingroup\$ Have you walked through the other words character-by-character to see where they differ? My bet it you're splitting on CR characters and leaving stray LFs in your strings \$\endgroup\$ – DMGregory Jan 4 at 16:04
  • \$\begingroup\$ I didn't quite get what you said hahaha, I am so sorry. \$\endgroup\$ – Rolenz Pangan Jan 4 at 16:10
  • \$\begingroup\$ what are CR and LF? \$\endgroup\$ – Rolenz Pangan Jan 4 at 16:10
  • 2
    \$\begingroup\$ Don't underestimate your ability to find answers through your own research \$\endgroup\$ – DMGregory Jan 4 at 16:36
0
\$\begingroup\$

It sounds like you want do to some form of pattern matching. There's many ways to do that. I coded up a quick example. I left out the Unity specifics, but should be able to easily take what I wrote and put modify it for your project.

I created a class that contains a method called CountOfInputThatExist that does the matching. The method is more important. I think that is what you want.

public class WordMatchMaking
{
    // this is the list of words you load from your asset
    // that you called WordText in your code
    public List<string> PossibleWords = new List<string>();

    public int CountOfInputThatExist(string input)
    {
        int matchesFound = 0;

        foreach(string validWord in PossibleWords)
        {
            if (input.Contains(validWord))
                matchesFound++;
        }

        return matchesFound;
    }
}

And here's the code being used:

class Program
{
    static void Main(string[] args)
    {
        WordMatchMaking matchTest = new WordMatchMaking();

        // this is how I initialize my example.  Its similar to
        // your Resources.Load call
        matchTest.PossibleWords.AddRange(new string[] { "cat", "dog", "bird", "fish" });

        string test = "my cat is hungry.";
        Console.WriteLine($"testing '{test}' found {matchTest.CountOfInputThatExist(test)} occurrences.");

        test = "I have a cat and a dog.";
        Console.WriteLine($"testing '{test}' found {matchTest.CountOfInputThatExist(test)} occurrences.");

        test = "The snake ate the fish.";
        Console.WriteLine($"testing '{test}' found {matchTest.CountOfInputThatExist(test)} occurrences.");

        Console.WriteLine("\r\n\r\ntest done...press any key to end application");
        Console.ReadKey();
    }
}

And here's the output:

testing 'my cat is hungry.' found 1 occurrences.
testing 'I have a cat and a dog.' found 2 occurrences.
testing 'The snake ate the fish.' found 1 occurrences.

If you have any questions about this, please ask. I hope it helps. Please remember to mark this as an answer, if you find this helpful.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.