Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace WordScramble
- {
- class Program
- {
- static void Main(string[] args)
- {
- string sentence = "reach for the stars";
- string[] sentenceArray = sentence.Split(" ");
- Console.WriteLine("WORD SCRAMBLE\n\nTry to unscramble this message!\n");
- Console.ForegroundColor = ConsoleColor.Red;
- // For each word, scramble it then write it to the console.
- foreach (string word in sentenceArray)
- {
- string scrambledWord;
- do
- {
- scrambledWord = Scramble(word);
- // Check to see if each scrambled word is equal to the starting word
- // If they are not equal then write it to the console
- // If they are equal then go back to loop and scramble it again
- if (word != scrambledWord)
- {
- Console.Write(scrambledWord + " ");
- }
- }
- while (word == scrambledWord);
- }
- Console.ResetColor();
- Console.WriteLine("\n\nPress enter to see the answer...\n");
- Console.ReadLine();
- Console.WriteLine(sentence);
- Console.ReadLine();
- // Outputs something similar to:
- // acher ofr het asrst
- }
- public static string Scramble(string word)
- {
- string output = "";
- List<char> letterArray = new List<char>();
- letterArray.Clear();
- letterArray.AddRange(word);
- Random random = new Random();
- // randomly order the letters in the list<char>
- for (int i = letterArray.Count; i > 0; i--)
- {
- int index = random.Next(letterArray.Count);
- char currentChar = letterArray[index];
- output += currentChar;
- letterArray.RemoveAt(index);
- }
- return output;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement