cipher
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Viewing readonly version of main branch: v504View latest version
Successfully implemented punctuation preservation for Aristocrat ciphers while maintaining punctuation stripping for Patristocrat ciphers.
- Line 148-158: Updated aristocrat encoding to preserve all punctuation and spacing
- Line 174: Fixed TypeScript type annotation for
encodedLetterCounts
Key Logic:
- Aristocrat: Encode only letters (
/[A-Z]/), preserve everything else (spaces, punctuation) - Patristocrat: Strip all non-letters, group into 5-letter blocks (no change from before)
Updated to handle letter-only indexing while displaying punctuation:
visualIndexFor(): Maps group/char position to visual position in encoded stringletterIndexFromVisual(): Converts visual position to letter-only array indexisLetterAtVisual(): Checks if character at position is a letter
handleLetterChange(): Works with letter-only indices, finds matching encoded lettershandleKeyDown(): Backspace handling with letter-only indiceshandleClear(): Clears only letter positionsisDuplicated(): Checks duplicates using letter-only indicesattemptedOriginalFor(): Maps encoded letters to attempted decryptionattemptedEncodedFor(): Maps attempted letters to encoded letters
decryptionAttemptnow only tracks letters:cipher.encoded.replace(/[^A-Z]/g, '')- Input refs array sized for letters only
- Display layer: Shows all characters including punctuation
- Input layer: Only creates input boxes for letters, displays punctuation as text
Comprehensive test suite with 8 tests:
- Aristocrat - preserves punctuation and spacing: Verifies commas, periods, exclamation marks, etc. are preserved
- Aristocrat - letters are properly substituted: Validates correct substitution with apostrophes
- Patristocrat - strips punctuation and groups into 5-letter blocks: Ensures no punctuation in output
- Patristocrat - same substitution but different formatting: Confirms both use same cipher
- Caesar cipher - aristocrat preserves punctuation: Tests Caesar as K1 with empty keyword
- Letter counts - only counts letters: Verifies punctuation doesn't affect frequency analysis
- cleanOriginal - only contains letters: Ensures answer checking works correctly
- Various punctuation marks: Tests hyphens, ellipses, parentheses, etc.
All tests pass! ✅
cipher.encoded: Full string with letters, spaces, and punctuation (aristocrat) or letters + spaces only (patristocrat)decryptionAttempt[]: Array of only letters (no punctuation or spaces)- Mapping: Helper functions convert between visual positions and letter-only array indices
Input: "Hello, World!"
Aristocrat Mode:
cipher.encoded:"KHOOR, ZRUOG!"(letters encoded, punctuation preserved)decryptionAttempt:["", "", "", "", "", "", "", "", "", ""](10 letters only)- User sees:
K H O O R , W R U O G !with inputs only under letters
Patristocrat Mode:
cipher.encoded:"KHOOR ZRUOG"(punctuation stripped, grouped)decryptionAttempt:["", "", "", "", "", "", "", "", "", ""](same 10 letters)- User sees:
KHOOR ZRUOGwith inputs under all characters
- More authentic: Real cryptograms preserve punctuation
- Better hints: Punctuation provides clues about sentence structure
- Cleaner code: Separation between display (with punctuation) and state (letters only)
- Backward compatible: Patristocrat unchanged, existing games work
- Well tested: Comprehensive test coverage ensures correctness
Run tests:
deno test substitutionCipher.test.ts
All 8 tests pass with type checking enabled.