;; The define-script construct -- does not need to be CPSed

(define abort #f)
(let/cc k (set! abort k))

(define-syntax define-script
  (syntax-rules ()
    [(define-script header body)
     (define header (abort body))]))

;;------------------------------------------------------

;; Mastermind starts here

;; prompt-read-many : list[string] -> list[symbol]
;; returns list of user responses to each prompt in input list
(define (prompt-read-many plist)
  (cond [(empty? plist) empty]
        [(cons? plist) (cons (begin
                               (printf "~a: " (first plist))
                               (read))
                             (prompt-read-many (rest plist)))]))

;; get-user-guess : -> list[symbol]
;; requests user guess for colors in pegs
(define (get-user-guess)
  (prompt-read-many (list "peg1" "peg2" "peg3" "peg4" "peg5")))

;; match? : list[symbol] list[symbol] -> boolean
;; determines whether user has guessed all the pegs correctly
;;   You do NOT need to convert match? to CPS.
(define (match? newguess target)
  (= (length target) (count-match newguess target)))

;; count-match : list[symbol] list[symbol] -> number
;; counts how many pegs user has guessed correctly
(define (count-match guess target)
  (cond [(empty? guess) 0]
	[else
	 (cond [(symbol=? (first guess) (first target))
		(+ 1 (count-match (rest guess) (rest target)))]
	       [else (count-match (rest guess) (rest target))])]))

;; mastermind-loop : list[symbol] -> void
;; plays the simolified mastermind game
(define (mastermind-loop target)
  (let ([newguess (get-user-guess)])
    (cond [(match? newguess target) "You win!"]
	  [else
	   (begin
             (printf "You have ~a pegs right. Try again! ~n" (count-match newguess target))
	     (mastermind-loop target))])))

