Advertisement
jyoung12387

Word Scramble v1.1

Mar 21st, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace WordScramble
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string sentence = "reach for the stars";
  11.             string[] sentenceArray = sentence.Split(" ");
  12.  
  13.             Console.WriteLine("WORD SCRAMBLE\n\nTry to unscramble this message!\n");
  14.  
  15.             Console.ForegroundColor = ConsoleColor.Red;
  16.  
  17.             // For each word, scramble it then write it to the console.
  18.             foreach (string word in sentenceArray)
  19.             {
  20.                 string scrambledWord;
  21.                 do
  22.                 {
  23.                     scrambledWord = Scramble(word);
  24.  
  25.                     // Check to see if each scrambled word is equal to the starting word
  26.                     // If they are not equal then write it to the console
  27.                     // If they are equal then go back to loop and scramble it again
  28.  
  29.                     if (word != scrambledWord)
  30.                     {
  31.                         Console.Write(scrambledWord + " ");
  32.                     }
  33.                 }
  34.                 while (word == scrambledWord);
  35.             }
  36.  
  37.             Console.ResetColor();
  38.  
  39.             Console.WriteLine("\n\nPress enter to see the answer...\n");
  40.             Console.ReadLine();
  41.  
  42.             Console.WriteLine(sentence);
  43.             Console.ReadLine();
  44.  
  45.             // Outputs something similar to:
  46.             // acher ofr het asrst
  47.         }
  48.  
  49.         public static string Scramble(string word)
  50.         {
  51.            string output = "";
  52.          
  53.                 List<char> letterArray = new List<char>();
  54.                 letterArray.Clear();
  55.                 letterArray.AddRange(word);
  56.  
  57.                 Random random = new Random();
  58.  
  59.                 // randomly order the letters in the list<char>
  60.                 for (int i = letterArray.Count; i > 0; i--)
  61.                 {
  62.                     int index = random.Next(letterArray.Count);
  63.                     char currentChar = letterArray[index];
  64.  
  65.                     output += currentChar;
  66.  
  67.                     letterArray.RemoveAt(index);
  68.                 }
  69.  
  70.             return output;
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement