#lang scheme
(require htdp/testing)
(require htdp/world)
(require lang/htdp-advanced)
(provide (all-defined-out))
(define block-size 20) (define board-width 10) (define board-height 20)
(define-struct block (x y color))
(define-struct tetra (center blocks))
(define-struct world (tetra blocks))
(define (block=? b1 b2)
(and (= (block-x b1) (block-x b2))
(= (block-y b1) (block-y b2))))
"Identical blocks are equal."
(check-expect (block=? (make-block 0 0 'black)
(make-block 0 0 'black))
true)
"Identical (modulo color) blocks are equal."
(check-expect (block=? (make-block 0 0 'black)
(make-block 0 0 'red))
true)
"Blocks with different coordinates are different."
(check-expect (block=? (make-block 0 1 'black)
(make-block 0 0 'black))
false)
(check-expect (block=? (make-block 0 0 'black)
(make-block 0 1 'black))
false)
(define (block-move dx dy b)
(make-block (+ dx (block-x b))
(+ dy (block-y b))
(block-color b)))
"Block move."
(check-expect (block=? (block-move 0 0 (make-block 0 0 'black))
(make-block 0 0 'black))
true)
(check-expect (block=? (block-move 0 1 (make-block 0 0 'black))
(make-block 0 1 'black))
true)
(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)))
(define (block-rotate-cw c b)
(block-rotate-ccw c (block-rotate-ccw c (block-rotate-ccw c b))))
(define (blocks-contains? bs b)
(ormap (lambda (c) (block=? b c)) bs))
"Block set membership."
(check-expect (blocks-contains? empty (make-block 0 0 'black))
false)
(check-expect (blocks-contains? (list (make-block 0 0 'black))
(make-block 0 0 'black))
true)
(check-expect (blocks-contains? (list (make-block 0 1 'black))
(make-block 0 0 'black))
false)
(define (blocks-subset? bs1 bs2)
(andmap (lambda (b) (blocks-contains? bs2 b)) bs1))
"Block set containment."
(check-expect (blocks-subset? empty empty)
true)
(check-expect (blocks-subset? empty (list (make-block 0 0 'black)))
true)
(check-expect (blocks-subset? (list (make-block 0 0 'black))
(list (make-block 0 0 'black)))
true)
(define (blocks=? bs1 bs2)
(and (blocks-subset? bs1 bs2)
(blocks-subset? bs2 bs1)))
"Block set equality."
(check-expect (blocks=? (list (make-block 0 0 'black))
(list (make-block 0 0 'black)))
true)
(check-expect (blocks=? (list (make-block 0 0 'black))
(list (make-block 0 1 'black)))
false)
(define (blocks-intersect bs1 bs2)
(filter (lambda (b) (blocks-contains? bs2 b)) bs1))
"Block set intersection."
(check-expect (blocks=? (blocks-intersect empty empty)
empty)
true)
(check-expect (blocks=? (blocks-intersect empty (list (make-block 0 0 'black)))
empty)
true)
(check-expect (blocks=? (blocks-intersect (list (make-block 0 0 'black)) empty)
empty)
true)
(check-expect (blocks=? (blocks-intersect (list (make-block 0 0 'black))
(list (make-block 0 0 'black)))
(list (make-block 0 0 'black)))
true)
(define (blocks-union bs1 bs2)
(foldr (lambda (b bs)
(cond [(blocks-contains? bs b) bs]
[else (cons b bs)]))
bs2
bs1))
"Block set union."
(check-expect (blocks=? (blocks-union empty empty) empty)
true)
(check-expect (blocks=? (blocks-union empty (list (make-block 0 0 'black)))
(list (make-block 0 0 'black)))
true)
(check-expect (blocks=? (blocks-union (list (make-block 0 0 'black)) empty)
(list (make-block 0 0 'black)))
true)
(check-expect (blocks=? (blocks-union (list (make-block 0 0 'black))
(list (make-block 0 0 'black)))
(list (make-block 0 0 'black)))
true)
(check-expect (blocks=? (blocks-union (list (make-block 0 0 'black))
(list (make-block 0 1 'black)))
(list (make-block 0 0 'black)
(make-block 0 1 'black)))
true)
(define (blocks-count bs)
(length bs))
"Block set cardinality."
(check-expect (blocks-count empty) 0)
(check-expect (blocks-count (list (make-block 0 0 'black))) 1)
(check-expect (blocks-count (blocks-union (list (make-block 0 0 'black))
(list (make-block 0 0 'black))))
1)
(check-expect (blocks-count (blocks-union (list (make-block 0 0 'black))
(list (make-block 1 1 'black))))
2)
(define (blocks-max-y bs)
(foldr (lambda (b n) (max (block-y b) n)) 0 bs))
"Block set maximal y-coordinate."
(check-expect (blocks-max-y empty) 0)
(check-expect (blocks-max-y (list (make-block 0 1 'black))) 1)
(check-expect (blocks-max-y (list (make-block 1 0 'black))) 0)
(define (blocks-min-x bs)
(foldr (lambda (b n) (min (block-x b) n)) board-width bs))
"Block set minimal x-coordinate."
(check-expect (blocks-min-x empty) board-width)
(check-expect (blocks-min-x (list (make-block 0 1 'black))) 0)
(check-expect (blocks-min-x (list (make-block 1 0 'black))) 1)
(define (blocks-max-x bs)
(foldr (lambda (b n) (max (block-x b) n)) 0 bs))
"Block set maximal x-coordinate."
(check-expect (blocks-max-x empty) 0)
(check-expect (blocks-max-x (list (make-block 0 1 'black))) 0)
(check-expect (blocks-max-x (list (make-block 1 0 'black))) 1)
(define (blocks-move dx dy bs)
(map (lambda (b) (block-move dx dy b)) bs))
"Block set move."
(check-expect (blocks=? (blocks-move 0 0 empty) empty)
true)
(check-expect (blocks=? (blocks-move 0 0 (list (make-block 0 0 'black)))
(list (make-block 0 0 'black)))
true)
(check-expect (blocks=? (blocks-move 1 1 (list (make-block 0 0 'black)))
(list (make-block 1 1 'black)))
true)
(define (blocks-rotate-ccw c bs)
(map (lambda (b) (block-rotate-ccw c b)) bs))
(define (blocks-rotate-cw c bs)
(map (lambda (b) (block-rotate-cw c b)) bs))
(define (blocks-change-color bs c)
(map (lambda (b) (make-block (block-x b)
(block-y b)
c))
bs))
(define (blocks-overflow? bs)
(ormap (lambda (b) (<= (block-y b) 0)) bs))
"Block overflow."
(check-expect (blocks-overflow? empty)
false)
(check-expect (blocks-overflow? (list (make-block 0 0 'black)))
true)
(check-expect (blocks-overflow? (list (make-block 0 1 'black)))
false)
(define (blocks-row bs i)
(filter (lambda (b) (= i (block-y b))) bs))
"Block set row selection."
(check-expect (blocks=? (blocks-row empty 0) empty)
true)
(check-expect (blocks=? (blocks-row (list (make-block 0 0 'black)) 1) empty)
true)
(check-expect (blocks=? (blocks-row (list (make-block 0 0 'black)) 0)
(list (make-block 0 0 'black)))
true)
(define (full-row? bs i)
(= board-width (blocks-count (blocks-row bs i))))
"Full row."
(check-expect (full-row? empty 0) false)
(define (eliminate-full-rows bs)
(local [(define (elim-row i offset)
(cond [(< i 0) empty]
[(full-row? bs i) (elim-row (sub1 i) (add1 offset))]
[else (blocks-union (elim-row (sub1 i) offset)
(blocks-move 0 offset (blocks-row bs i)))]))]
(elim-row board-height 0)))
(define (tetra-move dx dy t)
(make-tetra (make-posn (+ dx (posn-x (tetra-center t)))
(+ dy (posn-y (tetra-center t))))
(blocks-move dx dy (tetra-blocks t))))
(define (tetra-rotate-ccw tetra)
(make-tetra (tetra-center tetra)
(blocks-rotate-ccw (tetra-center tetra)
(tetra-blocks tetra))))
(define (tetra-rotate-cw tetra)
(make-tetra (tetra-center tetra)
(blocks-rotate-cw (tetra-center tetra)
(tetra-blocks tetra))))
(define (tetra-overlaps-blocks? t bs)
(not (empty? (blocks-intersect (tetra-blocks t) bs))))
(define (tetra-change-color t c)
(make-tetra (tetra-center t)
(blocks-change-color (tetra-blocks t) c)))
(define (build-tetra-blocks color xc yc x1 y1 x2 y2 x3 y3 x4 y4)
(tetra-move 3 0
(make-tetra (make-posn xc yc)
(list (make-block x1 y1 color)
(make-block x2 y2 color)
(make-block x3 y3 color)
(make-block x4 y4 color)))))
(define tetras
(list
(build-tetra-blocks 'green 1/2 -3/2 0 -1 0 -2 1 -1 1 -2)
(build-tetra-blocks 'blue 1 -1 0 -1 1 -1 2 -1 3 -1)
(build-tetra-blocks 'purple 1 -1 0 -1 1 -1 2 -1 2 -2)
(build-tetra-blocks 'cyan 1 -1 0 -1 1 -1 2 -1 0 -2)
(build-tetra-blocks 'orange 1 -1 0 -1 1 -1 2 -1 1 -2)
(build-tetra-blocks 'red 1 -1 0 -1 1 -1 1 -2 2 -2)
(build-tetra-blocks 'pink 1 -1 0 -2 1 -2 1 -1 2 -1)
))
(define (touchdown w)
(make-world (list-pick-random tetras)
(eliminate-full-rows (blocks-union (tetra-blocks (world-tetra w))
(world-blocks w)))))
(define (world-jump-down w)
(cond [(landed? w) w]
[else (world-jump-down (make-world (tetra-move 0 1 (world-tetra w))
(world-blocks w)))]))
(define (landed-on-blocks? w)
(tetra-overlaps-blocks? (tetra-move 0 1 (world-tetra w))
(world-blocks w)))
(define (landed-on-floor? w)
(= (blocks-max-y (tetra-blocks (world-tetra w)))
(sub1 board-height)))
(define (landed? w)
(or (landed-on-blocks? w)
(landed-on-floor? w)))
(define (next-world w)
(cond [(landed? w) (touchdown w)]
[else (make-world (tetra-move 0 1 (world-tetra w))
(world-blocks w))]))
(define (try-new-tetra w new-tetra)
(cond [(or (< (blocks-min-x (tetra-blocks new-tetra)) 0)
(>= (blocks-max-x (tetra-blocks new-tetra)) board-width)
(tetra-overlaps-blocks? new-tetra (world-blocks w)))
w]
[else (make-world new-tetra (world-blocks w))]))
(define (world-move dx dy w)
(try-new-tetra w (tetra-move dx dy (world-tetra w))))
(define (world-rotate-ccw w)
(try-new-tetra w (tetra-rotate-ccw (world-tetra w))))
(define (world-rotate-cw w)
(try-new-tetra w (tetra-rotate-cw (world-tetra w))))
(define (ghost-blocks w)
(tetra-blocks (tetra-change-color (world-tetra (world-jump-down w))
'gray)))
(define (world-key-move w k)
(cond [(key=? k 'left)
(world-move -1 0 w)]
[(key=? k 'right)
(world-move 1 0 w)]
[(key=? k 'down)
(world-jump-down w)]
[(key=? k #\a)
(world-rotate-ccw w)]
[(key=? k #\s)
(world-rotate-cw w)]
[else w]))
(define (list-pick-random ls)
(list-ref ls (random (length ls))))
(define (world->image w)
(place-image (blocks->image (append (tetra-blocks (world-tetra w))
(append (ghost-blocks w)
(world-blocks w))))
0 0
(empty-scene (* board-width block-size)
(* board-height block-size))))
(define (blocks->image bs)
(foldr (lambda (b img)
(cond [(<= 0 (block-y b)) (place-block b img)]
[else img]))
(empty-scene (add1 (* board-width block-size))
(add1 (* board-height block-size)))
bs))
(define (block->image b)
(overlay
(rectangle (add1 block-size) (add1 block-size) 'solid (block-color b))
(rectangle (add1 block-size) (add1 block-size) 'outline 'black)))
(define (place-block b scene)
(place-image (block->image b)
(+ (* (block-x b) block-size) (/ block-size 2))
(+ (* (block-y b) block-size) (/ block-size 2))
scene))
(define world0
(make-world (list-pick-random tetras) empty))
(generate-report)
(big-bang (* board-width block-size)
(* board-height block-size)
(/ 1.0 5)
world0)
(stop-when (lambda (w) (blocks-overflow? (world-blocks w))))
(on-tick-event next-world)
(on-redraw world->image)
(on-key-event world-key-move)