Advertisement
Guest User

ee

a guest
Dec 9th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.33 KB | None | 0 0
  1. (require 2htdp/image)
  2. (require 2htdp/universe)
  3.  
  4. ; --- constants ---
  5.  
  6. (define TEXT-SIZE 10)
  7. (define GAME-HEIGHT 30)
  8. (define GAME-WIDTH 20)
  9. (define BG (rectangle (* TEXT-SIZE GAME-WIDTH) (* TEXT-SIZE GAME-HEIGHT) "solid" "white"))
  10. (define AVAILABLE-WORDS (list
  11. "elmo"
  12. "lambda"
  13. "salmon"
  14. "serpinski"
  15. "hello"
  16. "snow day"
  17. "octagon"
  18. "orange"
  19. "racket"
  20. "cons"
  21. "hydrogen"
  22. "krypton"
  23. "e"
  24. "big bird"
  25. "kermit"
  26. "list ends here"))
  27.  
  28. ; --- data ---
  29.  
  30. ; A ToR (Type-o-Rama) is a (make-ToR LoW LoW TeS Nat)
  31. (define-struct tor [active stuck typed tick-num])
  32. ; and represents a Type-o-Rama game's active (falling) words, stuck words, type field, and number of ticks that have passed.
  33. #;(define (tor-temp tor)
  34. (...
  35. (low-temp (tor-active tor))
  36. (low-temp (tor-stuck tor))
  37. (tor-typed tor)
  38. (tor-gen? tor)))
  39.  
  40. ; A LoW (List of Words) is one of:
  41. ; - (cons Word LoW)
  42. ; - empty
  43. #;(define (low-temp low)
  44. (cond
  45. [(empty? low) ...]
  46. [(cons? low)
  47. (... (word-temp (first low)) (low-temp (rest low)))]))
  48.  
  49. ; A TeS (Text Editor State) is a (make-tes Nat String)
  50. (define-struct tes [cursor text])
  51. ; and represents a cursor's position and the text it is over
  52. #;(define (tes-temp tes)
  53. (...(define posn0 (make-posn 0 0))
  54. (tes-cursor tes)
  55. (tes-text tes)))
  56.  
  57. ; A Word is a (make-word String Posn)
  58. (define-struct word [string posn])
  59. ; and represents a word and its center's position
  60. #;(define (word-temp w)
  61. (...
  62. (word-string w)
  63. (posn-(define posn0 (make-posn 0 0))temp (word-posn w))))
  64.  
  65. ; A Posn is a (make-posn Nat Nat)
  66. ; and represents the position of a letter on the grid
  67. #;(define (posn-temp p)
  68. (posn-x p)
  69. (posn-y p))
  70.  
  71. (define posn0 (make-posn 0 0))
  72. (define posn1 (make-posn 15 5))
  73. (define posn2 (make-posn 15 25))
  74. (define posn3 (make-posn 7 26))
  75.  
  76. (define word0 (make-word "octagon" posn1))
  77. (define word1 (make-word "serpinski" posn2))
  78. (define word-error (make-word "big bird" posn0))
  79.  
  80. (define tes0 (make-tes 2 "racke"))
  81. (define tes1 (make-tes 5 "lambda"))
  82.  
  83. (define low0 (cons word0 empty))
  84. (define low1 (cons word1 low0))
  85.  
  86. (define tor0 (make-tor low1 empty tes1 4))
  87. (define tor1 (make-tor low1 (cons (make-word "kermit" posn3) empty) tes0 6))
  88.  
  89. ; --- main ---
  90.  
  91. ;type-o-rama : Positive -> Positive
  92. ;type-o-rama : Launches the game and gets the score
  93. #;(define (type-o-rama n)
  94. (get-score n (launch-game n)))
  95.  
  96. ;launch-game : Positive -> ToR
  97. ;launch-game : Launches the game at the given seconds/frame
  98. #;(define (launch-game n)
  99. (big-bang
  100. (make-tor empty empty (make-tes 0 "") 0)
  101. [to-draw tor-todraw]
  102. [on-tick tor-ontick n]
  103. [on-mouse tor-onmouse]
  104. [stop-when tor-stopwhen]))
  105.  
  106. ;get-score : Positive ToR -> Positive
  107. ;get-score : Gets the score given the end state and speed
  108. (define (get-score n tor)
  109. (* (/ 1 n) (tor-tick-num tor)))
  110. (check-expect (get-score 5 tor0) 0.8)
  111.  
  112. ;grid->pixel : Nat -> Nat
  113. ;grid->pixel : Converts grid coordinates to pixel ones
  114. (define (grid->pixel n)
  115. (+ (/ TEXT-SIZE 2) (* TEXT-SIZE n)))
  116.  
  117. ; --- to-draw ---
  118.  
  119. ;tor-todraw : ToR -> Image
  120. ;tor-todraw : Draws the type-o-rama
  121. (define (tor-todraw tor)
  122. (place-active (tor-active tor)
  123. (place-stuck (tor-stuck tor)
  124. (place-typed (tor-typed tor) BG))))
  125.  
  126. ;place-active : LoW Image -> Image
  127. ;place-active : Places all active words
  128. (define (place-active low i)
  129. (cond
  130. [(empty? low) i]
  131. [(cons? low)
  132. (place-image
  133. (text (word-string (first low)) TEXT-SIZE "lime")
  134. (grid->pixel (posn-x (word-posn (first low))))
  135. (grid->pixel (posn-y (word-posn (first low))))
  136. (place-active (rest low) i))]))
  137. (check-expect (place-active low1 BG)
  138. (place-image (text "serpinski" 10 "lime") 155 255
  139. (place-image (text "octagon" 10 "lime") 155 55
  140. BG)))
  141.  
  142. ;place-stuck : LoW Image -> Image
  143. ;place-stuck : Places all active words
  144. (define (place-stuck low i)
  145. (cond
  146. [(empty? low) i]
  147. [(cons? low)
  148. (place-image
  149. (text (word-string (first low)) TEXT-SIZE "red")
  150. (grid->pixel (posn-x (word-posn (first low))))
  151. (grid->pixel (posn-y (word-posn (first low))))
  152. (place-stuck (rest low) i))]))
  153. (check-expect (place-stuck low1 BG)
  154. (place-image (text "serpinski" 10 "red") 155 255
  155. (place-image (text "octagon" 10 "red") 155 55
  156. BG)))
  157.  
  158. ;place-typed : TeS Image -> Image
  159. ;place-typed : Places the typed word below the image
  160. (define (place-typed tes i)
  161. (above
  162. i
  163. (rectangle (* TEXT-SIZE GAME-WIDTH) 5 "solid" "black")
  164. (overlay
  165. (text (place-cursor tes) TEXT-SIZE "magenta")
  166. (rectangle (* TEXT-SIZE GAME-WIDTH) (* TEXT-SIZE GAME-HEIGHT 1/4) "solid" "white"))))
  167.  
  168. ;place-cursor : TeS -> String
  169. ;place-cursor : Inserts the cursor as a | character
  170. (define (place-cursor tes)
  171. (string-append
  172. (substring (tes-text tes) 0 (tes-cursor tes))
  173. "|"
  174. (substring (tes-text tes) (tes-cursor tes) (string-length (tes-text tes)))))
  175. (check-expect (place-cursor tes0) "ra|cke")
  176. (check-expect (place-cursor tes1) "lambd|a")
  177.  
  178. (check-expect (place-typed tes1 BG)
  179. (above
  180. BG
  181. (rectangle 200 5 "solid" "black")
  182. (overlay
  183. (text "lambd|a" 10 "magenta")
  184. (rectangle 200 75 "solid" "white"))))
  185.  
  186. ; --- on-tick ---
  187.  
  188. ; A List of Posns (LoP) is one of:
  189. ; - empty
  190. ; - (cons posn lop)
  191. ; and represents a list of positions#;(define (low-temp low)
  192. (define (lop-temp lop)
  193. (cond
  194. [(empty? lop) ...]
  195. [(cons? lop)
  196. (... (posn-temp (first low)) (lop-temp (rest low)))]))
  197. (define lop0 empty)
  198. (define lop1 (cons posn0 lop0))
  199. (define lop2 (cons posn1 lop1))
  200. (define lop3 (cons posn2 lop2))
  201. (define lop4 (cons posn3 lop3))
  202.  
  203. ;on-tick needs to stick all words that need to be, gen new words, and advance all words, that's it
  204.  
  205. ;all-positions-occupied : Word -> LoP
  206. ;all-positions-occupied : Gets a list of all occupied positions of a word
  207. (define (all-positions-occupied w)
  208. (list-positions
  209. (if
  210. (even? (string-length (word-string w)))
  211. (add1 (string-length (word-string w)))
  212. (string-length (word-string w)))
  213. (word-posn w)))
  214.  
  215. ; list-positions : OddPositive Posn -> LoP
  216. ; list-positions : Lists all positions occupied
  217. (define (list-positions op p)
  218. (rest
  219. (insert-innermost
  220. (list-dir 'left (/ (sub1 op) 2) p)
  221. (list-dir 'right (/ (sub1 op) 2) p))))
  222.  
  223. ; insert-innermost : LoP LoP -> LoP
  224. ; insert-innermost : Inserts one LoP inside another
  225. (define (insert-innermost lop1 lop2)
  226. (cond
  227. [(empty? lop1) lop2]
  228. [(cons? lop1)
  229. (cons (first lop1) (insert-innermost (rest lop1) lop2))]))
  230.  
  231. ; list-dir : Symbol Nat Posn -> LoP
  232. ; list-dir : Lists posns in a specific direction, 'left or 'right
  233. (define (list-dir d n p)
  234. (cond
  235. [(zero? n) empty]
  236. [(positive? n)
  237. (cons
  238. p
  239. (list-dir
  240. d
  241. (sub1 n)
  242. (cond
  243. [(symbol=? d 'left)
  244. (make-posn (sub1 (posn-x p)) (posn-y p))]
  245. [(symbol=? d 'right)
  246. (make-posn (add1 (posn-x p)) (posn-y p))])))]))
  247.  
  248. ; advance : Posn -> Posn
  249. ; advance : Advances a posn
  250. (define (advance p)
  251. (make-posn (posn-x p) (add1 (posn-y p))))
  252.  
  253. ; become-stuck? : Word LoW -> Boolean
  254. ; become-stuck? : Checks if a word needs to stick given a list of existing words to stick to
  255. (define (become-stuck? w low)
  256. (or
  257. (lists-contain-any-matches? (all-positions-occupied (make-word (word-string w) (advance (word-posn w)))) (all-positions-of-list low))
  258. (= (posn-y (word-posn w)) GAME-HEIGHT)))
  259.  
  260. ; lists-contain-any-matches? : LoP LoP -> Boolean
  261. ; lists-contain-any-matches? : Determines if there are any matches in any of the things in the list
  262. (define (lists-contain-any-matches? lop1 lop2)
  263. (cond
  264. [(empty? lop1) #f]
  265. [(cons? lop1)
  266. (or (list-contains? (first lop1) lop2) (lists-contain-any-matches? (rest lop1) lop2))]))
  267.  
  268. ; list-contains? : Posn LoP -> Boolean
  269. ; list-contains? : Determines if a posn is in a list of posns
  270. (define (list-contains? p lop)
  271. (cond
  272. [(empty? lop) #f]
  273. [(cons? lop)
  274. (or (posn=? p (first lop)) (list-contains? p (rest lop)))]))
  275.  
  276. ; posn=? : Posn Posn -> Boolean
  277. ; posn=? : Determines if two posns are equal
  278. (define (posn=? p1 p2)
  279. (and
  280. (= (posn-x p1) (posn-x p2))
  281. (= (posn-y p1) (posn-y p2))))
  282.  
  283. ; all-positions-of-list : LoW -> LoP
  284. ; all-positions-of-list : Lists all occupied positions of all words in a list
  285. (define (all-positions-of-list low)
  286. (cond
  287. [(empty? low) empty]
  288. [(cons? low)
  289. (insert-innermost
  290. (all-positions-occupied (first low))
  291. (all-positions-of-list (rest low)))]))
  292.  
  293. ; stick : ToR Word
  294. ; stick : Sticks the given word into the stuck list
  295. (define (stick tor w)
  296. (make-tor (shave-word w (tor-active tor)) (cons w (tor-stuck tor)) (tor-tes tor) (tor-tick-num tor)))
  297.  
  298. ; shave-word : Word LoW -> LoW
  299. ; shave-word : Deletes the first instance of a given word
  300. (define (shave-word w low)
  301. (cond
  302. [(empty? low) empty]
  303. [(cons? low)
  304. (if
  305. (word=? (first low) w)
  306. (rest low)
  307. (cons (first low) (shave-word w low)))]))
  308.  
  309. ; word=? : Word Word -> Boolean
  310. ; word=? : Determines if two words are equivalent
  311. (define (word=? w1 w2)
  312. (and
  313. (posn=? (word-posn w1) (word-posn w2))
  314. (string=? (word-string w1) (word-string w2))))
  315.  
  316. ; stick-all-words-which-must-be : ToR LoW -> ToR
  317. ; 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)
  318. (define (stick-all-words-which-must-be tor low)
  319. (cond
  320. [(empty? low) tor]
  321. [(cons? low)
  322. (if
  323. (become-stuck? (first low) (tor-stuck tor))
  324. (stick-all-words-which-must-be (stick tor (first low)) (rest low))
  325. (stick-all-words-which-must-be tor (rest low)))]))
  326.  
  327. ;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