cryptogram-game.scm 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. (import
  2. (scheme base)
  3. (scheme read)
  4. (scheme write)
  5. (srfi 1)
  6. (macduffie cipher))
  7. (define puzzle #f)
  8. (define (try-solution key)
  9. (display (apply-cipher mono-decipher #t puzzle key))
  10. (newline))
  11. (define (check-input key)
  12. (define (check-alpha-issues)
  13. (define a (string->list "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
  14. (define b (string->list key))
  15. (null? (filter (lambda (s) (not (member s b))) a)))
  16. (cond
  17. ((< (string-length key) 26)
  18. "Input contains less than 26 letters\n")
  19. ((> (string-length key) 26)
  20. "Input contains more than 26 letters\n")
  21. ((not (check-alpha-issues))
  22. "Input does not contain exactly 1 of each letter\n")
  23. (else
  24. #f)))
  25. (define (main)
  26. (display "Enter the puzzle:\n")
  27. (set! puzzle (read-line))
  28. (display "Enter next key to try (leave blank to quit):\n")
  29. (let loop ()
  30. (define next-key (read-line))
  31. (display "\n")
  32. (when (and (string? next-key)
  33. (> (string-length next-key) 0))
  34. (let ((input-error (check-input next-key)))
  35. (if input-error
  36. (display input-error)
  37. (try-solution next-key))
  38. (display "Enter next key to try: (leave blank to quit)\n")
  39. (loop)))))
  40. (main)