;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-advanced-reader.ss" "lang")((modname |37.1|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))))) ;;Model ;;Constants ;;The allowed variety of colors for mastermind as a (listof symbols). (define COLORS '(black white red blue green gold pink orange purple navy)) ;;The number of colors. The number of colors must be even. (define COL# (length COLORS)) ;;Data Definition ;;A color is a symbol found in COLORS. ;;State Variables ;;target1, target2 : colors ;;Both target1 and target2 represent the chosen colors for mastermind. They are originally defined to be the first entry in COLORS but their definitions will change after master initializes the state variables. (define target1 (first COLORS)) (define target2 (first COLORS)) ;;guesses : number ;;guesses represent the number of guesses taken to reach the answer for mastermind. (define guesses 0) ;;guess1, guess2 : color ;;Represents the first or second color guessed. (define guess1 (first COLORS)) (define guess2 (first COLORS)) ;;click-number : N ;;Determines whether this is the first click in a pair or the second. If the click-number is 2, we will clear the messages that keep track of which guesses have been made for a pair, and write the new guess in guess1-message. If the click-number is 1, we will not clear anything and fill in guess2-message. (define click-number 2) ;;master : -> void ;;Initializes each of target1 and target2 to a random color in COLORS. Also initializes the number of guesses to 0. (define (master) (begin (set! target1 (random-pick COLORS)) (set! target2 (random-pick COLORS)) (set! guesses 0) (set! guess1 (first COLORS)) (set! guess2 (first COLORS)) (set! click-number 2))) ;random-pick : (listof X) -> XExercise 37.1.4. Modify the color guessing program so that it ;Given alist, randomly choose one item from that list. (define (random-pick alist) (list-ref alist (random (length alist)))) ;; check-guess : color color color color -> symbol ;; Given guess1 and guess2, which are symbols representing the color of squares 1 ;; and 2, respectively, the function lets you check if the guesses are the same ;; as target1 and target2. (define (check-guess guess1 guess2 target1 target2) (cond [(and (symbol=? guess1 target1) (symbol=? guess2 target2)) 'Perfect] [(or (symbol=? guess1 target1) (symbol=? guess2 target2)) 'OneColorAtCorrectPosition] [(or (symbol=? guess1 target2) (symbol=? guess2 target1)) 'OneColorOccurs] [else 'NothingCorrect])) ;master-check : color color -> (list symbol number) ;Returns a list containing a message and a number. The message either indicates that the game is solved given guess1 and guess2, or it reveals a hint. The number indicates the number of guesses taken. ;Effect : Increases the number of guesses by 1 each time. (define (master-check guess1 guess2) (local ((define return-list (begin (set! guesses (add1 guesses)) (list (check-guess guess1 guess2 target1 target2) guesses)))) (cond [(and (symbol=? guess1 target1) (symbol=? guess2 target2)) (begin (master) return-list)] [else return-list]))) ;;View (define guess1-message (make-message "Guess 1")) (define guess2-message (make-message "Guess 2")) (define status-message (make-message "Let's play mastermind!")) (define num-guesses-message (make-message "Guesses: 0")) ;;Controller ;color-callback : color color -> true ;Draws the message associated with the two color guesses (status-message) and also the number of guesses in the appropriate text-box given color1 and color2. ;;button-callback : event -> true ;;Effect: writes guess1 into guess1-message and guess2 into guess2-message. Once the second guess has been made, button-callback will write the hint and number of guesses into status-message and num-guesses-message through color-callback. (define (color-callback color1 color2) (local ((define result-list (master-check color1 color2))) (and (draw-message status-message (symbol->string (first result-list))) (draw-message num-guesses-message (number->string (second result-list)))))) ;make-color-button : N N -> GUI-Item (button) ;Given col and row (0,0 inclusive), make the button for the given color at row, col. ;Effect: Accesses click-number, guess1, guess2 (define (make-color-button col row) (local ((define index (+ col (* (/ COL# 2) row))) (define current-color (list-ref COLORS index)) (define (button-callback event) (cond [(= click-number 2) (begin (set! guess1 current-color) (set! click-number 1) (draw-message guess1-message (symbol->string current-color)) (draw-message guess2-message ""))] [(= click-number 1) (begin (set! guess2 current-color) (set! click-number 2) (draw-message guess2-message (symbol->string current-color)) (color-callback guess1 guess2))]))) (make-button (symbol->string current-color) button-callback))) (define buttons-list (build-list 2 (lambda (y) (build-list (/ COL# 2) (lambda (x) (make-color-button x y)))))) ;init-mastermind : -> true ;Initiates the game mastermind. (define (init-mastermind) (begin (master) (create-window (append buttons-list (list (list guess1-message guess2-message) (list status-message num-guesses-message)))))) (init-mastermind)