codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
; rock paper scissors (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 (rps) (display "Welcome to Rock Paper Scissors. ") (newline) (call-with-current-continuation (lambda (return) (let ((cscore 0) (hscore 0)) (let play ((cthrow (list-ref '(rock paper scissors) (randint 3))) (hthrow 'none)) (do () ((member hthrow '(rock paper scissors quit))) (display "Enter R, P or S to make your throw, or Q to quit: ") (let ((h (read))) (case (string-ref (symbol->string h) 0) ((#\r #\R) (set! hthrow 'rock)) ((#\p #\P) (set! hthrow 'paper)) ((#\s #\S) (set! hthrow 'scissors)) ((#\q #\Q) (display "Thanks for playing.") (newline)(return))))) (display " I throw ") (display cthrow) (cond ((and (eq? cthrow 'rock) (eq? hthrow 'paper)) (set! hscore (+ hscore 1)) (display ". Your paper wraps my rock. You win.")) ((and (eq? cthrow 'paper) (eq? hthrow 'rock)) (set! cscore (+ cscore 1)) (display ". My paper wraps your rock. I win.")) ((and (eq? cthrow 'paper) (eq? hthrow 'scissors)) (set! hscore (+ hscore 1)) (display ". Your scissors cuts my paper. You win.")) ((and (eq? cthrow 'scissors) (eq? hthrow 'paper)) (set! cscore (+ cscore 1)) (display ". My scissors cuts your paper. I win.")) ((and (eq? cthrow 'scissors) (eq? hthrow 'rock)) (set! hscore (+ hscore 1)) (display ". Your rock blunts my scissors. You win.")) ((and (eq? cthrow 'rock) (eq? hthrow 'scissors)) (set! cscore (+ cscore 1)) (display ". My rock blunts your scissors. I win.")) ((eq? cthrow hthrow) (display ". Our throws are the same. It's a tie."))) (newline) (display " ") (cond ((< cscore hscore) (display "You are winning: ") (display hscore) (display " to ") (display cscore) (display ".")) ((< hscore cscore) (display "I am winning: ") (display cscore) (display " to ") (display hscore) (display ".")) (else (display "The score is tied at ") (display cscore) (display "."))) (newline) (play (list-ref '(rock paper scissors) (randint 3)) 'none)))))) (rps)
Private
[
?
]
Run code
Submit