; bridge hands
(define (take n xs)
(let loop ((n n) (xs xs) (ys '()))
(if (or (zero? n) (null? xs))
(reverse ys)
(loop (- n 1) (cdr xs)
(cons (car xs) ys)))))
(define (drop n xs)
(let loop ((n n) (xs xs))
(if (or (zero? n) (null? xs)) xs
(loop (- n 1) (cdr xs)))))
(define (range . args)
(case (length args)
((1) (range 0 (car args) (if (negative? (car args)) -1 1)))
((2) (range (car args) (cadr args) (if (< (car args) (cadr args)) 1 -1)))
((3) (let ((le? (if (negative? (caddr args)) >= <=)))
(let loop ((x(car args)) (xs '()))
(if (le? (cadr args) x)
(reverse xs)
(loop (+ x (caddr args)) (cons x xs))))))
(else (error 'range "unrecognized arguments"))))
(define (string-join sep ss)
(define (f s ss)
(string-append s (string sep) ss))
(define (join ss)
(if (null? (cdr ss)) (car ss)
(f (car ss) (join (cdr ss)))))
(if (null? ss) "" (join ss)))
(define rand #f)
(define randint #f)
(let ((two31 #x80000000) (a (make-vector 56 -1)) (fptr #f))
(define (mod-diff x y) (modulo (- x y) two31)) ; generic version
; (define (mod-diff x y) (logand (- x y) #x7FFFFFFF)) ; fast version
(define (flip-cycle)
(do ((ii 1 (+ ii 1)) (jj 32 (+ jj 1))) ((< 55 jj))
(vector-set! a ii (mod-diff (vector-ref a ii) (vector-ref a jj))))
(do ((ii 25 (+ ii 1)) (jj 1 (+ jj 1))) ((< 55 ii))
(vector-set! a ii (mod-diff (vector-ref a ii) (vector-ref a jj))))
(set! fptr 54) (vector-ref a 55))
(define (init-rand seed)
(let* ((seed (mod-diff seed 0)) (prev seed) (next 1))
(vector-set! a 55 prev)
(do ((i 21 (modulo (+ i 21) 55))) ((zero? i))
(vector-set! a i next) (set! next (mod-diff prev next))
(set! seed (+ (quotient seed 2) (if (odd? seed) #x40000000 0)))
(set! next (mod-diff next seed)) (set! prev (vector-ref a i)))
(flip-cycle) (flip-cycle) (flip-cycle) (flip-cycle) (flip-cycle)))
(define (next-rand)
(if (negative? (vector-ref a fptr)) (flip-cycle)
(let ((next (vector-ref a fptr))) (set! fptr (- fptr 1)) next)))
(define (unif-rand m)
(let ((t (- two31 (modulo two31 m))))
(let loop ((r (next-rand)))
(if (<= t r) (loop (next-rand)) (modulo r m)))))
(init-rand 19380110) ; happy birthday donald e knuth
(set! rand (lambda seed
(cond ((null? seed) (/ (next-rand) two31))
((eq? (car seed) 'get) (cons fptr (vector->list a)))
((eq? (car seed) 'set) (set! fptr (caadr seed))
(set! a (list->vector (cdadr seed))))
(else (/ (init-rand (modulo (numerator
(inexact->exact (car seed))) two31)) two31)))))
(set! randint (lambda args
(cond ((null? (cdr args))
(if (< (car args) two31) (unif-rand (car args))
(floor (* (next-rand) (car args)))))
((< (car args) (cadr args))
(let ((span (- (cadr args) (car args))))
(+ (car args)
(if (< span two31) (unif-rand span)
(floor (* (next-rand) span))))))
(else (let ((span (- (car args) (cadr args))))
(- (car args)
(if (< span two31) (unif-rand span)
(floor (* (next-rand) span))))))))))
(define (shuffle x)
(do ((v (list->vector x)) (n (length x) (- n 1)))
((zero? n) (vector->list v))
(let* ((r (randint n)) (t (vector-ref v r)))
(vector-set! v r (vector-ref v (- n 1)))
(vector-set! v (- n 1) t))))
(define (hands)
(let loop ((deck (shuffle (range 52)))
(hands (list)))
(if (null? deck) hands
(loop (drop 13 deck)
(cons (sort (take 13 deck) >) hands)))))
(define (print-hands hands)
(define (blanks n) (display (make-string n #\space)))
(blanks 20) (display "NORTH") (newline)
(blanks 20) (display (cards "S" (car hands))) (newline)
(blanks 20) (display (cards "H" (car hands))) (newline)
(blanks 20) (display (cards "D" (car hands))) (newline)
(blanks 20) (display (cards "C" (car hands))) (newline)
(set! hands (cdr hands))
(display "WEST") (blanks 36) (display "EAST") (newline)
(display (cards "S" (car hands)))
(blanks (- 40 (string-length (cards "S" (car hands)))))
(display (cards "S" (cadr hands))) (newline)
(display (cards "H" (car hands)))
(blanks (- 40 (string-length (cards "H" (car hands)))))
(display (cards "H" (cadr hands))) (newline)
(display (cards "D" (car hands)))
(blanks (- 40 (string-length (cards "D" (car hands)))))
(display (cards "D" (cadr hands))) (newline)
(display (cards "C" (car hands)))
(blanks (- 40 (string-length (cards "C" (car hands)))))
(display (cards "C" (cadr hands))) (newline)
(set! hands (cddr hands))
(blanks 20) (display "SOUTH") (newline)
(blanks 20) (display (cards "S" (car hands))) (newline)
(blanks 20) (display (cards "H" (car hands))) (newline)
(blanks 20) (display (cards "D" (car hands))) (newline)
(blanks 20) (display (cards "C" (car hands))) (newline))
(define (cards suit hand)
(define (between lo hi) (lambda (x) (<= lo x hi)))
(cond ((string=? suit "S")
(string-join #\space (cons "S:"
(map face (filter (between 39 51) hand)))))
((string=? suit "H")
(string-join #\space (cons "H:"
(map face (filter (between 26 38) hand)))))
((string=? suit "D")
(string-join #\space (cons "D:"
(map face (filter (between 13 25) hand)))))
((string=? suit "C")
(string-join #\space (cons "C:"
(map face (filter (between 0 12) hand)))))))
(define (face card)
(case (modulo card 13)
((12) "A")
((11) "K")
((10) "Q")
((9) "J")
(else (number->string (+ (modulo card 13) 2)))))
(print-hands (hands))