chore: claude code refactor (7.5/10)
This commit is contained in:
25
game.ts
25
game.ts
@@ -33,6 +33,11 @@ export interface GameSession {
|
||||
bestStreak: number;
|
||||
}
|
||||
|
||||
// Constants for word filtering
|
||||
const MIN_WORD_LENGTH = 3;
|
||||
const MAX_WORD_LENGTH = 8;
|
||||
const WORD_POOL_SIZE = 500;
|
||||
|
||||
// Character sets for different difficulty levels
|
||||
const LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
|
||||
const NUMBERS = "0123456789".split("");
|
||||
@@ -40,18 +45,32 @@ const PUNCTUATION = ".,?!-/()@".split("");
|
||||
const ALPHANUMERIC = [...LETTERS, ...NUMBERS];
|
||||
const FULL_SET = [...LETTERS, ...NUMBERS, ...PUNCTUATION];
|
||||
|
||||
/**
|
||||
* Fisher-Yates shuffle algorithm - O(n) complexity
|
||||
*/
|
||||
function shuffleArray<T>(array: T[]): T[] {
|
||||
const shuffled = [...array];
|
||||
for (let i = shuffled.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
|
||||
}
|
||||
return shuffled;
|
||||
}
|
||||
|
||||
// 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
|
||||
word.length >= MIN_WORD_LENGTH &&
|
||||
word.length <= MAX_WORD_LENGTH &&
|
||||
/^[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);
|
||||
// Randomly select a subset for variety using Fisher-Yates
|
||||
const WORDS = shuffleArray(ALL_WORDS).slice(0, WORD_POOL_SIZE);
|
||||
|
||||
/**
|
||||
* Generate a random phrase from word combinations
|
||||
|
||||
Reference in New Issue
Block a user