Advertisement
Filkolev

MorseCodeChallenge

Apr 8th, 2015
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. namespace MorseCodeChallenge
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Threading;
  7.  
  8.     public class MorseCodeChallenge
  9.     {
  10.         private const int SleepInterval = 750;
  11.         private const int LongBeepDuration = 1200;
  12.         private const int ShortBeepDuration = 600;
  13.         private const int BeepFrequency = 1200;
  14.  
  15.         private static readonly Dictionary<char, string> MorseCodes = new Dictionary<char, string>()
  16.             {
  17.                 {'A', ".-"},
  18.                 {'B', "-..."},
  19.                 {'C', "-.-."},
  20.                 {'D', "-.."},
  21.                 {'E', "."},
  22.                 {'F', "..-."},
  23.                 {'G', "--."},
  24.                 {'H', "...."},
  25.                 {'I', ".."},
  26.                 {'J', ".---"},
  27.                 {'K', "-.-"},
  28.                 {'L', ".-.."},
  29.                 {'M', "--"},
  30.                 {'N', "-."},
  31.                 {'O', "---"},
  32.                 {'P', ".--."},
  33.                 {'Q', "--.-"},
  34.                 {'R', ".-."},
  35.                 {'S', "..."},
  36.                 {'T', "-"},
  37.                 {'U', "..--"},
  38.                 {'V', "...-"},
  39.                 {'W', ".--"},
  40.                 {'X', "-..-"},
  41.                 {'Y', "-.--"},
  42.                 {'Z', "--.."},
  43.                 {' ',  "/"}
  44.             };
  45.  
  46.         public static void Main()
  47.         {
  48.             string message = Console.ReadLine().ToUpper();
  49.  
  50.             foreach (var symbol in message.Where(symbol => MorseCodes.ContainsKey(symbol)))
  51.             {
  52.                 PlaySound(symbol);
  53.             }
  54.         }
  55.  
  56.         private static void PlaySound(char symbol)
  57.         {
  58.             foreach (var sound in MorseCodes[symbol])
  59.             {
  60.                 switch (sound)
  61.                 {
  62.                     case '-':
  63.                         Console.Beep(BeepFrequency, LongBeepDuration);
  64.                         break;
  65.                     case '.':
  66.                         Console.Beep(BeepFrequency, ShortBeepDuration);
  67.                         break;
  68.                     case '/':
  69.                         Thread.Sleep(SleepInterval);
  70.                         break;
  71.                 }
  72.             }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement