如何使列表中的字符串相互转换?

时间:2019-04-25 18:29:27

标签: c# winforms

我有两个带有不同字符串的列表。一个列表包含一种语言的字符串,其他列表包含其翻译。我已经坚持了很长时间(甚至尝试使用Dictionary <>,但随机部分都有问题)

如何使字符串彼此匹配以制作基于记忆单词的游戏?

 Random random = new Random();

        List<string> EngBasicPhrases = new List<string>()
{
    "Hello", "How are you?", "Hot",  "Thank you", "Welcome",
    "Let's go", "My name is...", "Cold", "Good luck",
    "Congratulations", "Bless you","I forgot","Sorry","I'm fine",
    "It's no problem","Don't worry","Here it is","What?","Of course",
    "Boy","Girl","Man","Woman","Friend","Almost","Late"

};

        List<string> FrBasicPhrases = new List<string>()
{
    "Salut","Ca va?","Chaud", "Merci", "Bienvenu", "Allons-y","Je m'appelle","Du froid",
    "Bonne chance","Felicitations","A vos souhaits","J'ai oublie","Desole","Je vais bien",
    "Ce n'est pas grave","Ne t'en fais pas","Voila","Comment?","Bien sur","Un garcon","Une fille",
    "Un home","Une femme","Un ami","Presque","En retard"
};

Button firstClicked, secondClicked;

        public Game()
        {
            InitializeComponent();
            AssignWordsToSquares();
            EngBasicPhrases.AddRange(FrBasicPhrases);
}

 public void AssignWordsToSquares()
        {
            Button button1 = button2;

            int randomNumber;
string[] phrases = EngBasicPhrases.ToArray();
            for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
            {
                if (tableLayoutPanel1.Controls[i] is Button)
                    button1 = (Button)tableLayoutPanel1.Controls[i];
                else
                    continue;

                randomNumber = random.Next(0, EngBasicPhrases.Count - 1);
                button1.Text = phrases[randomNumber];

                EngBasicPhrases.RemoveAt(randomNumber);
            }



            phrases = FrBasicPhrases.ToArray();
            for (int i = 0; i < tableLayoutPanel2.Controls.Count; i++)
            {
                if (tableLayoutPanel2.Controls[i] is Button)
                    button2 = (Button)tableLayoutPanel2.Controls[i];
                else
                    continue;

                randomNumber = random.Next(0, FrBasicPhrases.Count);
                button2.Text = phrases[randomNumber];

                FrBasicPhrases.RemoveAt(randomNumber);
            }
        }

 private void Button_Click(object sender, EventArgs e)
        {
            if (firstClicked != null && secondClicked != null)
                return;

            Button clickedButton = sender as Button;

            if (clickedButton == null)
                return;

            if (clickedButton.ForeColor == Color.Black)
                return;

            if (firstClicked == null)
            {
                firstClicked = clickedButton;
                firstClicked.ForeColor = Color.Black;
                return;
            }

            secondClicked = clickedButton;
            secondClicked.ForeColor = Color.Black;


            CheckForWinner1();

            if (firstClicked.Text == secondClicked.Text)
            {
                firstClicked = null;
                secondClicked = null;
            }
            else
                timer1.Start();
        }

        private void CheckForWinner1()
        {
            Button button;
            for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
            {
                button = tableLayoutPanel1.Controls[i] as Button;

                if (button != null && button.ForeColor == button.BackColor)
                    return;
            }
            MessageBox.Show("Congratulations!");
        }

        private void Button_Click2(object sender, EventArgs e)
        {
            if (firstClicked != null && secondClicked != null)
                return;

            Button clickedButton = sender as Button;

            if (clickedButton == null)
                return;

            if (clickedButton.ForeColor == Color.Black)
                return;

            if (firstClicked == null)
            {
                firstClicked = clickedButton;
                firstClicked.ForeColor = Color.Black;
                return;
            }

            secondClicked = clickedButton;
            secondClicked.ForeColor = Color.Black;

            CheckForWinner2();

            if (firstClicked.Text == secondClicked.Text)
            {
                firstClicked = null;
                secondClicked = null;
            }
            else
                timer1.Start();
        }


        private void CheckForWinner2()
        {
            Button button;


            for (int i = 0; i < tableLayoutPanel2.Controls.Count; i++)
            {
                button = tableLayoutPanel2.Controls[i] as Button;

                if (button != null && button.ForeColor == button.BackColor)
                    return;
            }

            MessageBox.Show("Congratulations!");
        }
  private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();

            firstClicked.ForeColor = firstClicked.BackColor;
            secondClicked.ForeColor = secondClicked.BackColor;

            firstClicked = null;
            secondClicked = null;


        }

1 个答案:

答案 0 :(得分:1)

我建议使用类,因此任务变得微不足道。顺便说一句,由于您使用的是winforms,因此可以使用“标记”属性来存储正确的答案。

我构建了一个小型控制台应用程序以突出我的想法:

using System;
using System.Collections.Generic;

namespace ConsoleApp9
{
    public class GameObject
    {
        public int key { get; set; }
        public string EN { get; set; }
        public string FR { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<string> EngBasicPhrases = new List<string>()
{
    "Hello", "How are you?", "Hot",  "Thank you", "Welcome",
    "Let's go", "My name is...", "Cold", "Good luck",
    "Congratulations", "Bless you","I forgot","Sorry","I'm fine",
    "It's no problem","Don't worry","Here it is","What?","Of course",
    "Boy","Girl","Man","Woman","Friend","Almost","Late"

};
            List<string> FrBasicPhrases = new List<string>()
{
    "Salut","Ca va?","Chaud", "Merci", "Bienvenu", "Allons-y","Je m'appelle","Du froid",
    "Bonne chance","Felicitations","A vos souhaits","J'ai oublie","Desole","Je vais bien",
    "Ce n'est pas grave","Ne t'en fais pas","Voila","Comment?","Bien sur","Un garcon","Une fille",
    "Un home","Une femme","Un ami","Presque","En retard"
};

            List<GameObject> list = new List<GameObject>();
            int max = EngBasicPhrases.Count;

            for (int i = 0; i < max; i++)
            {
                list.Add(new GameObject { key = i, EN = EngBasicPhrases[i], FR = FrBasicPhrases[i] });
            }

            Random rnd = new Random();
            int nextIndex = rnd.Next(max);

            Console.WriteLine($"Guess this: { list[nextIndex].EN}");
            Console.WriteLine($"Youe answer is (type number, press enter):");
            for (int i = 0; i < max; i++)
            {
                Console.WriteLine($"{list[i].key} - { list[i].FR}");
            }
            int answer = int.Parse(Console.ReadLine());

            if(nextIndex == answer)
            {
                Console.WriteLine($"you won a game !");
            }
            else
            {
                Console.WriteLine($"you lost a game ... the correct answer was {list[nextIndex].key} - {list[nextIndex].FR} ");
            }           

            Console.WriteLine($"");
            Console.WriteLine($"Game over");
            Console.ReadKey();
        }
    }
}

我希望对您有帮助