Blob


1 ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 ;; about the language level of this file in a form that our tools can easily process.
3 #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")))))
4 ;;Model
6 ;;Constants
8 ;;The allowed variety of colors for mastermind as a (listof symbols).
9 (define COLORS '(black white red blue green gold pink orange purple navy))
11 ;;The number of colors. The number of colors must be even.
12 (define COL# (length COLORS))
14 ;;Data Definition
15 ;;A color is a symbol found in COLORS.
17 ;;State Variables
18 ;;target1, target2 : colors
19 ;;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.
20 (define target1 (first COLORS))
21 (define target2 (first COLORS))
23 ;;guesses : number
24 ;;guesses represent the number of guesses taken to reach the answer for mastermind.
25 (define guesses 0)
27 ;;guess1, guess2 : color
28 ;;Represents the first or second color guessed.
30 (define guess1 (first COLORS))
31 (define guess2 (first COLORS))
33 ;;click-number : N
34 ;;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.
36 (define click-number 2)
38 ;;master : -> void
39 ;;Initializes each of target1 and target2 to a random color in COLORS. Also initializes the number of guesses to 0.
41 (define (master)
42 (begin (set! target1 (random-pick COLORS))
43 (set! target2 (random-pick COLORS))
44 (set! guesses 0)
45 (set! guess1 (first COLORS))
46 (set! guess2 (first COLORS))
47 (set! click-number 2)))
49 ;random-pick : (listof X) -> XExercise 37.1.4. Modify the color guessing program so that it
50 ;Given alist, randomly choose one item from that list.
52 (define (random-pick alist)
53 (list-ref alist (random (length alist))))
55 ;; check-guess : color color color color -> symbol
56 ;; Given guess1 and guess2, which are symbols representing the color of squares 1
57 ;; and 2, respectively, the function lets you check if the guesses are the same
58 ;; as target1 and target2.
60 (define (check-guess guess1 guess2 target1 target2)
61 (cond
62 [(and (symbol=? guess1 target1)
63 (symbol=? guess2 target2)) 'Perfect]
64 [(or (symbol=? guess1 target1)
65 (symbol=? guess2 target2)) 'OneColorAtCorrectPosition]
66 [(or (symbol=? guess1 target2)
67 (symbol=? guess2 target1)) 'OneColorOccurs]
68 [else 'NothingCorrect]))
70 ;master-check : color color -> (list symbol number)
71 ;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.
72 ;Effect : Increases the number of guesses by 1 each time.
73 (define (master-check guess1 guess2)
74 (local ((define return-list
75 (begin (set! guesses (add1 guesses))
76 (list (check-guess guess1 guess2 target1 target2) guesses))))
77 (cond
78 [(and (symbol=? guess1 target1)
79 (symbol=? guess2 target2)) (begin (master)
80 return-list)]
81 [else return-list])))
83 ;;View
85 (define guess1-message (make-message "Guess 1"))
86 (define guess2-message (make-message "Guess 2"))
87 (define status-message (make-message "Let's play mastermind!"))
88 (define num-guesses-message (make-message "Guesses: 0"))
90 ;;Controller
92 ;color-callback : color color -> true
93 ;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.
95 ;;button-callback : event -> true
96 ;;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.
98 (define (color-callback color1 color2)
99 (local ((define result-list (master-check color1 color2)))
100 (and (draw-message status-message (symbol->string (first result-list)))
101 (draw-message num-guesses-message (number->string (second result-list))))))
103 ;make-color-button : N N -> GUI-Item (button)
104 ;Given col and row (0,0 inclusive), make the button for the given color at row, col.
105 ;Effect: Accesses click-number, guess1, guess2
106 (define (make-color-button col row)
107 (local
108 ((define index (+ col (* (/ COL# 2) row)))
109 (define current-color (list-ref COLORS index))
110 (define (button-callback event)
111 (cond
112 [(= click-number 2)
113 (begin (set! guess1 current-color)
114 (set! click-number 1)
115 (draw-message guess1-message (symbol->string current-color))
116 (draw-message guess2-message ""))]
117 [(= click-number 1)
118 (begin (set! guess2 current-color)
119 (set! click-number 2)
120 (draw-message guess2-message (symbol->string current-color))
121 (color-callback guess1 guess2))])))
122 (make-button (symbol->string current-color) button-callback)))
124 (define buttons-list
125 (build-list 2 (lambda (y)
126 (build-list (/ COL# 2)
127 (lambda (x) (make-color-button x y))))))
129 ;init-mastermind : -> true
130 ;Initiates the game mastermind.
132 (define (init-mastermind)
133 (begin (master)
134 (create-window (append buttons-list
135 (list (list guess1-message guess2-message)
136 (list status-message num-guesses-message))))))
138 (init-mastermind)