Advertisement
jyoung12387

Word Scramble v1.2

Mar 21st, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace WordScramble
  5. {
  6.     class Program
  7.     {
  8.         static string[] inputList = new string[]
  9.         {
  10.            "i'm trying my best ma'am", "i'm over here"
  11.         };
  12.        
  13.         public static string RandomString()
  14.         {
  15.             Random random = new Random();
  16.  
  17.             string output = inputList[random.Next(inputList.Length)];
  18.  
  19.             return output;
  20.         }
  21.  
  22.        
  23.         static void Main(string[] args)
  24.         {
  25.             Console.WriteLine("WORD SCRAMBLE\n\nTry to unscramble this message!\n");
  26.  
  27.             string originalMessage = RandomString();
  28.             WriteMessage(originalMessage);
  29.  
  30.             Console.WriteLine("\n\nPress enter to see the answer...\n");
  31.             Console.ReadLine();
  32.  
  33.             Console.WriteLine(originalMessage);
  34.             Console.ReadLine();
  35.  
  36.             // Outputs something similar to:
  37.             // acher ofr het asrst
  38.         }
  39.        
  40.         public static void WriteMessage(string message)
  41.         {
  42.             Console.ForegroundColor = ConsoleColor.Red;
  43.  
  44.             string[] messageArray = message.Split(" ");
  45.  
  46.             // For each word, scramble it then write it to the console.
  47.             foreach (string word in messageArray)
  48.             {
  49.                 string scrambledWord;
  50.                 do
  51.                 {
  52.                     scrambledWord = Scramble(word);
  53.  
  54.                     // Check to see if each scrambled word is equal to the starting word
  55.                     // If they are not equal then write it to the console
  56.                     // If they are equal then go back to loop and scramble it again
  57.  
  58.                     if (word != scrambledWord)
  59.                     {
  60.                         Console.Write(scrambledWord + " ");
  61.                     }
  62.                 }
  63.                 while (word == scrambledWord);
  64.             }
  65.  
  66.             Console.ResetColor();
  67.         }
  68.  
  69.         public static string Scramble(string word)
  70.         {
  71.            string output = "";
  72.          
  73.             List<char> letterArray = new List<char>();
  74.             letterArray.Clear();
  75.             letterArray.AddRange(word);
  76.  
  77.             // Remove punctuation and record its index in the array
  78.             int? specialCharIndex;
  79.             if( letterArray.Contains('\''))
  80.             {
  81.                 specialCharIndex = letterArray.IndexOf('\'');
  82.                 letterArray.RemoveAt((int)specialCharIndex);
  83.             }
  84.             else
  85.             {
  86.                 specialCharIndex = null;
  87.             }
  88.  
  89.  
  90.             Random random = new Random();
  91.  
  92.             // randomly order the letters in the list<char>
  93.             for (int i = letterArray.Count; i > 0; i--)
  94.             {
  95.                 int index = random.Next(letterArray.Count);
  96.                 char currentChar = letterArray[index];
  97.  
  98.                 output += currentChar;
  99.  
  100.                 letterArray.RemoveAt(index);
  101.             }
  102.  
  103.             // return output, if output contained an ' then add it to the string. TODO - need to add more to catch other special characters
  104.             if (specialCharIndex != null)
  105.             {
  106.                 return output.Insert((int)specialCharIndex, "\'");
  107.             }
  108.             else
  109.             {
  110.                 return output;
  111.             }
  112.  
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement