diff --git a/README.md b/README.md index 24cda80..557d792 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ A terminal-based morse code practice game built with Deno. Improve your morse co - Interactive menu mode (default) - Quick play mode with command-line arguments - Stats viewer + - Morse code translator (text ↔ morse) - Built-in morse code reference ## Installation @@ -64,6 +65,14 @@ deno task start deno run --allow-read --allow-write --allow-env main.ts ``` +The interactive menu provides access to: +- Start a new game +- View statistics +- Morse code reference chart +- Translator tool (convert between text and morse code) +- Reset statistics +- Exit + ### Quick Play Mode Start a game directly with custom settings: @@ -95,6 +104,19 @@ Display a morse code reference chart: deno task start reference ``` +### Translator + +Convert between text and morse code: + +```bash +deno task start +# Then select "Translator" from the menu +``` + +The translator allows bidirectional conversion: +- Text → Morse Code +- Morse Code → Text + ### Reset Statistics ```bash diff --git a/main.ts b/main.ts index 76c866f..c708fbd 100644 --- a/main.ts +++ b/main.ts @@ -12,6 +12,7 @@ import { showGameResults, showStats, showReference, + showTranslator, confirmAction, } from "./ui.ts"; import { createGameSession, isGameComplete } from "./game.ts"; @@ -43,6 +44,9 @@ class MorseGame { case "reference": await this.showReference(); break; + case "translator": + await this.showTranslator(); + break; case "reset": await this.resetStats(); break; @@ -151,6 +155,13 @@ class MorseGame { }); } + /** + * Show translator + */ + async showTranslator(): Promise { + await showTranslator(); + } + /** * Reset statistics */ diff --git a/ui.ts b/ui.ts index 0c58f43..d7f886e 100644 --- a/ui.ts +++ b/ui.ts @@ -42,6 +42,7 @@ export async function showMainMenu(): Promise { { name: "Start New Game", value: "play" }, { name: "View Statistics", value: "stats" }, { name: "Morse Code Reference", value: "reference" }, + { name: "Translator", value: "translator" }, { name: "Reset Statistics", value: "reset" }, { name: "Exit", value: "exit" }, ], @@ -50,6 +51,62 @@ export async function showMainMenu(): Promise { return action; } +/** + * Show translator utility + */ +export async function showTranslator(): Promise { + clearScreen(); + console.log(colors.bold.cyan("\n=== Morse Code Translator ===\n")); + + const direction = await Select.prompt({ + message: "Select translation direction:", + options: [ + { name: "Text → Morse Code", value: "toMorse" }, + { name: "Morse Code → Text", value: "toText" }, + ], + }); + + if (direction === "toMorse") { + const text = await Input.prompt({ + message: "Enter text to translate:", + validate: (value) => { + if (!value.trim()) { + return "Please enter some text"; + } + return true; + }, + }); + + const morse = textToMorse(text.toUpperCase()); + console.log(colors.bold.green("\nTranslation:")); + console.log(colors.yellow(morse)); + } else { + const morse = await Input.prompt({ + message: "Enter Morse code to translate (use dots and dashes):", + validate: (value) => { + if (!value.trim()) { + return "Please enter some Morse code"; + } + return true; + }, + }); + + const text = morseToText(morse); + console.log(colors.bold.green("\nTranslation:")); + console.log(colors.yellow(text)); + } + + console.log(colors.gray("\nPress Enter to continue...")); + await new Promise((resolve) => { + const buf = new Uint8Array(1); + Deno.stdin.setRaw(true); + Deno.stdin.read(buf).then(() => { + Deno.stdin.setRaw(false); + resolve(undefined); + }); + }); +} + /** * Select game mode */