Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;Bin-Packing
- ;Like tetris, but different
- (require 2htdp/image)
- (require 2htdp/universe)
- ;defines for easy change
- (define TICK-RATE 0.2)
- (define BOARD-WIDTH 10)
- (define BOARD-HEIGHT 20)
- (define CELL 20) ;how big a cell is, in pixels
- (define HALF-CELL (/ CELL 2))
- ;Board coordinates makes a grid system
- ;with x increasing as you go right
- ;and y decreasing as you go up
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Data Definitions
- ;; A Block is a (make-block Number Number Color)
- (define-struct block (x y color))
- ;; A Tetra is a (make-tetra Posn BSet)
- ;; The center point is the point around which the tetra rotates
- ;; when it spins.
- (define-struct tetra (center blocks))
- ;; A Set of Blocks (BSet) is one of:
- ;; - empty
- ;; - (cons Block BSet)
- ;; Order does not matter. Repetitions are NOT allowed.
- ;; A World is a (make-world Tetra BSet)
- ;; The BSet represents the pile of blocks at the bottom of the screen.
- (define-struct world (tetra pile))
- ;; block-rotate-ccw : Posn Block -> Block
- ;; Rotate the block 90 counterclockwise around the posn.
- (define (block-rotate-ccw c b)
- (make-block (+ (posn-x c) (- (posn-y c) (block-y b)))
- (+ (posn-y c) (- (block-x b) (posn-x c)))
- (block-color b)))
- ;template for tetra
- ;make a tetra
- (define (O-block x y)
- (make-tetra (make-posn x y)
- (cons (make-block (- x 1) (+ y 1) "green")
- (cons (make-block x (+ y 1) "green")
- (cons (make-block (- x 1) y "green")
- (cons (make-block x y "green") empty))))))
- (define (I-block x y)
- (make-tetra (make-posn x y)
- (cons (make-block (- x 1) y "blue")
- (cons (make-block x y "blue")
- (cons (make-block (+ x 1) y "blue")
- (cons (make-block (+ x 2) y "blue") empty))))))
- (define (L-block x y)
- (make-tetra (make-posn x y)
- (cons (make-block (- x 1) y "purple")
- (cons (make-block x y "purple")
- (cons (make-block (+ x 1) y "purple")
- (cons (make-block (+ x 1) (+ y 1) "purple") empty))))))
- (define (J-block x y)
- (make-tetra (make-posn x y)
- (cons (make-block (- x 1) (+ y 1) "teal")
- (cons (make-block (- x 1) y "teal")
- (cons (make-block x y "teal")
- (cons (make-block (+ x 1) y "teal") empty))))))
- (define (T-block x y)
- (make-tetra (make-posn x y)
- (cons (make-block (- x 1) y "orange")
- (cons (make-block x y "orange")
- (cons (make-block x (- y 1) "orange")
- (cons (make-block (+ x 1) y "orange") empty))))))
- (define (Z-block x y)
- (make-tetra (make-posn x y)
- (cons (make-block (- x 1) (- y 1) "pink")
- (cons (make-block x (- y 1) "pink")
- (cons (make-block x y "pink")
- (cons (make-block (+ x 1) y "pink") empty))))))
- (define (S-block x y)
- (make-tetra (make-posn x y)
- (cons (make-block (- x 1) y "red")
- (cons (make-block x y "red")
- (cons (make-block x (- y 1) "red")
- (cons (make-block (+ x 1) (- y 1) "red") empty))))))
- #;(check-expect (O-block 5 7) (make-tetra (make-posn 5 7)
- (cons (make-block 4 8 "green")
- (cons (make-block 6 8 "green")
- (cons (make-block 4 6 "green")
- (cons (make-block 6 6 "green") empty))))))
- #;(check-expect (I-block 4 3) (make-tetra (make-posn 4 3)
- (cons (make-block 3 3 "blue")
- (cons (make-block 4 3 "blue")
- (cons (make-block 5 3 "blue")
- (cons (make-block 6 3 "blue") empty))))))
- ;renderblockset function
- ;BSet scene -> image
- ;renders a BSet onto a scene to produce an image
- (define (renderblockset BSet scene)
- (cond
- [(empty? BSet) scene]
- [else (place-image (overlay
- (square CELL "outline" "black")
- (square CELL "solid" (block-color (first BSet))))
- (* CELL (block-x (first BSet)))
- (* CELL (block-y (first BSet)))
- (renderblockset (rest BSet) scene))]))
- ; (list (make-block 4 8 "green") (make-block 6 8 "green") (make-block 4 6 "green") (make-block 6 6 "green"))
- (define (gen erator)
- (cond
- [(= erator 0) (I-block (/ BOARD-WIDTH 2) 0)]
- [(= erator 1) (O-block (/ BOARD-WIDTH 2) 0)]
- [(= erator 2) (L-block (/ BOARD-WIDTH 2) 0)]
- [(= erator 3) (J-block (/ BOARD-WIDTH 2) 0)]
- [(= erator 4) (T-block (/ BOARD-WIDTH 2) 0)]
- [(= erator 5) (Z-block (/ BOARD-WIDTH 2) 0)]
- [(= erator 6) (S-block (/ BOARD-WIDTH 2) 0)]))
- ;world->image
- ;takes world and renders image
- (define (world->image wrld)
- (renderblockset (tetra-blocks (world-tetra wrld))
- (renderblockset (world-pile wrld)
- (empty-scene (* BOARD-WIDTH CELL) (* BOARD-HEIGHT CELL)))))
- ;world->world
- ;world -> world (new)
- (define (world->worldlame wrld)
- (make-world (make-tetra (posn-shift (tetra-center (world-tetra wrld)))
- (list-shift (tetra-blocks (world-tetra wrld))))
- (world-pile wrld)))
- (define (posn-shift pos)
- (make-posn (posn-x pos) (+ 1 (posn-y pos))))
- (define (list-shift LoB)
- (cond
- [(empty? LoB) empty]
- [else (cons (make-block (block-x (first LoB))
- (+ 1 (block-y (first LoB)))
- (block-color (first LoB)))
- (list-shift (rest LoB)))]))
- (define (listcollide list1 list2)
- (cond
- [(empty?
- [(collide? list1 (first list2)) true]
- [else
- (define (collide? list block)
- (cond
- [(equal? (first list1) block) true]
- [else (collide? (rest list1) block)]))
- (define (recurserot pos BSet)
- (cond
- [(empty? BSet) empty]
- [else (cons (block-rotate-ccw pos (first BSet)) (recurserot pos (rest BSet)))]))
- (define (rottetra tetra)
- (make-tetra (tetra-center tetra) (recurserot (tetra-center tetra) (tetra-blocks tetra))))
- (define (handle-key w ke)
- (cond [(key=? ke "n") world0]
- [(or (key=? ke "left")
- (key=? ke "right")
- (key=? ke "a")
- (key=? ke "s"))
- (make-world
- (world-food w))]
- [else w]))
- (define world0 (make-world (gen (random 6)) empty))
- (big-bang world0
- (on-tick world->worldlame TICK-RATE)
- (on-key handle-key)
- (to-draw world->image)
- )
Add Comment
Please, Sign In to add comment