Guest User

Untitled

a guest
Feb 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. ;;Bin-Packing
  2. ;Like tetris, but different
  3. (require 2htdp/image)
  4. (require 2htdp/universe)
  5. ;defines for easy change
  6. (define TICK-RATE 0.2)
  7. (define BOARD-WIDTH 10)
  8. (define BOARD-HEIGHT 20)
  9. (define CELL 20) ;how big a cell is, in pixels
  10. (define HALF-CELL (/ CELL 2))
  11. ;Board coordinates makes a grid system
  12. ;with x increasing as you go right
  13. ;and y decreasing as you go up
  14.  
  15.  
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17. ;; Data Definitions
  18.  
  19. ;; A Block is a (make-block Number Number Color)
  20. (define-struct block (x y color))
  21.  
  22. ;; A Tetra is a (make-tetra Posn BSet)
  23. ;; The center point is the point around which the tetra rotates
  24. ;; when it spins.
  25. (define-struct tetra (center blocks))
  26.  
  27. ;; A Set of Blocks (BSet) is one of:
  28. ;; - empty
  29. ;; - (cons Block BSet)
  30. ;; Order does not matter. Repetitions are NOT allowed.
  31.  
  32. ;; A World is a (make-world Tetra BSet)
  33. ;; The BSet represents the pile of blocks at the bottom of the screen.
  34. (define-struct world (tetra pile))
  35.  
  36. ;; block-rotate-ccw : Posn Block -> Block
  37. ;; Rotate the block 90 counterclockwise around the posn.
  38. (define (block-rotate-ccw c b)
  39. (make-block (+ (posn-x c) (- (posn-y c) (block-y b)))
  40. (+ (posn-y c) (- (block-x b) (posn-x c)))
  41. (block-color b)))
  42.  
  43.  
  44. ;template for tetra
  45.  
  46.  
  47.  
  48.  
  49. ;make a tetra
  50. (define (O-block x y)
  51. (make-tetra (make-posn x y)
  52. (cons (make-block (- x 1) (+ y 1) "green")
  53. (cons (make-block x (+ y 1) "green")
  54. (cons (make-block (- x 1) y "green")
  55. (cons (make-block x y "green") empty))))))
  56. (define (I-block x y)
  57. (make-tetra (make-posn x y)
  58. (cons (make-block (- x 1) y "blue")
  59. (cons (make-block x y "blue")
  60. (cons (make-block (+ x 1) y "blue")
  61. (cons (make-block (+ x 2) y "blue") empty))))))
  62. (define (L-block x y)
  63. (make-tetra (make-posn x y)
  64. (cons (make-block (- x 1) y "purple")
  65. (cons (make-block x y "purple")
  66. (cons (make-block (+ x 1) y "purple")
  67. (cons (make-block (+ x 1) (+ y 1) "purple") empty))))))
  68. (define (J-block x y)
  69. (make-tetra (make-posn x y)
  70. (cons (make-block (- x 1) (+ y 1) "teal")
  71. (cons (make-block (- x 1) y "teal")
  72. (cons (make-block x y "teal")
  73. (cons (make-block (+ x 1) y "teal") empty))))))
  74. (define (T-block x y)
  75. (make-tetra (make-posn x y)
  76. (cons (make-block (- x 1) y "orange")
  77. (cons (make-block x y "orange")
  78. (cons (make-block x (- y 1) "orange")
  79. (cons (make-block (+ x 1) y "orange") empty))))))
  80. (define (Z-block x y)
  81. (make-tetra (make-posn x y)
  82. (cons (make-block (- x 1) (- y 1) "pink")
  83. (cons (make-block x (- y 1) "pink")
  84. (cons (make-block x y "pink")
  85. (cons (make-block (+ x 1) y "pink") empty))))))
  86. (define (S-block x y)
  87. (make-tetra (make-posn x y)
  88. (cons (make-block (- x 1) y "red")
  89. (cons (make-block x y "red")
  90. (cons (make-block x (- y 1) "red")
  91. (cons (make-block (+ x 1) (- y 1) "red") empty))))))
  92.  
  93.  
  94. #;(check-expect (O-block 5 7) (make-tetra (make-posn 5 7)
  95. (cons (make-block 4 8 "green")
  96. (cons (make-block 6 8 "green")
  97. (cons (make-block 4 6 "green")
  98. (cons (make-block 6 6 "green") empty))))))
  99. #;(check-expect (I-block 4 3) (make-tetra (make-posn 4 3)
  100. (cons (make-block 3 3 "blue")
  101. (cons (make-block 4 3 "blue")
  102. (cons (make-block 5 3 "blue")
  103. (cons (make-block 6 3 "blue") empty))))))
  104.  
  105.  
  106.  
  107. ;renderblockset function
  108. ;BSet scene -> image
  109. ;renders a BSet onto a scene to produce an image
  110. (define (renderblockset BSet scene)
  111. (cond
  112. [(empty? BSet) scene]
  113. [else (place-image (overlay
  114. (square CELL "outline" "black")
  115. (square CELL "solid" (block-color (first BSet))))
  116. (* CELL (block-x (first BSet)))
  117. (* CELL (block-y (first BSet)))
  118. (renderblockset (rest BSet) scene))]))
  119. ; (list (make-block 4 8 "green") (make-block 6 8 "green") (make-block 4 6 "green") (make-block 6 6 "green"))
  120. (define (gen erator)
  121. (cond
  122. [(= erator 0) (I-block (/ BOARD-WIDTH 2) 0)]
  123. [(= erator 1) (O-block (/ BOARD-WIDTH 2) 0)]
  124. [(= erator 2) (L-block (/ BOARD-WIDTH 2) 0)]
  125. [(= erator 3) (J-block (/ BOARD-WIDTH 2) 0)]
  126. [(= erator 4) (T-block (/ BOARD-WIDTH 2) 0)]
  127. [(= erator 5) (Z-block (/ BOARD-WIDTH 2) 0)]
  128. [(= erator 6) (S-block (/ BOARD-WIDTH 2) 0)]))
  129. ;world->image
  130. ;takes world and renders image
  131. (define (world->image wrld)
  132. (renderblockset (tetra-blocks (world-tetra wrld))
  133. (renderblockset (world-pile wrld)
  134. (empty-scene (* BOARD-WIDTH CELL) (* BOARD-HEIGHT CELL)))))
  135. ;world->world
  136. ;world -> world (new)
  137. (define (world->worldlame wrld)
  138. (make-world (make-tetra (posn-shift (tetra-center (world-tetra wrld)))
  139. (list-shift (tetra-blocks (world-tetra wrld))))
  140. (world-pile wrld)))
  141. (define (posn-shift pos)
  142. (make-posn (posn-x pos) (+ 1 (posn-y pos))))
  143. (define (list-shift LoB)
  144. (cond
  145. [(empty? LoB) empty]
  146. [else (cons (make-block (block-x (first LoB))
  147. (+ 1 (block-y (first LoB)))
  148. (block-color (first LoB)))
  149. (list-shift (rest LoB)))]))
  150. (define (listcollide list1 list2)
  151. (cond
  152. [(empty?
  153. [(collide? list1 (first list2)) true]
  154. [else
  155. (define (collide? list block)
  156. (cond
  157. [(equal? (first list1) block) true]
  158. [else (collide? (rest list1) block)]))
  159.  
  160.  
  161. (define (recurserot pos BSet)
  162. (cond
  163. [(empty? BSet) empty]
  164. [else (cons (block-rotate-ccw pos (first BSet)) (recurserot pos (rest BSet)))]))
  165.  
  166. (define (rottetra tetra)
  167. (make-tetra (tetra-center tetra) (recurserot (tetra-center tetra) (tetra-blocks tetra))))
  168.  
  169.  
  170. (define (handle-key w ke)
  171. (cond [(key=? ke "n") world0]
  172. [(or (key=? ke "left")
  173. (key=? ke "right")
  174. (key=? ke "a")
  175. (key=? ke "s"))
  176. (make-world
  177. (world-food w))]
  178. [else w]))
  179. (define world0 (make-world (gen (random 6)) empty))
  180. (big-bang world0
  181. (on-tick world->worldlame TICK-RATE)
  182. (on-key handle-key)
  183. (to-draw world->image)
  184. )
Add Comment
Please, Sign In to add comment