jyoung12387

Word Scramble

Mar 21st, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.47 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.             foreach (string word in sentenceArray)
  18.             {
  19.                 Console.Write(Scramble(word) + " ");
  20.             }
  21.  
  22.             Console.ResetColor();
  23.  
  24.             Console.WriteLine("\n\nPress enter to see the answer...\n");
  25.             Console.ReadLine();
  26.  
  27.             Console.WriteLine(sentence);
  28.             Console.ReadLine();
  29.  
  30.             // Outputs something similar to:
  31.             //
  32.  
  33.         }
  34.  
  35.         public static string Scramble(string word)
  36.         {
  37.             List<char> letterArray = new List<char>();
  38.             letterArray.AddRange(word);
  39.  
  40.             Random random = new Random();
  41.             string output = "";
  42.  
  43.             // randomly order the letters in the list<char>
  44.             for (int i = letterArray.Count; i > 0; i--)
  45.             {
  46.                 int index = random.Next(letterArray.Count);
  47.                 char currentChar = letterArray[index];
  48.  
  49.                 output += currentChar;
  50.  
  51.                 letterArray.RemoveAt(index);
  52.             }
  53.             return output;
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment