#lang scheme
(require test-engine/scheme-tests
2htdp/universe
2htdp/image)
(require (file "private/list.ss"))
(provide play)
(define-struct moment (board dice selected prev) #:transparent)
(define-struct act (moment x y))
(define-struct pas (moment))
(define (lift-wrapped s2s)
(λ (w)
(cond [(act? w)
(make-act (s2s (act-moment w))
(act-x w)
(act-y w))]
[(pas? w)
(make-pas (s2s w))])))
(define B 'black)
(define W 'white)
(define-struct board (points bar off) #:transparent)
(define-struct off (top bot) #:transparent)
(define-struct die (val degree) #:transparent)
(define-struct source (dest i) #:transparent)
(define (white? c) (symbol=? c W))
(define (black? c) (symbol=? c B))
(define (board-count p? b)
(+ (foldl (λ (p s) (+ (count p? p) s))
0
(board-points b))
(count p? (board-bar b))
(count p? (off-top (board-off b)))
(count p? (off-bot (board-off b)))))
(define packed-up-board
(make-board (make-list 24 empty)
empty
(make-off (make-list 15 B)
(make-list 15 W))))
(define (board-remove-points b i j)
(make-board (replace-at (board-points b)
(remove-at (list-ref (board-points b)
i)
j)
i)
(board-bar b)
(board-off b)))
(define (board-ref b s)
(list-ref
((cond [(eq? (source-dest s) 'BAR) board-bar]
[(eq? (source-dest s) 'TOP) (λ (b) (off-top (board-off b)))]
[(eq? (source-dest s) 'BOT) (λ (b) (off-bot (board-off b)))]
[else (λ (b) (list-ref (board-points b)
(source-dest s)))])
b)
(source-i s)))
(check-expect (board-ref initial-board (make-source 0 0)) W)
(check-expect (board-ref initial-board (make-source 0 1)) W)
(define (board-add b c d)
(cond [(eq? 'TOP d)
(make-board (board-points b)
(board-bar b)
(make-off (cons c (off-top (board-off b)))
(off-bot (board-off b))))]
[(eq? 'BOT d)
(make-board (board-points b)
(board-bar b)
(make-off (off-top (board-off b))
(cons c (off-bot (board-off b)))))]
[(eq? 'BAR d)
(make-board (board-points b)
(cons c (board-bar b))
(board-off b))]
[else
(make-board (points-add (board-points b) c d)
(board-bar b)
(board-off b))]))
(define mt-board (make-board (list empty) empty (make-off empty empty)))
(check-expect (board-add mt-board W 'BAR)
(make-board (list empty) (list W) (make-off empty empty)))
(check-expect (board-add mt-board W 'TOP)
(make-board (list empty) empty (make-off (list W) empty)))
(check-expect (board-add mt-board W 'BOT)
(make-board (list empty) empty (make-off empty (list W))))
(check-expect (board-add mt-board W 0)
(make-board (list (list W)) empty (make-off empty empty)))
(define (points-add pts c i)
(replace-at pts (append (list-ref pts i) (list c)) i))
(check-expect (points-add (list empty) B 0)
(list (list B)))
(check-expect (points-add (list (list W) empty) B 1)
(list (list W)
(list B)))
(define (board-bar-fetch b i)
(list (list-ref (board-bar b) i)
(make-board (board-points b)
(remove-at (board-bar b) i)
(board-off b))))
(define (board-flip b)
(make-board (reverse (board-points b))
(reverse (board-bar b))
(make-off (off-bot (board-off b))
(off-top (board-off b)))))
(define (moment->sexp m)
(list (board->sexp (moment-board m))
(dice->sexp (moment-dice m))
(maybe-checker->sexp (moment-selected m))))
(define (sexp->moment s)
(make-moment (sexp->board (first s))
(sexp->dice (second s))
(sexp->maybe-checker (third s))
#f))
(define (board->sexp b)
(list (map checker->sexp (board-points b))
(map checker->sexp (board-bar b))
(map checker->sexp (off-top (board-off b)))
(map checker->sexp (off-bot (board-off b)))))
(define (sexp->board s)
(make-board (map sexp->checker (first s))
(map sexp->checker (second s))
(make-off (map sexp->checker (third s))
(map sexp->checker (fourth s)))))
(define (checker->sexp c) c)
(define (dice->sexp ds) (map (λ (d) (list (die-val d) (die-degree d))) ds))
(define (sexp->checker s) s)
(define (sexp->dice s) (map (λ (s) (make-die (first s) (second s))) s))
(define (sexp->maybe-checker s)
(and s (sexp->checker s)))
(check-expect (sexp->maybe-checker #f) #f)
(check-expect (sexp->maybe-checker B) B)
(check-expect (sexp->maybe-checker W) W)
(define (maybe-checker->sexp c)
(and c (checker->sexp c)))
(check-expect (sexp->board (board->sexp initial-board))
initial-board)
(define (hspace size)
(rectangle size 0 'solid 'red))
(define (vspace size)
(rectangle 0 size 'solid 'red))
(define (above0 . is)
(cond [(empty? is) (hspace 0)]
[(empty? (rest is)) (first is)]
[else (apply above is)]))
(define (beside0 . is)
(cond [(empty? is) (hspace 0)]
[(empty? (rest is)) (first is)]
[else (apply beside is)]))
(define CRADIUS 20)
(define CDIAM (* 2 CRADIUS))
(define (checker->img c)
(cond [(white? c)
(overlay (circle CRADIUS 'outline 'black)
(circle CRADIUS 'solid 'white))]
[(black? c)
(overlay (circle CRADIUS 'outline 'white)
(circle CRADIUS 'solid 'black))]
[else
(circle CRADIUS 'solid c)]))
(define (raw-points->img ps)
(apply beside/align "bottom"
(map point->img (reverse ps))))
(define (point->img p)
(above (hspace CDIAM)
(apply above0 (reverse (map checker->img p)))))
(define BORDER-CLR 'darkslateblue)
(define FLOOR-CLR 'slateblue)
(define (point-img c)
(let ((theta (* 2 (atan 1/10))))
(isosceles-triangle (/ CRADIUS (sin (* 1/2 theta)))
(* theta (/ 180 pi))
'solid
c)))
(define black-point-img (point-img 'black))
(define white-point-img (point-img 'white))
(define QUAD-HEIGHT (* 7 CDIAM))
(define BOARD-HEIGHT (* 2 QUAD-HEIGHT))
(define quadrant-background-img
(rectangle (* 6 CDIAM)
QUAD-HEIGHT
'solid
FLOOR-CLR))
(define quadrant-img
(overlay/align
"center" "bottom"
(apply beside
(build-list 6
(λ (i)
(if (even? i)
black-point-img
white-point-img)))) quadrant-background-img))
(define bar-img
(rectangle CDIAM QUAD-HEIGHT 'solid BORDER-CLR))
(define (quadrant->img ps)
(overlay/align "center" "bottom"
(raw-points->img ps)
quadrant-img))
(define (board-quadrant b q)
(take (drop (board-points b) (* q 6)) 6))
(define (checker-off->img c)
(overlay (rectangle CDIAM
(* 1/2 CRADIUS)
'outline
(if (white? c) 'black 'white))
(rectangle CDIAM
(* 1/2 CRADIUS)
'solid
(if (white? c) 'white 'black))))
(define (off->img o)
(apply above0 (map checker-off->img o)))
(define (top-board->img b)
(rotate 180
(beside (overlay/align 'center 'bottom
(off->img (off-top (board-off b)))
bar-img)
(quadrant->img (board-quadrant b 3))
bar-img
(quadrant->img (board-quadrant b 2))
bar-img)))
(define (bot-board->img b)
(beside bar-img
(quadrant->img (board-quadrant b 1))
bar-img
(quadrant->img (board-quadrant b 0))
(overlay/align 'center 'bottom
(off->img (off-bot (board-off b)))
bar-img)))
(define (board->img b)
(overlay (apply above0 (map checker->img (board-bar b)))
(above (top-board->img b)
(bot-board->img b))))
(define (world->img w)
(cond [(act? w)
(if (and (act-x w)
(moment-selected (act-moment w)))
(place-image (checker->img (moment-selected (act-moment w)))
(act-x w)
(act-y w)
(act-moment->img (act-moment w)))
(act-moment->img (act-moment w)))]
[(pas? w)
(overlay (text "Passive" 40 'red)
(pas-moment->img (pas-moment w)))]))
(define DIE-SIZE (* 2 CRADIUS))
(define BRADIUS 5)
(define die-sq-img
(square DIE-SIZE 'outline 'black))
(define bullet (circle BRADIUS 'solid 'black))
(define die-face-one
(overlay bullet die-sq-img))
(define die-face-two
(overlay/xy bullet
(- (* 5/8 DIE-SIZE))
(- (* 5/8 DIE-SIZE))
(overlay/xy bullet
(- (* 1/8 DIE-SIZE))
(- (* 1/8 DIE-SIZE))
die-sq-img)))
(define die-face-three
(overlay bullet die-face-two))
(define die-face-four
(overlay (rotate 90 die-face-two) die-face-two))
(define die-face-five
(overlay bullet die-face-four))
(define die-half-six
(overlay/align 'left 'center
(beside (hspace (* 1/2 BRADIUS))
(apply above (interleave (vspace (* 1/2 BRADIUS))
(make-list 3 bullet))))
die-sq-img))
(define die-face-six
(overlay die-half-six (rotate 180 die-half-six)))
(define (die->img d)
(rotate (die-degree d)
(overlay (list-ref (list die-face-one
die-face-two
die-face-three
die-face-four
die-face-five
die-face-six)
(die-val d))
(square DIE-SIZE 'solid 'white))))
(define (dice->img ds)
(apply beside0 (interleave (hspace DIE-SIZE)
(map die->img ds))))
(define initial-board
(make-board (list (list W W)
empty
empty
empty
empty
(list B B B B B)
empty
(list B B B)
empty
empty
empty
(list W W W W W)
(list B B B B B)
empty
empty
empty
(list W W W)
empty
(list W W W W W)
empty
empty
empty
empty
(list B B))
empty
(make-off empty empty)))
(define some-board0
(make-board (list (list W W)
empty
empty
empty
empty
(list B B B B B)
empty
(list B B B)
empty
empty
empty
(list W W W W W)
(list B B B B B)
empty
empty
empty
(list W W W)
empty
(list W W W W)
empty
empty
empty
(list W)
empty)
(list B B)
(make-off empty empty)))
(define some-board1
(make-board (list empty
empty
empty
empty
empty
(list B B B B B)
empty
(list B B B)
empty
empty
empty
empty
(list B B B B B)
empty
empty
empty
empty
empty
(list W W W W)
empty
empty
(list W)
(list W W W)
empty)
(list B B)
(make-off (list W W W W W W W)
empty)))
(define some-board2
(make-board (list empty
(list B B B)
empty
empty
(list B)
(list B B B)
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
empty
(list W W W W)
empty
empty
(list W)
(list W W W)
empty)
empty
(make-off (list W W W W W W W)
(list B B B B B B B B))))
(define (act-moment->img w)
(overlay (beside (hspace (* 7 CDIAM))
(dice->img (moment-dice w)))
(board->img (moment-board w))))
(define (pas-moment->img w)
(overlay (beside (rotate 180 (dice->img (moment-dice w)))
(hspace (* 7 CDIAM)))
(board->img (moment-board w))))
(define moment0
(make-moment initial-board
empty
#f
#f))
(define (moment-no-dice m)
(make-moment (moment-board m)
empty
(moment-selected m)
m))
(define (moment-flip m)
(make-moment (board-flip (moment-board m))
(moment-dice m)
(moment-selected m)
(moment-prev m)))
(define (roll-dice m)
(make-moment (moment-board m)
(list (make-die (random 6) (random 360))
(make-die (random 6) (random 360)))
(moment-selected m)
m))
(define (moment-undo m)
(or (moment-prev m) m))
(check-expect (board-count white? initial-board) 15)
(check-expect (board-count black? initial-board) 15)
(check-expect (board-count white? packed-up-board) 15)
(check-expect (board-count black? packed-up-board) 15)
(check-expect (board-count white? some-board0) 15)
(check-expect (board-count black? some-board0) 15)
(check-expect (board-count white? some-board1) 15)
(check-expect (board-count black? some-board1) 15)
(check-expect (board-count white? some-board2) 15)
(check-expect (board-count black? some-board2) 15)
(define (top? y)
(<= 0 y QUAD-HEIGHT))
(define (click-dest col top?)
(cond [(<= 1 col 6)
(if top?
(+ 11 col)
(+ 6 (- 6 col)))]
[(= 7 col) 'BAR]
[(<= 8 col 13)
(if top?
(+ 10 col)
(+ 6 (- 7 col)))]
[(<= 14 col) (if top? 'TOP 'BOT)]
[else #f]))
(define (y->points-index y)
(let ((a (quotient y CDIAM)))
(if (< a 7)
a
(- 13 a))))
(define (y->bar-index y odd?)
(ceiling (/ (- y QUAD-HEIGHT (if odd? CRADIUS 0)) CDIAM)))
(check-expect (y->bar-index QUAD-HEIGHT true) 0)
(check-expect (y->bar-index (- QUAD-HEIGHT CRADIUS) true) -1)
(check-expect (y->bar-index (- QUAD-HEIGHT CRADIUS 1) true) -1)
(check-expect (y->bar-index (+ QUAD-HEIGHT CRADIUS) true) 0)
(check-expect (y->bar-index (+ QUAD-HEIGHT CRADIUS 1) true) 1)
(check-expect (y->bar-index QUAD-HEIGHT false) 0)
(check-expect (y->bar-index (- QUAD-HEIGHT CRADIUS) false) 0)
(define (maybe-dest x y)
(click-dest (quotient x CDIAM)
(top? y)))
(define (maybe-select-bar b x y)
(let ((n (length (board-bar b))))
(let ((o? (odd? n)))
(let ((bi (y->bar-index y o?)))
(if o?
(and (<= (- (quotient (sub1 n) 2))
bi
(quotient (sub1 n) 2))
(board-bar-fetch b (- (quotient (sub1 n) 2) (- bi))))
(and (<= (- (sub1 (quotient n 2)))
bi
(quotient n 2))
(board-bar-fetch b (sub1 (- (quotient n 2) (- bi))))))))))
(define (maybe-select b x y)
(let ((d (maybe-dest x y)))
(and d
(cond [(eq? 'BAR d)
(maybe-select-bar b x y)]
[(eq? 'TOP d)
(and (cons? (off-top (board-off b)))
(list (first (off-top (board-off b)))
(make-board (board-points b)
(board-bar b)
(make-off (rest (off-top (board-off b)))
(off-bot (board-off b))))))]
[(eq? 'BOT d)
(and (cons? (off-bot (board-off b)))
(list (first (off-bot (board-off b)))
(make-board (board-points b)
(board-bar b)
(make-off (off-top (board-off b))
(rest (off-bot (board-off b)))))))]
[else
(let ((pt (list-ref (board-points b) d)))
(and (< (y->points-index y)
(length pt))
(list (list-ref pt (y->points-index y))
(board-remove-points b d (y->points-index y)))))]))))
(define (moment-maybe-move w x y)
(if (maybe-dest x y)
(make-moment (board-add (moment-board w)
(moment-selected w)
(maybe-dest x y))
(moment-dice w)
#f
w)
w))
(define (moment-maybe-select w x y)
(let ((ms (maybe-select (moment-board w) x y)))
(if ms
(make-moment (second ms)
(moment-dice w)
(first ms)
w)
w)))
(define (squeek w x y m)
(cond [(act? w)
(let* ((s (act-moment w))
(s* (cond [(mouse=? m "button-down")
(cond [(moment-selected s)
(moment-maybe-move s x y)]
[else
(moment-maybe-select s x y)])]
[else s])))
(if (eq? s s*)
(make-act s x y)
(make-package
(make-act s* x y)
`(moment ,(moment->sexp s*)))))]
[else w]))
(define (play player-name #:IP [ip LOCALHOST])
(big-bang (make-pas moment0)
(register ip)
(name (format "Backgammon: ~a" player-name))
(on-draw world->img)
(on-mouse squeek)
(on-key
(λ (w k)
(cond [(act? w)
(cond [(and (key=? "D" k)
(not (moment-selected (act-moment w))))
(make-package (make-pas (moment-no-dice (act-moment w)))
'(passive))]
[(key=? "R" k)
(let ((w* ((lift-wrapped roll-dice) w)))
(make-package
w*
`(moment ,(moment->sexp (act-moment w*)))))]
[else w])]
[else w])))
(on-receive
(λ (w msg)
(cond [(equal? msg '(initial))
(make-act (moment-flip moment0) #f #f)]
[(equal? msg '(active))
(make-act (moment-no-dice (pas-moment w)) #f #f)]
[(equal? msg '(passive))
(make-pas (act-moment w))]
[(equal? (first msg) 'moment)
(make-pas (moment-flip (sexp->moment (second msg))))])))))
(test)