feat: added translation, updated readme
This commit is contained in:
22
README.md
22
README.md
@@ -38,6 +38,7 @@ A terminal-based morse code practice game built with Deno. Improve your morse co
|
|||||||
- Interactive menu mode (default)
|
- Interactive menu mode (default)
|
||||||
- Quick play mode with command-line arguments
|
- Quick play mode with command-line arguments
|
||||||
- Stats viewer
|
- Stats viewer
|
||||||
|
- Morse code translator (text ↔ morse)
|
||||||
- Built-in morse code reference
|
- Built-in morse code reference
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
@@ -64,6 +65,14 @@ deno task start
|
|||||||
deno run --allow-read --allow-write --allow-env main.ts
|
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
|
### Quick Play Mode
|
||||||
|
|
||||||
Start a game directly with custom settings:
|
Start a game directly with custom settings:
|
||||||
@@ -95,6 +104,19 @@ Display a morse code reference chart:
|
|||||||
deno task start reference
|
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
|
### Reset Statistics
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
11
main.ts
11
main.ts
@@ -12,6 +12,7 @@ import {
|
|||||||
showGameResults,
|
showGameResults,
|
||||||
showStats,
|
showStats,
|
||||||
showReference,
|
showReference,
|
||||||
|
showTranslator,
|
||||||
confirmAction,
|
confirmAction,
|
||||||
} from "./ui.ts";
|
} from "./ui.ts";
|
||||||
import { createGameSession, isGameComplete } from "./game.ts";
|
import { createGameSession, isGameComplete } from "./game.ts";
|
||||||
@@ -43,6 +44,9 @@ class MorseGame {
|
|||||||
case "reference":
|
case "reference":
|
||||||
await this.showReference();
|
await this.showReference();
|
||||||
break;
|
break;
|
||||||
|
case "translator":
|
||||||
|
await this.showTranslator();
|
||||||
|
break;
|
||||||
case "reset":
|
case "reset":
|
||||||
await this.resetStats();
|
await this.resetStats();
|
||||||
break;
|
break;
|
||||||
@@ -151,6 +155,13 @@ class MorseGame {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show translator
|
||||||
|
*/
|
||||||
|
async showTranslator(): Promise<void> {
|
||||||
|
await showTranslator();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset statistics
|
* Reset statistics
|
||||||
*/
|
*/
|
||||||
|
|||||||
57
ui.ts
57
ui.ts
@@ -42,6 +42,7 @@ export async function showMainMenu(): Promise<string> {
|
|||||||
{ name: "Start New Game", value: "play" },
|
{ name: "Start New Game", value: "play" },
|
||||||
{ name: "View Statistics", value: "stats" },
|
{ name: "View Statistics", value: "stats" },
|
||||||
{ name: "Morse Code Reference", value: "reference" },
|
{ name: "Morse Code Reference", value: "reference" },
|
||||||
|
{ name: "Translator", value: "translator" },
|
||||||
{ name: "Reset Statistics", value: "reset" },
|
{ name: "Reset Statistics", value: "reset" },
|
||||||
{ name: "Exit", value: "exit" },
|
{ name: "Exit", value: "exit" },
|
||||||
],
|
],
|
||||||
@@ -50,6 +51,62 @@ export async function showMainMenu(): Promise<string> {
|
|||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show translator utility
|
||||||
|
*/
|
||||||
|
export async function showTranslator(): Promise<void> {
|
||||||
|
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
|
* Select game mode
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user