From aa35d4a0f9e4fae00adf0b34c74d4d5b36148e9e Mon Sep 17 00:00:00 2001 From: fkDeath Date: Mon, 7 Sep 2026 09:32:28 +0200 Subject: [PATCH] feat(morse): implement morse code translation with audio output Refactor morse code generation to return a dictionary for reuse across methods. Add translateUserSentenceToMorse method that converts user input to morse code and plays audio beeps based on dots and dashes. Pass letterToMorse dictionary through the call chain (askUserSentence -> userTextToAnArray -> translateUserSentenceToMorse) to enable translation functionality. Changes: - Make programmeLetterToMorseCode public and return IDictionary - Add morse code translation method with beep audio feedback - Update method signatures to accept morse code dictionary --- Program.cs | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/Program.cs b/Program.cs index c8d148a..81ce083 100644 --- a/Program.cs +++ b/Program.cs @@ -9,6 +9,7 @@ using System; using System.Linq; +using System.Threading.Tasks.Dataflow; class PI114 { @@ -49,9 +50,10 @@ class PI114 { Console.WriteLine($"Key: {ele.Key}, Value: {ele.Value}"); } - askUserSentence(); + IDictionary letterToMorse = programmeLetterToMorseCode(); + askUserSentence(letterToMorse); } - static void programmeLetterToMorseCode() + public static IDictionary programmeLetterToMorseCode() { Dictionary letterToMorse = new Dictionary(); letterToMorse.Add("a", ".-"); @@ -90,19 +92,51 @@ class PI114 letterToMorse.Add("8", "---.."); letterToMorse.Add("9", "----."); letterToMorse.Add(" ", "/"); + return letterToMorse; } - static void askUserSentence() + static void askUserSentence(IDictionary letterToMorse) { Console.Write("Entrez votre phrase à déchiffrer : "); string userText = Console.ReadLine(); Console.WriteLine(); Console.WriteLine(userText); - userTextToAnArray(userText); + userTextToAnArray(userText, letterToMorse); } - static void userTextToAnArray(string userText) + static void userTextToAnArray(string userText, IDictionary letterToMorse) { string[] userTextArray = userText.Select(x => x.ToString()).ToArray(); + translateUserSentenceToMorse(userTextArray, letterToMorse); + } + static void translateUserSentenceToMorse(string[] userTextArray, IDictionary letterToMorse) + { + Console.WriteLine(string.Join(" ", userTextArray)); + foreach (string k in userTextArray) + { + foreach(KeyValuePair v in letterToMorse) + { + if (k == v.Key) + { + if (v.Value.Contains(".")) + { + var resultCount = v.Value.Count(x => x.ToString() == "."); + for (int i = 0 ; i < resultCount; i++) + { + Console.Beep(1000, 200); + } + } + else if (v.Value.Contains("-")) + { + var resultCount = v.Value.Count(x => x.ToString() == "-"); + for (int i = 0 ; i < resultCount; i++) + { + Console.Beep(1000, 200); + } + } + Console.Write(v.Value + " "); + } + } + } } } \ No newline at end of file