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-intermediate-reader.ss" "lang")((modname 17.6.2-2) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "dir.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "dir.ss" "teachpack" "htdp") (lib "hangman.ss" "teachpack" "htdp")))))
4 ;A word is either
5 ;1. empty or
6 ;2. (cons s w)
7 ;where s is a one-letter symbol ('a, 'b, ...) and '_, and w is a word.
8 ;
9 ;reveal-list : word word symbol -> word
10 ;Given chosen (word), status (word), and guess (one-letter symbol), return the new status word. A status word represents the progress of the hangman game. If guess matches a letter in chosen, replace '_ in status with the letter. Otherwise, return the same status word.
11 ;ASSUMPTION: both chosen and status are the same length.
13 ;Examples
14 ;(reveal-list '(b r e a d) '(b r e _ _) 'a)
15 ;'(b r e a _)
16 ;
17 ;(reveal-list '(b r e a d) '(b r e _ _) 'x)
18 ;'(b r e _ _)
19 ;
20 ;(reveal-list '(m o o n) '(_ _ _ n) 'o)
21 ;'(_ o o n)
23 (define (reveal-list chosen status guess)
24 (cond
25 [(empty? chosen) empty]
26 [(cons? chosen)
27 (cond
28 [(symbol=? guess (first chosen))
29 (cons (first chosen) (reveal-list (rest chosen) (rest status) guess))]
30 [else (cons (first status) (reveal-list (rest chosen) (rest status) guess))])]))
32 ; Defines canvas width (CWIDTH) and canvas height (CHEIGHT)
33 ; Defines center of x-axis
34 (define CWIDTH 600)
35 (define CHEIGHT 600)
36 (define XCENTER (/ CWIDTH 2))
37 (define COLORHEAD 'brown)
38 (define COLORBODY 'purple)
39 (define COLORARMS 'brown)
40 (define COLORLEGS 'red)
42 ; draw-noose : symbol -> boolean
43 ; Draws the corresponding part and returns true if evaluation succeeds, false
44 ; if the part to be drawn does not match this function.
46 (define (draw-noose part)
47 (cond
48 [(symbol=? part 'noose)
49 (and
50 (draw-solid-line (make-posn 0 (/ CHEIGHT 10))
51 (make-posn XCENTER (/ CHEIGHT 10)))
52 (draw-solid-line (make-posn XCENTER (/ CHEIGHT 10))
53 (make-posn XCENTER (/ CHEIGHT 5))))]
54 [else false]))
56 ; draw-head : symbol -> boolean
58 (define (draw-head part)
59 (cond
60 [(symbol=? part 'head)
61 (draw-circle (make-posn XCENTER (/ CHEIGHT 3)) (* CHEIGHT 2/15) COLORHEAD)]
62 [else false]))
64 ; draw-body : symbol -> boolean
66 (define (draw-body part)
67 (cond
68 [(symbol=? part 'body)
69 (draw-solid-line (make-posn XCENTER (* 7/15 CHEIGHT))
70 (make-posn XCENTER (* CHEIGHT 3/4))
71 COLORBODY)]
72 [else false]))
74 ; draw-right-arm : symbol -> boolean
75 (define (draw-right-arm part)
76 (cond
77 [(symbol=? part 'right-arm)
78 (draw-solid-line (make-posn XCENTER (* 3/5 CHEIGHT))
79 (make-posn (* CWIDTH 3/4) (* CHEIGHT 7/15))
80 COLORARMS)]
81 [else false]))
83 ; draw-left-arm : symbol -> boolean
84 (define (draw-left-arm part)
85 (cond
86 [(symbol=? part 'left-arm)
87 (draw-solid-line (make-posn XCENTER (* 3/5 CHEIGHT))
88 (make-posn (* CWIDTH 1/4) (* CHEIGHT 7/15))
89 COLORARMS)]
90 [else false]))
92 ; draw-right-leg : symbol -> boolean
93 (define (draw-right-leg part)
94 (cond
95 [(symbol=? part 'right-leg)
96 (draw-solid-line (make-posn XCENTER (* 3/4 CHEIGHT))
97 (make-posn (* CWIDTH 7/8) (* CHEIGHT 15/16))
98 COLORLEGS)]
99 [else false]))
101 ; draw-left-leg : symbol -> boolean
102 (define (draw-left-leg part)
103 (cond
104 [(symbol=? part 'left-leg)
105 (draw-solid-line (make-posn XCENTER (* 3/4 CHEIGHT))
106 (make-posn (* CWIDTH 1/8) (* CHEIGHT 15/16))
107 COLORLEGS)]
108 [else false]))
110 ; draw-next-part : symbol -> boolean
111 ; Draws the next part given the name of the part. Returns true if
112 ; drawing is successful.
114 (define (draw-next-part part)
115 (cond
116 [(symbol=? part 'noose) (draw-noose part)]
117 [(symbol=? part 'head) (draw-head part)]
118 [(symbol=? part 'body) (draw-body part)]
119 [(symbol=? part 'right-arm) (draw-right-arm part)]
120 [(symbol=? part 'left-arm) (draw-left-arm part)]
121 [(symbol=? part 'right-leg) (draw-right-leg part)]
122 [(symbol=? part 'left-leg) (draw-left-leg part)]))
124 (start CWIDTH CHEIGHT)
125 (hangman-list reveal-list draw-next-part)