Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #reader(lib "htdp-advanced-reader.ss" "lang")((modname |37.2|) (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 12687dd9 2023-08-04 jrmu ;;Model
5 12687dd9 2023-08-04 jrmu
6 12687dd9 2023-08-04 jrmu ;A word is a (listof letters) where letters is a symbol from 'a ... 'z and '_.
7 12687dd9 2023-08-04 jrmu
8 12687dd9 2023-08-04 jrmu ;;Constants
9 12687dd9 2023-08-04 jrmu
10 12687dd9 2023-08-04 jrmu (define WORDS '((o c t o p u s)
11 12687dd9 2023-08-04 jrmu (s q u i d)
12 12687dd9 2023-08-04 jrmu (s a l m o n)
13 12687dd9 2023-08-04 jrmu (t i l a p i a)
14 12687dd9 2023-08-04 jrmu (b a s s)
15 12687dd9 2023-08-04 jrmu (s h r i m p)
16 12687dd9 2023-08-04 jrmu (c l a m s)
17 12687dd9 2023-08-04 jrmu (m u s s e l)
18 12687dd9 2023-08-04 jrmu (o y s t e r)
19 12687dd9 2023-08-04 jrmu (c r a b)
20 12687dd9 2023-08-04 jrmu (s t a r f i s h)
21 12687dd9 2023-08-04 jrmu (j e l l y f i s h)
22 12687dd9 2023-08-04 jrmu (s e a l i o n)
23 12687dd9 2023-08-04 jrmu (t u n a)
24 12687dd9 2023-08-04 jrmu (d o l p h i n)
25 12687dd9 2023-08-04 jrmu (w h a l e)
26 12687dd9 2023-08-04 jrmu (k e l p)
27 12687dd9 2023-08-04 jrmu (m a n a t e e)))
28 12687dd9 2023-08-04 jrmu
29 12687dd9 2023-08-04 jrmu ;;The alphabet
30 12687dd9 2023-08-04 jrmu (define LETTERS '(a b c d e f g h i j k l m n o p q r s t u v w x y z))
31 12687dd9 2023-08-04 jrmu
32 12687dd9 2023-08-04 jrmu ;;The number of words
33 12687dd9 2023-08-04 jrmu (define WORDS# (length WORDS))
34 12687dd9 2023-08-04 jrmu
35 12687dd9 2023-08-04 jrmu ;;A body-part is one of the following symbols:
36 12687dd9 2023-08-04 jrmu (define PARTS '(noose head body right-arm left-arm right-leg left-leg))
37 12687dd9 2023-08-04 jrmu
38 12687dd9 2023-08-04 jrmu ;;State Variables
39 12687dd9 2023-08-04 jrmu
40 12687dd9 2023-08-04 jrmu ;;chosen-word : word
41 12687dd9 2023-08-04 jrmu ;;This word is the target word that the player needs to guess
42 12687dd9 2023-08-04 jrmu
43 12687dd9 2023-08-04 jrmu (define chosen-word (first WORDS))
44 12687dd9 2023-08-04 jrmu
45 12687dd9 2023-08-04 jrmu ;;status-word : word
46 12687dd9 2023-08-04 jrmu ;;This word represents the current status of the player's guesses
47 12687dd9 2023-08-04 jrmu
48 12687dd9 2023-08-04 jrmu (define status-word (first WORDS))
49 12687dd9 2023-08-04 jrmu
50 12687dd9 2023-08-04 jrmu ;;body-parts-left : (listof body-parts)
51 12687dd9 2023-08-04 jrmu ;;Indicates how many body-parts are left before the hangman is dead.
52 12687dd9 2023-08-04 jrmu
53 12687dd9 2023-08-04 jrmu (define body-parts-left PARTS)
54 12687dd9 2023-08-04 jrmu
55 12687dd9 2023-08-04 jrmu ;;previous-guesses : word
56 12687dd9 2023-08-04 jrmu ;;Keeps track of all previous guesses.
57 12687dd9 2023-08-04 jrmu
58 12687dd9 2023-08-04 jrmu (define previous-guesses empty)
59 12687dd9 2023-08-04 jrmu
60 12687dd9 2023-08-04 jrmu ;make-status-word : word -> word
61 12687dd9 2023-08-04 jrmu ;Given aword, creates an equally long word consisting only of the letter '_.
62 12687dd9 2023-08-04 jrmu
63 12687dd9 2023-08-04 jrmu (define (make-status-word aword)
64 12687dd9 2023-08-04 jrmu (build-list (length aword) (lambda (x) '_)))
65 12687dd9 2023-08-04 jrmu
66 12687dd9 2023-08-04 jrmu ;;hangman : -> void
67 12687dd9 2023-08-04 jrmu ;;Initiates the hangman program by selecting the chosen word and resetting the status word and the number of body-parts left.
68 12687dd9 2023-08-04 jrmu
69 12687dd9 2023-08-04 jrmu (define (hangman)
70 12687dd9 2023-08-04 jrmu (begin (set! chosen-word (list-ref WORDS (random WORDS#)))
71 12687dd9 2023-08-04 jrmu (set! status-word (make-status-word chosen-word))
72 12687dd9 2023-08-04 jrmu (set! body-parts-left PARTS)))
73 12687dd9 2023-08-04 jrmu
74 12687dd9 2023-08-04 jrmu ;;Initializes the state variables
75 12687dd9 2023-08-04 jrmu (hangman)
76 12687dd9 2023-08-04 jrmu
77 12687dd9 2023-08-04 jrmu ;A response is either
78 12687dd9 2023-08-04 jrmu ; 1. "You won"
79 12687dd9 2023-08-04 jrmu ; 2. (list "The End" body-part word)
80 12687dd9 2023-08-04 jrmu ; 3. (list "Good guess!" word)
81 12687dd9 2023-08-04 jrmu ; 4. (list "Sorry" body-part word)
82 12687dd9 2023-08-04 jrmu
83 12687dd9 2023-08-04 jrmu ;hangman-guess : letter -> response
84 12687dd9 2023-08-04 jrmu ;If aletter is present in chosen-word but not in status-word, (effect) update status-word. Otherwise, shorten body-part-list. In all cases, output one of the four possible responses. Also effect the update of previous-guesses.
85 12687dd9 2023-08-04 jrmu
86 12687dd9 2023-08-04 jrmu (define (hangman-guess aletter)
87 12687dd9 2023-08-04 jrmu (local ((define updated-status (reveal-word chosen-word status-word aletter)))
88 12687dd9 2023-08-04 jrmu (cond
89 12687dd9 2023-08-04 jrmu [(contains previous-guesses aletter) "You have used this guess before."]
90 12687dd9 2023-08-04 jrmu [else
91 12687dd9 2023-08-04 jrmu (begin
92 12687dd9 2023-08-04 jrmu (set! previous-guesses (cons aletter previous-guesses))
93 12687dd9 2023-08-04 jrmu (cond
94 12687dd9 2023-08-04 jrmu [(equal? status-word updated-status)
95 12687dd9 2023-08-04 jrmu (local ((define lost-part (first body-parts-left)))
96 12687dd9 2023-08-04 jrmu (begin (set! body-parts-left (rest body-parts-left))
97 12687dd9 2023-08-04 jrmu (cond
98 12687dd9 2023-08-04 jrmu [(empty? body-parts-left) (list "The End" lost-part chosen-word)]
99 12687dd9 2023-08-04 jrmu [else (list "Sorry" lost-part status-word)])))]
100 12687dd9 2023-08-04 jrmu [else (begin (set! status-word updated-status)
101 12687dd9 2023-08-04 jrmu (cond
102 12687dd9 2023-08-04 jrmu [(equal? status-word chosen-word) "You won"]
103 12687dd9 2023-08-04 jrmu [else (list "Good guess!" status-word)]))]))])))
104 12687dd9 2023-08-04 jrmu
105 12687dd9 2023-08-04 jrmu ;reveal-word: word word letter -> word
106 12687dd9 2023-08-04 jrmu ;Given chosen-word, status-word, and aletter, return an updated status-word where '_ is replaced by aletter for all letters in chosen-word that are aletter.
107 12687dd9 2023-08-04 jrmu
108 12687dd9 2023-08-04 jrmu (define (reveal-word chosen-word status-word aletter)
109 12687dd9 2023-08-04 jrmu (local ((define (reveal-letter chosen-letter status-letter)
110 12687dd9 2023-08-04 jrmu (cond
111 12687dd9 2023-08-04 jrmu [(equal? chosen-letter aletter) chosen-letter]
112 12687dd9 2023-08-04 jrmu [else status-letter])))
113 12687dd9 2023-08-04 jrmu (map reveal-letter chosen-word status-word)))
114 12687dd9 2023-08-04 jrmu
115 12687dd9 2023-08-04 jrmu ;contains : (listof X) X -> boolean
116 12687dd9 2023-08-04 jrmu ;Determine if alox contains anx
117 12687dd9 2023-08-04 jrmu (define (contains alox anx)
118 12687dd9 2023-08-04 jrmu (ormap (lambda (x) (equal? x anx)) alox))
119 12687dd9 2023-08-04 jrmu ;Exercise 37.2.4. Formulate the four examples for hangman-guess as boolean-valued expressions that produce true if hangman-guess is correct. Develop an additional example for each case; turn these new examples into additional tests. Solution
120 12687dd9 2023-08-04 jrmu
121 12687dd9 2023-08-04 jrmu #|
122 12687dd9 2023-08-04 jrmu ;Tests
123 12687dd9 2023-08-04 jrmu ;test-hangman : letter word X -> boolean
124 12687dd9 2023-08-04 jrmu ;Tests hangman-guess and returns true if the test is successful. Consumes guess (a letter), status (a word), partsleft (listof body-parts), and response (one of the four responses).
125 12687dd9 2023-08-04 jrmu (define (test-hangman guess status partsleft response)
126 12687dd9 2023-08-04 jrmu (begin (set! chosen-word '(b a l l))
127 12687dd9 2023-08-04 jrmu (set! status-word status)
128 12687dd9 2023-08-04 jrmu (set! body-parts-left partsleft)
129 12687dd9 2023-08-04 jrmu (equal? (hangman-guess guess) response)))
130 12687dd9 2023-08-04 jrmu
131 12687dd9 2023-08-04 jrmu
132 12687dd9 2023-08-04 jrmu
133 12687dd9 2023-08-04 jrmu (and (test-hangman 'l '(b _ _ _) '(arm leg) '("Good guess!" (b _ l l)))
134 12687dd9 2023-08-04 jrmu (equal? status-word '(b _ l l)))
135 12687dd9 2023-08-04 jrmu (test-hangman 'a '(b _ l l) '(arm leg) "You won")
136 12687dd9 2023-08-04 jrmu (and (test-hangman 'l '(b _ l l) '(right-leg left-leg) '("Sorry" right-leg (b _ l l)))
137 12687dd9 2023-08-04 jrmu (equal? body-parts-left '(left-leg)))
138 12687dd9 2023-08-04 jrmu (and (test-hangman 'l '(b _ l l) '(left-leg) '("The End" (b a l l)))
139 12687dd9 2023-08-04 jrmu (equal? body-parts-left empty))
140 12687dd9 2023-08-04 jrmu |#
141 12687dd9 2023-08-04 jrmu
142 12687dd9 2023-08-04 jrmu ;;View
143 12687dd9 2023-08-04 jrmu
144 12687dd9 2023-08-04 jrmu ;word->string : word -> string
145 12687dd9 2023-08-04 jrmu ;Given a word, convert it to a string.
146 12687dd9 2023-08-04 jrmu
147 12687dd9 2023-08-04 jrmu (define (word->string aword)
148 12687dd9 2023-08-04 jrmu (foldr string-append "" (map (lambda (aletter) (symbol->string aletter)) aword)))
149 12687dd9 2023-08-04 jrmu
150 12687dd9 2023-08-04 jrmu ;;GUI-items
151 12687dd9 2023-08-04 jrmu (define guess-message (make-message "Guess: "))
152 12687dd9 2023-08-04 jrmu (define guess-choice (make-choice (map (lambda (aletter) (symbol->string aletter)) LETTERS)))
153 12687dd9 2023-08-04 jrmu (define status-message (make-message "Status: "))
154 12687dd9 2023-08-04 jrmu (define status-word-message (make-message (word->string status-word)))
155 12687dd9 2023-08-04 jrmu (define result-message (make-message "Let's play hangman!"))
156 12687dd9 2023-08-04 jrmu (define body-part-message (make-message ""))
157 12687dd9 2023-08-04 jrmu
158 12687dd9 2023-08-04 jrmu ;;Controller
159 12687dd9 2023-08-04 jrmu
160 12687dd9 2023-08-04 jrmu ; 1. "You won"
161 12687dd9 2023-08-04 jrmu ; 2. (list "The End" body-part word)
162 12687dd9 2023-08-04 jrmu ; 3. (list "Good guess!" word)
163 12687dd9 2023-08-04 jrmu ; 4. (list "Sorry" body-part word)
164 12687dd9 2023-08-04 jrmu
165 12687dd9 2023-08-04 jrmu
166 12687dd9 2023-08-04 jrmu (define (check-call-back event)
167 12687dd9 2023-08-04 jrmu (local ((define response (hangman-guess (list-ref LETTERS (choice-index guess-choice)))))
168 12687dd9 2023-08-04 jrmu (cond
169 12687dd9 2023-08-04 jrmu [(string? response) (and (draw-message result-message response)
170 12687dd9 2023-08-04 jrmu (draw-message status-word-message (word->string status-word)))]
171 12687dd9 2023-08-04 jrmu [(= (length response) 2)
172 12687dd9 2023-08-04 jrmu (and (draw-message result-message (first response))
173 12687dd9 2023-08-04 jrmu (draw-message status-word-message (word->string (second response))))]
174 12687dd9 2023-08-04 jrmu [(and (draw-message result-message (first response))
175 12687dd9 2023-08-04 jrmu (draw-message body-part-message (symbol->string (second response)))
176 12687dd9 2023-08-04 jrmu (draw-message status-word-message (word->string (third response)))
177 12687dd9 2023-08-04 jrmu (draw-next-part (second response)))
178 12687dd9 2023-08-04 jrmu (cond
179 12687dd9 2023-08-04 jrmu [(empty? body-parts-left)
180 12687dd9 2023-08-04 jrmu (begin (hangman)
181 12687dd9 2023-08-04 jrmu (draw-message status-message "Chosen Word:"))]
182 12687dd9 2023-08-04 jrmu [else true])])))
183 12687dd9 2023-08-04 jrmu
184 12687dd9 2023-08-04 jrmu (define check-button (make-button "Check" check-call-back))
185 12687dd9 2023-08-04 jrmu
186 12687dd9 2023-08-04 jrmu (create-window
187 12687dd9 2023-08-04 jrmu (list (list guess-message guess-choice check-button)
188 12687dd9 2023-08-04 jrmu (list status-message status-word-message)
189 12687dd9 2023-08-04 jrmu (list result-message body-part-message)))
190 12687dd9 2023-08-04 jrmu
191 12687dd9 2023-08-04 jrmu (define CWIDTH 300)
192 12687dd9 2023-08-04 jrmu (define CHEIGHT 300)
193 12687dd9 2023-08-04 jrmu (start CWIDTH CHEIGHT)
194 12687dd9 2023-08-04 jrmu
195 12687dd9 2023-08-04 jrmu ; draw-next-part : symbol -> boolean
196 12687dd9 2023-08-04 jrmu ; Draws the next part given the name of the part. Returns true if
197 12687dd9 2023-08-04 jrmu ; drawing is successful.
198 12687dd9 2023-08-04 jrmu
199 12687dd9 2023-08-04 jrmu (define (draw-next-part part)
200 12687dd9 2023-08-04 jrmu (local ((define XCENTER (/ CWIDTH 2))
201 12687dd9 2023-08-04 jrmu (define COLORHEAD 'brown)
202 12687dd9 2023-08-04 jrmu (define COLORBODY 'purple)
203 12687dd9 2023-08-04 jrmu (define COLORARMS 'brown)
204 12687dd9 2023-08-04 jrmu (define COLORLEGS 'red)
205 12687dd9 2023-08-04 jrmu (define (draw-noose)
206 12687dd9 2023-08-04 jrmu (and (draw-solid-line (make-posn 0 (/ CHEIGHT 10))
207 12687dd9 2023-08-04 jrmu (make-posn XCENTER (/ CHEIGHT 10)))
208 12687dd9 2023-08-04 jrmu (draw-solid-line (make-posn XCENTER (/ CHEIGHT 10))
209 12687dd9 2023-08-04 jrmu (make-posn XCENTER (/ CHEIGHT 5)))))
210 12687dd9 2023-08-04 jrmu (define (draw-head)
211 12687dd9 2023-08-04 jrmu (draw-circle (make-posn XCENTER (/ CHEIGHT 3)) (* CHEIGHT 2/15) COLORHEAD))
212 12687dd9 2023-08-04 jrmu (define (draw-body)
213 12687dd9 2023-08-04 jrmu (draw-solid-line (make-posn XCENTER (* 7/15 CHEIGHT))
214 12687dd9 2023-08-04 jrmu (make-posn XCENTER (* CHEIGHT 3/4))
215 12687dd9 2023-08-04 jrmu COLORBODY))
216 12687dd9 2023-08-04 jrmu (define (draw-right-arm)
217 12687dd9 2023-08-04 jrmu (draw-solid-line (make-posn XCENTER (* 3/5 CHEIGHT))
218 12687dd9 2023-08-04 jrmu (make-posn (* CWIDTH 3/4) (* CHEIGHT 7/15))
219 12687dd9 2023-08-04 jrmu COLORARMS))
220 12687dd9 2023-08-04 jrmu (define (draw-left-arm)
221 12687dd9 2023-08-04 jrmu (draw-solid-line (make-posn XCENTER (* 3/5 CHEIGHT))
222 12687dd9 2023-08-04 jrmu (make-posn (* CWIDTH 1/4) (* CHEIGHT 7/15))
223 12687dd9 2023-08-04 jrmu COLORARMS))
224 12687dd9 2023-08-04 jrmu (define (draw-right-leg)
225 12687dd9 2023-08-04 jrmu (draw-solid-line (make-posn XCENTER (* 3/4 CHEIGHT))
226 12687dd9 2023-08-04 jrmu (make-posn (* CWIDTH 7/8) (* CHEIGHT 15/16))
227 12687dd9 2023-08-04 jrmu COLORLEGS))
228 12687dd9 2023-08-04 jrmu (define (draw-left-leg)
229 12687dd9 2023-08-04 jrmu (draw-solid-line (make-posn XCENTER (* 3/4 CHEIGHT))
230 12687dd9 2023-08-04 jrmu (make-posn (* CWIDTH 1/8) (* CHEIGHT 15/16))
231 12687dd9 2023-08-04 jrmu COLORLEGS)))
232 12687dd9 2023-08-04 jrmu (cond
233 12687dd9 2023-08-04 jrmu [(symbol=? part 'noose) (draw-noose)]
234 12687dd9 2023-08-04 jrmu [(symbol=? part 'head) (draw-head)]
235 12687dd9 2023-08-04 jrmu [(symbol=? part 'body) (draw-body)]
236 12687dd9 2023-08-04 jrmu [(symbol=? part 'right-arm) (draw-right-arm)]
237 12687dd9 2023-08-04 jrmu [(symbol=? part 'left-arm) (draw-left-arm)]
238 12687dd9 2023-08-04 jrmu [(symbol=? part 'right-leg) (draw-right-leg)]
239 12687dd9 2023-08-04 jrmu [(symbol=? part 'left-leg) (draw-left-leg)])))
240 12687dd9 2023-08-04 jrmu
241 12687dd9 2023-08-04 jrmu
242 12687dd9 2023-08-04 jrmu ;;Wish-list
243 12687dd9 2023-08-04 jrmu ;;Once game is over, create a new button, reset the board
244 12687dd9 2023-08-04 jrmu ;;Once game is over, you need to erase the body-part message or at least put the correct body part image (left-leg is never given)
245 12687dd9 2023-08-04 jrmu ;;Once game is over, replace status-word with chosen-word