logo

c# - Spelling app using threads, methodInvoker, and BeginInvoke

Overview: This code snippet show how to use threads to change characters in four button squares to spell a word moving across the screen. The c# code will be converted to swift for a spelling game where kids whack at goofers to spell a word.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SpellingApp
{
    public partial class Form1 : Form
    {
        List<string> colWords = new List<string>();
        string selWord;
        int alphabet_index = 0;
        Thread threadOne;
        Thread threadTwo;
        Thread threadThree;
        Thread threadFour;

        private void RandomWord()
        {
            Random ran = new Random();
            int index = ran.Next(0, colWords.Count);
            selWord = colWords[index];
            labelWord.Text = selWord;

            txtCharacter1.Visible = false;
            txtCharacter2.Visible = false;
            txtCharacter3.Visible = false;
            txtCharacter4.Visible = false;
            txtCharacter5.Visible = false;

            for (int i = 1; i <= selWord.Length; i++)
            {
                this.Controls["txtCharacter" + i].Visible = true;
                this.Controls["txtCharacter" + i].Text = "";
            }
        }
        public Form1()
        {
            InitializeComponent();

            colWords.Add("cat");
            colWords.Add("dog");
            colWords.Add("rat");
            colWords.Add("kid");
            colWords.Add("mouse");
            colWords.Add("house");
            colWords.Add("heat");
            colWords.Add("sheep");

            RandomWord();

            txtTry.KeyDown += (sender, args) => {
                if (args.KeyCode == Keys.Return)
                {
                    cmdTry.PerformClick();
                }
            };
  
        }
        private void UpdateButtonText(Button cmd, char character)
        {
            MethodInvoker action = delegate
            { cmd.Text = character.ToString(); };
            if (cmd.InvokeRequired)
            {
                cmd.BeginInvoke(action);
            }
        }
        private void UpdateText(TextBox txtBox, char character)
        {
            MethodInvoker action = delegate
            { txtBox.Text = character.ToString(); };
            if (txtBox.InvokeRequired)
            {
                txtBox.BeginInvoke(action);
            }
            else
            {
                txtBox.Text = character.ToString();
            }
        }
        private void MatchLetters(char character)
        {

            int matchIndex = -1;

            matchIndex = selWord.IndexOf(character);

            switch (matchIndex)
            {
                case 0:
                    UpdateText(txtCharacter1, character);
                    break;
                case 1:
                    UpdateText(txtCharacter2, character);
                    break;
                 
                case 2:
                    UpdateText(txtCharacter3, character);
                    break;
                case 3:
                    UpdateText(txtCharacter4, character);
                    break;
                case 4:
                    UpdateText(txtCharacter5, character);
                    break;
            }

            string result = txtCharacter1.Text + txtCharacter2.Text + txtCharacter3.Text + txtCharacter4.Text + txtCharacter5.Text;
            if (result == selWord)
            {
                MessageBox.Show("You Win!");
            }
        }
        private void cmdTry_Click(object sender, EventArgs e)
        {
            MatchLetters(txtTry.Text.ToString()[0]);
            txtTry.Text = "";
            txtTry.Focus();
        }

        private void cmdRandomize_Click(object sender, EventArgs e)
        {
            RandomWord();

        }

        private void Form1_Unload(object sender, EventArgs e)
        {
            threadOne.Abort();
            threadTwo.Abort();
            threadThree.Abort();
            threadFour.Abort();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            RandomWord();

            timer1.Start();

            threadOne = new Thread(new ThreadStart(this.RandomButtonOne));
            threadTwo = new Thread(new ThreadStart(this.RandomButtonTwo));
            threadThree = new Thread(new ThreadStart(this.RandomButtonThree));
            threadFour = new Thread(new ThreadStart(this.RandomButtonFour));


            threadOne.Start();
            threadTwo.Start();
            threadThree.Start();
            threadFour.Start();

        }
        private void RandomButtonOne()
        {
           while(1==1)
            {
                RandomCharacter(cmdOne);

                Thread.Sleep(2500);
            }
         
        }
       
        private void RandomButtonTwo()
        {
            while (1 == 1)
            {
                RandomCharacter(cmdTwo);

                Thread.Sleep(2400);
            }
        }
        private void RandomButtonThree()
        {
            while (1 == 1)
            {
                RandomCharacter(cmdThree);

                Thread.Sleep(2100);
            }

        }
        private void RandomButtonFour()
        {
            while (1 == 1)
            {
                RandomCharacter(cmdFour);

                Thread.Sleep(2300);
            }

        }

      

        private void RandomCharacter(Button cmd)
        {
            
            Random ran = new Random(Guid.NewGuid().GetHashCode());
            //alphabet_index = ran.Next(0, 25);
            

            char[] alphabet = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z' };

            UpdateButtonText(cmd, alphabet[alphabet_index]);
          
            alphabet_index += 1;
            if (alphabet_index > 24) alphabet_index = 0;

       }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            labelWord.Location = new Point(labelWord.Location.X + 5, labelWord.Location.Y);

            if (labelWord.Location.X > this.Width)
            {
                labelWord.Location = new Point(0 - labelWord.Width, labelWord.Location.Y);
       
            }
        }

        private void cmdOne_Click(object sender, EventArgs e)
        {
            MatchLetters(cmdOne.Text.ToString()[0]);
        }

        private void cmdTwo_Click(object sender, EventArgs e)
        {
            MatchLetters(cmdTwo.Text.ToString()[0]);
        }

        private void cmdThree_Click(object sender, EventArgs e)
        {
            MatchLetters(cmdThree.Text.ToString()[0]);
        }

        private void cmdFour_Click(object sender, EventArgs e)
        {
            MatchLetters(cmdFour.Text.ToString()[0]);
        }
    }
}
s