[ create a new paste ] login | about

Project: programmingpraxis
Link: http://programmingpraxis.codepad.org/joeEXDmD    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Jan 16:
; guess the number

(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 (show . msg) (for-each display msg))
(define (show-line . msg) (apply show msg) (newline))

(define (get-yes-no)
  (let ((answer (char-downcase (string-ref
                  (symbol->string (read)) 0))))
    (if (member answer '(#\y #\n)) answer
      (begin (show-line "Please answer yes or no.")

(define (human-plays limit)
  (let ((n (randint 1 limit)))
    (show-line "I am thinking of a number from 1 to " limit)
    (display "What is your guess? ")
    (let loop ((g (read)) (k 0))
      (cond ((< g n)
              (show-line "Your guess is less than my number.")
              (show "What is your next guess? ")
              (loop (read) (+ k 1)))
            ((< n g)
              (show-line "My number is less than your guess.")
              (show "What is your next guess? ")
              (loop (read) (+ k 1)))
              (show-line "You guessed my number "
                         n " in " k " tries!")

(define (computer-plays limit)
  (show-line "Think of a number from 1 to " limit ".")
  (let loop ((lo 1) (hi limit) (k 0))
    (let ((mid (quotient (+ lo hi) 2)))
      (cond ((= lo mid)
              (show-line "I guessed your number "
                         lo " in " k " tries.")
      (else (show "Is your number less than " mid "? ")
            (case (get-yes-no)
            ((#\y) (loop lo mid (+ k 1)))
            ((#\n) (loop mid hi (+ k 1)))))))))

(define (play limit)
  (show-line "Let's play Guess-The-Number!")
  (show-line "You go first.")
  (let ((k-human (human-plays limit)))
    (show-line "Now it's my turn.")
    (let ((k-computer (computer-plays limit)))
      (cond ((< k-human k-computer)
              (show-line "You made less guesses than me.")
              (show-line "You win! Congratulations!"))
            ((< k-computer k-human)
              (show-line "I made less guesses than you.")
              (show-line "I win. Better luck next time!"))
              (show-line "We both made the same number of guesses.")
              (show-line "The game is a tie. Good game!")))
      (show "Would you like to play again? ")
      (if (char=? (get-yes-no) #\y)
          (play limit)
          (show-line "Thanks for playing! Good-bye!")))))

(play 100)

Let's play Guess-The-Number!
You go first.
I am thinking of a number from 1 to 100
What is your guess? 
Disallowed system call: SYS__newselect

Create a new paste based on this one