Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (require 2htdp/image)
- (require 2htdp/universe)
- ; --- constants ---
- (define TEXT-SIZE 10)
- (define GAME-HEIGHT 30)
- (define GAME-WIDTH 20)
- (define BG (rectangle (* TEXT-SIZE GAME-WIDTH) (* TEXT-SIZE GAME-HEIGHT) "solid" "white"))
- (define AVAILABLE-WORDS (list
- "elmo"
- "lambda"
- "salmon"
- "serpinski"
- "hello"
- "snow day"
- "octagon"
- "orange"
- "racket"
- "cons"
- "hydrogen"
- "krypton"
- "e"
- "big bird"
- "kermit"
- "list ends here"))
- ; --- data ---
- ; A ToR (Type-o-Rama) is a (make-ToR LoW LoW TeS Nat)
- (define-struct tor [active stuck typed tick-num])
- ; and represents a Type-o-Rama game's active (falling) words, stuck words, type field, and number of ticks that have passed.
- #;(define (tor-temp tor)
- (...
- (low-temp (tor-active tor))
- (low-temp (tor-stuck tor))
- (tor-typed tor)
- (tor-gen? tor)))
- ; A LoW (List of Words) is one of:
- ; - (cons Word LoW)
- ; - empty
- #;(define (low-temp low)
- (cond
- [(empty? low) ...]
- [(cons? low)
- (... (word-temp (first low)) (low-temp (rest low)))]))
- ; A TeS (Text Editor State) is a (make-tes Nat String)
- (define-struct tes [cursor text])
- ; and represents a cursor's position and the text it is over
- #;(define (tes-temp tes)
- (...(define posn0 (make-posn 0 0))
- (tes-cursor tes)
- (tes-text tes)))
- ; A Word is a (make-word String Posn)
- (define-struct word [string posn])
- ; and represents a word and its center's position
- #;(define (word-temp w)
- (...
- (word-string w)
- (posn-(define posn0 (make-posn 0 0))temp (word-posn w))))
- ; A Posn is a (make-posn Nat Nat)
- ; and represents the position of a letter on the grid
- #;(define (posn-temp p)
- (posn-x p)
- (posn-y p))
- (define posn0 (make-posn 0 0))
- (define posn1 (make-posn 15 5))
- (define posn2 (make-posn 15 25))
- (define posn3 (make-posn 7 26))
- (define word0 (make-word "octagon" posn1))
- (define word1 (make-word "serpinski" posn2))
- (define word-error (make-word "big bird" posn0))
- (define tes0 (make-tes 2 "racke"))
- (define tes1 (make-tes 5 "lambda"))
- (define low0 (cons word0 empty))
- (define low1 (cons word1 low0))
- (define tor0 (make-tor low1 empty tes1 4))
- (define tor1 (make-tor low1 (cons (make-word "kermit" posn3) empty) tes0 6))
- ; --- main ---
- ;type-o-rama : Positive -> Positive
- ;type-o-rama : Launches the game and gets the score
- #;(define (type-o-rama n)
- (get-score n (launch-game n)))
- ;launch-game : Positive -> ToR
- ;launch-game : Launches the game at the given seconds/frame
- #;(define (launch-game n)
- (big-bang
- (make-tor empty empty (make-tes 0 "") 0)
- [to-draw tor-todraw]
- [on-tick tor-ontick n]
- [on-mouse tor-onmouse]
- [stop-when tor-stopwhen]))
- ;get-score : Positive ToR -> Positive
- ;get-score : Gets the score given the end state and speed
- (define (get-score n tor)
- (* (/ 1 n) (tor-tick-num tor)))
- (check-expect (get-score 5 tor0) 0.8)
- ;grid->pixel : Nat -> Nat
- ;grid->pixel : Converts grid coordinates to pixel ones
- (define (grid->pixel n)
- (+ (/ TEXT-SIZE 2) (* TEXT-SIZE n)))
- ; --- to-draw ---
- ;tor-todraw : ToR -> Image
- ;tor-todraw : Draws the type-o-rama
- (define (tor-todraw tor)
- (place-active (tor-active tor)
- (place-stuck (tor-stuck tor)
- (place-typed (tor-typed tor) BG))))
- ;place-active : LoW Image -> Image
- ;place-active : Places all active words
- (define (place-active low i)
- (cond
- [(empty? low) i]
- [(cons? low)
- (place-image
- (text (word-string (first low)) TEXT-SIZE "lime")
- (grid->pixel (posn-x (word-posn (first low))))
- (grid->pixel (posn-y (word-posn (first low))))
- (place-active (rest low) i))]))
- (check-expect (place-active low1 BG)
- (place-image (text "serpinski" 10 "lime") 155 255
- (place-image (text "octagon" 10 "lime") 155 55
- BG)))
- ;place-stuck : LoW Image -> Image
- ;place-stuck : Places all active words
- (define (place-stuck low i)
- (cond
- [(empty? low) i]
- [(cons? low)
- (place-image
- (text (word-string (first low)) TEXT-SIZE "red")
- (grid->pixel (posn-x (word-posn (first low))))
- (grid->pixel (posn-y (word-posn (first low))))
- (place-stuck (rest low) i))]))
- (check-expect (place-stuck low1 BG)
- (place-image (text "serpinski" 10 "red") 155 255
- (place-image (text "octagon" 10 "red") 155 55
- BG)))
- ;place-typed : TeS Image -> Image
- ;place-typed : Places the typed word below the image
- (define (place-typed tes i)
- (above
- i
- (rectangle (* TEXT-SIZE GAME-WIDTH) 5 "solid" "black")
- (overlay
- (text (place-cursor tes) TEXT-SIZE "magenta")
- (rectangle (* TEXT-SIZE GAME-WIDTH) (* TEXT-SIZE GAME-HEIGHT 1/4) "solid" "white"))))
- ;place-cursor : TeS -> String
- ;place-cursor : Inserts the cursor as a | character
- (define (place-cursor tes)
- (string-append
- (substring (tes-text tes) 0 (tes-cursor tes))
- "|"
- (substring (tes-text tes) (tes-cursor tes) (string-length (tes-text tes)))))
- (check-expect (place-cursor tes0) "ra|cke")
- (check-expect (place-cursor tes1) "lambd|a")
- (check-expect (place-typed tes1 BG)
- (above
- BG
- (rectangle 200 5 "solid" "black")
- (overlay
- (text "lambd|a" 10 "magenta")
- (rectangle 200 75 "solid" "white"))))
- ; --- on-tick ---
- ; A List of Posns (LoP) is one of:
- ; - empty
- ; - (cons posn lop)
- ; and represents a list of positions#;(define (low-temp low)
- (define (lop-temp lop)
- (cond
- [(empty? lop) ...]
- [(cons? lop)
- (... (posn-temp (first low)) (lop-temp (rest low)))]))
- (define lop0 empty)
- (define lop1 (cons posn0 lop0))
- (define lop2 (cons posn1 lop1))
- (define lop3 (cons posn2 lop2))
- (define lop4 (cons posn3 lop3))
- ;on-tick needs to stick all words that need to be, gen new words, and advance all words, that's it
- ;all-positions-occupied : Word -> LoP
- ;all-positions-occupied : Gets a list of all occupied positions of a word
- (define (all-positions-occupied w)
- (list-positions
- (if
- (even? (string-length (word-string w)))
- (add1 (string-length (word-string w)))
- (string-length (word-string w)))
- (word-posn w)))
- ; list-positions : OddPositive Posn -> LoP
- ; list-positions : Lists all positions occupied
- (define (list-positions op p)
- (rest
- (insert-innermost
- (list-dir 'left (/ (sub1 op) 2) p)
- (list-dir 'right (/ (sub1 op) 2) p))))
- ; insert-innermost : LoP LoP -> LoP
- ; insert-innermost : Inserts one LoP inside another
- (define (insert-innermost lop1 lop2)
- (cond
- [(empty? lop1) lop2]
- [(cons? lop1)
- (cons (first lop1) (insert-innermost (rest lop1) lop2))]))
- ; list-dir : Symbol Nat Posn -> LoP
- ; list-dir : Lists posns in a specific direction, 'left or 'right
- (define (list-dir d n p)
- (cond
- [(zero? n) empty]
- [(positive? n)
- (cons
- p
- (list-dir
- d
- (sub1 n)
- (cond
- [(symbol=? d 'left)
- (make-posn (sub1 (posn-x p)) (posn-y p))]
- [(symbol=? d 'right)
- (make-posn (add1 (posn-x p)) (posn-y p))])))]))
- ; advance : Posn -> Posn
- ; advance : Advances a posn
- (define (advance p)
- (make-posn (posn-x p) (add1 (posn-y p))))
- ; become-stuck? : Word LoW -> Boolean
- ; become-stuck? : Checks if a word needs to stick given a list of existing words to stick to
- (define (become-stuck? w low)
- (or
- (lists-contain-any-matches? (all-positions-occupied (make-word (word-string w) (advance (word-posn w)))) (all-positions-of-list low))
- (= (posn-y (word-posn w)) GAME-HEIGHT)))
- ; lists-contain-any-matches? : LoP LoP -> Boolean
- ; lists-contain-any-matches? : Determines if there are any matches in any of the things in the list
- (define (lists-contain-any-matches? lop1 lop2)
- (cond
- [(empty? lop1) #f]
- [(cons? lop1)
- (or (list-contains? (first lop1) lop2) (lists-contain-any-matches? (rest lop1) lop2))]))
- ; list-contains? : Posn LoP -> Boolean
- ; list-contains? : Determines if a posn is in a list of posns
- (define (list-contains? p lop)
- (cond
- [(empty? lop) #f]
- [(cons? lop)
- (or (posn=? p (first lop)) (list-contains? p (rest lop)))]))
- ; posn=? : Posn Posn -> Boolean
- ; posn=? : Determines if two posns are equal
- (define (posn=? p1 p2)
- (and
- (= (posn-x p1) (posn-x p2))
- (= (posn-y p1) (posn-y p2))))
- ; all-positions-of-list : LoW -> LoP
- ; all-positions-of-list : Lists all occupied positions of all words in a list
- (define (all-positions-of-list low)
- (cond
- [(empty? low) empty]
- [(cons? low)
- (insert-innermost
- (all-positions-occupied (first low))
- (all-positions-of-list (rest low)))]))
- ; stick : ToR Word
- ; stick : Sticks the given word into the stuck list
- (define (stick tor w)
- (make-tor (shave-word w (tor-active tor)) (cons w (tor-stuck tor)) (tor-tes tor) (tor-tick-num tor)))
- ; shave-word : Word LoW -> LoW
- ; shave-word : Deletes the first instance of a given word
- (define (shave-word w low)
- (cond
- [(empty? low) empty]
- [(cons? low)
- (if
- (word=? (first low) w)
- (rest low)
- (cons (first low) (shave-word w low)))]))
- ; word=? : Word Word -> Boolean
- ; word=? : Determines if two words are equivalent
- (define (word=? w1 w2)
- (and
- (posn=? (word-posn w1) (word-posn w2))
- (string=? (word-string w1) (word-string w2))))
- ; stick-all-words-which-must-be : ToR LoW -> ToR
- ; stick-all-words-which-must-be : Sticks all words which need to be stuck from a given list of words to check (should be same as active)
- (define (stick-all-words-which-must-be tor low)
- (cond
- [(empty? low) tor]
- [(cons? low)
- (if
- (become-stuck? (first low) (tor-stuck tor))
- (stick-all-words-which-must-be (stick tor (first low)) (rest low))
- (stick-all-words-which-must-be tor (rest low)))]))
- ;you can check if a word must be stuck and stick the word, the rest is busywork)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement