feat: added features, refactored code, updated readme
This commit is contained in:
70
game.ts
70
game.ts
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user