简体   繁体   中英

How to check if user input is the same in dictionary c#

I'm making a game in Visual Studio 2015 UWP C# where the user enters in all the words they can think of beginning with 'Ac' within a time limit and a score variable increments with each word. I have a Dictionary Collection in my C# UWP application containing all of the 'Ac' words in the dictionary. The game works fine, the only thing is that the user can enter in the same word as many times as they want and the score will still increment. Is there any way to deal with duplicate User Input? This is my Code (excluding about 400 of the words in the array to be brief and excluding the DispatcherTimer):

    private void btnEnter_Click(object sender, RoutedEventArgs e)
    {
        Dictionary<string, int> WordsWithAc = new Dictionary<string, int>();
        WordsWithAc.Add("e", 1);
        WordsWithAc.Add("t", 2);
        WordsWithAc.Add("ed", 3);
        WordsWithAc.Add("es", 4);
        WordsWithAc.Add("he", 5);
        WordsWithAc.Add("hy", 6);
        WordsWithAc.Add("id", 7);
        WordsWithAc.Add("me", 8);
        WordsWithAc.Add("ne",9);
        WordsWithAc.Add("re",10); 

        if (GlobalClassAttention.totalRecallScore >= 150)
        {
            btnLevel2.Visibility = Visibility.Visible;
        }

        if (WordsWithAc.ContainsKey(txtUserInput.Text))
        {
            GlobalClassAttention.totalRecallScore += 10;
            txtScore.Text=GlobalClassAttention.totalRecallScore.ToString();
            imgCorrectSign.Visibility = Visibility.Visible;
            imgX.Visibility = Visibility.Collapsed;
            WordsWithAc.Remove(txtUserInput.Text); //Doesn't remove it from the dictionary so the user can enter in the same word more than once
        }
        else
        {
            imgX.Visibility = Visibility.Visible;
            imgCorrectSign.Visibility = Visibility.Collapsed;
        }
    }

    private void btnLevel2_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(TotalRecallLevel2));
    }

wordserror.png

The image shows that the user entered in "Act" twice and the score still incremented, the user shouldn't be able to enter in duplicate words.

I've tried using an array instead but was pointed towards using Dictionary. This is a link to other things I've tried. https://www.codeproject.com/Questions/1167927/How-to-eliminate-duplicate-user-input-within-the-b

Try declaring the dictionary outside of your event function. That won't initialize it every time the button is clicked.

Dictionary<string, int> WordsWithAc = new Dictionary<string, int>();
    WordsWithAc.Add("e", 1);
    WordsWithAc.Add("t", 2);
    WordsWithAc.Add("ed", 3);
    WordsWithAc.Add("es", 4);
    WordsWithAc.Add("he", 5);
    WordsWithAc.Add("hy", 6);
    WordsWithAc.Add("id", 7);
    WordsWithAc.Add("me", 8);
    WordsWithAc.Add("ne",9);
    WordsWithAc.Add("re",10);
private void btnEnter_Click(object sender, RoutedEventArgs e)
{


    if (GlobalClassAttention.totalRecallScore >= 150)
    {
        btnLevel2.Visibility = Visibility.Visible;
    }

    if (WordsWithAc.ContainsKey(txtUserInput.Text))
    {
        GlobalClassAttention.totalRecallScore += 10;
        txtScore.Text=GlobalClassAttention.totalRecallScore.ToString();
        imgCorrectSign.Visibility = Visibility.Visible;
        imgX.Visibility = Visibility.Collapsed;
        WordsWithAc.Remove(txtUserInput.Text); //Doesn't remove it from the dictionary so the user can enter in the same word more than once
    }
    else
    {
        imgX.Visibility = Visibility.Visible;
        imgCorrectSign.Visibility = Visibility.Collapsed;
    }
}

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