C# Tutorial – Make a Word Shuffle Game In Windows Form and Visual Studio

Hi, in this tutorial we will be making a word shuffle game in windows form. Words will be loaded from an external text file. We will add those words to the text file and load using C#. Each word in the text file will be saved as a list item then we will create a scramble function that will shuffle or scramble the words around in a random order and display it on the screen. There is a text box on the app to enter your  input, it will capture that input and compare it to the original word. If the word captured is correct then we will move on the next word from list and display to the screen or we will add 1 to the guessed integer.

Lesson Objectives:

  1. Make a word shuffle game in windows form using C#
  2. Use Key Press enter to check if the value entered matches the original word
  3. Load external text file
  4. Use list to capture the lines from the text file and save them as items
  5. Create a custom function to take in the text as an argument and scramble the words using New.GUID() method
  6. Keep track of how many times we guessed each word
  7. Show a final game won message when all of the words have been solved.

Video Tutorial

Get Word Shuffle Game on GitHub

Source Code –

namespace Guess_The_Word_Windows_Forms_MOO_ICT
{
    public partial class Form1 : Form
    {

        // created by MOO ICT 2023
        // for educational purpose only

        List<string> words = new List<string>();
        string newText;
        int i = 0;
        int guessed = 0;


        public Form1()
        {
            InitializeComponent();
            Setup();
        }

        private void KeyIsPressed(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (words[i].ToLower() == textBox1.Text.ToLower())
                {
                    if (i < words.Count - 1)
                    {
                        MessageBox.Show("Correct!", "Moo Says: ");
                        textBox1.Text = "";
                        i += 1;
                        newText = Scramble(words[i]);
                        lblWord.Text = newText;
                        lblInfo.Text = "Words: " + (i + 1) + " of " + words.Count;
                        guessed = 0;
                        lblGussed.Text = "Guessed: " + guessed + " times.";
                    }
                    else
                    {
                        lblWord.Text = "You Win, Well done";
                        return;
                    }    
                }
                else
                {
                    guessed += 1;
                    lblGussed.Text = "Guessed: " + guessed + " times.";
                }
                e.Handled = true;
            }
        }

        private void Setup()
        {
            words = File.ReadLines("words.txt").ToList();
            newText = Scramble(words[i]);
            lblWord.Text = newText;
            lblInfo.Text = "Words: " + (i + 1) + " of " + words.Count;
        }

        private string Scramble(string text)
        {
            return new string(text.ToCharArray().OrderBy(x => Guid.NewGuid()).ToArray());
        }
    }
}



Comments are closed.