feat: added features, refactored code, updated readme

This commit is contained in:
2025-11-21 16:41:16 +01:00
parent 64119550f0
commit 19f0ccb126
4 changed files with 87 additions and 59 deletions

70
game.ts
View File

@@ -1,6 +1,7 @@
// Game logic for morse code practice
import { textToMorse, compareMorse, normalizeMorse } from "./morse.ts";
import wordsArray from "an-array-of-english-words" with { type: "json" };
export type GameMode =
| "letters"
@@ -38,45 +39,34 @@ const NUMBERS = "0123456789".split("");
const PUNCTUATION = ".,?!-/()@".split("");
const ALPHANUMERIC = [...LETTERS, ...NUMBERS];
const FULL_SET = [...LETTERS, ...NUMBERS, ...PUNCTUATION];
const WORDS = [
"HELLO",
"WORLD",
"CODE",
"TIME",
"GAME",
"TEST",
"PLAY",
"FAST",
"SLOW",
"HELP",
"GOOD",
"BEST",
"NICE",
"COOL",
"MORSE",
"SIGNAL",
"RADIO",
"SEND",
"MESSAGE",
"QUICK",
"LEARN",
"PRACTICE",
"SKILL",
"MASTER",
"EXPERT",
];
const PHRASES = [
"HELLO WORLD",
"GOOD MORNING",
"HOW ARE YOU",
"THANK YOU",
"SEE YOU SOON",
"HAVE A NICE DAY",
"MORSE CODE",
"QUICK BROWN FOX",
"THE END",
"WELL DONE",
];
// Filter words for morse code practice (3-8 letters, common words)
const ALL_WORDS = wordsArray
.filter((word: string) => {
const upper = word.toUpperCase();
return (
word.length >= 3 && word.length <= 8 && /^[A-Z]+$/.test(upper) // Only letters, no special chars
);
})
.map((word: string) => word.toUpperCase());
// Randomly select a subset for variety
const WORDS = ALL_WORDS.sort(() => Math.random() - 0.5).slice(0, 500);
/**
* Generate a random phrase from word combinations
*/
function generatePhrase(): string {
const phraseLength = Math.floor(Math.random() * 3) + 2; // 2-4 words
const selectedWords: string[] = [];
for (let i = 0; i < phraseLength; i++) {
const word = WORDS[Math.floor(Math.random() * WORDS.length)];
selectedWords.push(word);
}
return selectedWords.join(" ");
}
/**
* Get a random challenge based on game mode
@@ -92,7 +82,7 @@ export function getChallenge(mode: GameMode): string {
case "words":
return WORDS[Math.floor(Math.random() * WORDS.length)];
case "phrases":
return PHRASES[Math.floor(Math.random() * PHRASES.length)];
return generatePhrase();
}
}