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 string[] inputList = new string[]
- {
- "i'm trying my best ma'am", "i'm over here"
- };
- public static string RandomString()
- {
- Random random = new Random();
- string output = inputList[random.Next(inputList.Length)];
- return output;
- }
- static void Main(string[] args)
- {
- Console.WriteLine("WORD SCRAMBLE\n\nTry to unscramble this message!\n");
- string originalMessage = RandomString();
- WriteMessage(originalMessage);
- Console.WriteLine("\n\nPress enter to see the answer...\n");
- Console.ReadLine();
- Console.WriteLine(originalMessage);
- Console.ReadLine();
- // Outputs something similar to:
- // acher ofr het asrst
- }
- public static void WriteMessage(string message)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- string[] messageArray = message.Split(" ");
- // For each word, scramble it then write it to the console.
- foreach (string word in messageArray)
- {
- 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();
- }
- public static string Scramble(string word)
- {
- string output = "";
- List<char> letterArray = new List<char>();
- letterArray.Clear();
- letterArray.AddRange(word);
- // Remove punctuation and record its index in the array
- int? specialCharIndex;
- if( letterArray.Contains('\''))
- {
- specialCharIndex = letterArray.IndexOf('\'');
- letterArray.RemoveAt((int)specialCharIndex);
- }
- else
- {
- specialCharIndex = null;
- }
- 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, if output contained an ' then add it to the string. TODO - need to add more to catch other special characters
- if (specialCharIndex != null)
- {
- return output.Insert((int)specialCharIndex, "\'");
- }
- else
- {
- return output;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement