chickumber / examples / codebreaker / codebreaker.scm

;; 
;; %%HEADER%%
;; 

(use srfi-13 miscmacros srfi-1)

(define *current-secret* #f)

(define (start-game io)
  (display "Welcome to Codebreaker!" io)
  (newline io)
  (display "Enter guess: " io))


(define (guess secret what)
  (let ((exact_matches (exact-match-count secret what))
        (number_matches (number-match-count secret what)))
    (call-with-output-string
     (lambda (out)
       (repeat exact_matches
               (display "+" out))
       (repeat number_matches
               (display "-" out))))))

(define (exact-match-count secret guess)
  (count-matches-for secret guess exact-match?))

(define (number-match-count secret guess)
  (count-matches-for secret guess number-match?))


(define (count-matches-for secret guess pred?)
  (fold (lambda (idx count)
          (+ count (if (pred? secret guess idx) 1 0)))
        0
        (iota 4 0)))

(define (exact-match? guess secret index)
  (and
   (< index (string-length secret))
   (< index (string-length guess))
   (char=? (string-ref guess index) (string-ref secret index))))

(define (number-match? guess secret index)
  (and
   (< index (string-length secret))
   (< index (string-length guess))
   (string-contains secret (->string (string-ref guess index)))
   (not (exact-match? guess secret index))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.