yellowpigs.net
Sara Smollett
November 27, 1998
Programming in Scheme
Text Adventure in Computer Services
- ; Sara Smollett
- ; sara.ss
- ; the uncommented version
- ; yes, there really was a commented version
- ; with thanks to C.H.
-
- (define nl (list->string '(#\newline)))
- (define dn (lambda x '()))
-
- (define string-loc
- (lambda (str k)
- (letrec ((sl-h
- (lambda (str acc)
- (cond ((zero? (string-length str)) -1)
- ((equal? (string-ref str 0) k) acc)
- (else (sl-h (substring str 1 (string-length str))
- (add1 acc)))))))
- (sl-h str 0))))
-
- (define remove-voids
- (lambda (str)
- (cond ((null? str) '())
- ((void? (car str)) (remove-voids (cdr str)))
- (else (cons (car str) (remove-voids (cdr str)))))))
-
- (define exchange-blows
- (lambda (attacker victim)
- (let ((at-damage (random (attacker 'get-hp)))
- (vic-damage (random (victim 'get-hp))))
- (victim 'sub-hp at-damage)
- (if (victim 'is-dead)
- (victim 'die)
- (begin
- (attacker 'sub-hp vic-damage)
- (if (attacker 'is-dead)
- (attacker 'die)
- (string-append
- "You bonk "
- (string-append
- (victim 'get-sdesc)
- (string-append
- " for "
- (string-append
- (number->string at-damage)
- (string-append
- " and it bonks you back for "
- (string-append
- (number->string vic-damage)
- "."))))))))))))
-
- (define make-creature
- (lambda ()
- (letrec ((names '())
- (sdesc "")
- (ldesc "")
- (hp 0)
- (items '())
- (room 0)
- (interface dn)
- (this
- (lambda x
- (case (length x)
- ('0 (void))
- ('1
- (case (car x)
- ('get-names names)
- ('get-sdesc sdesc)
- ('get-ldesc ldesc)
- ('get-hp hp)
- ('get-items items)
- ('get-room room)
- ('get-interface interface)
- ('is-dead (if (< hp 0) #t #f))
- ('die (if (not (number? room))
- (begin
- (room 'remove-creature this)
- (let ((cur-items (room 'get-items)))
- (room 'set-items (append
- cur-items
- (this 'get-items)))
- (map
- (lambda (obj)
- (obj 'set-room room))
- (this 'get-items)))
- (set! room 0)
- (string-append
- (this 'get-sdesc) " says, 'Ack, I'm DEAD!'"))))
- (else (void))))
- ('2
- (case (car x)
- ('add-name (set! names (cons (cadr x) names)))
- ('set-names (set! names (cadr x)))
- ('remove-name (set! names (remove (cadr x) names)))
- ('is-name (if (member (cadr x) names)
- #t #f))
-
- ('set-sdesc (set! sdesc (cadr x)))
- ('set-ldesc (set! ldesc (cadr x)))
-
- ('add-hp (set! hp (+ hp (cadr x))))
- ('sub-hp (set! hp (- hp (cadr x))))
- ('set-hp (set! hp (cadr x)))
-
- ('add-item (set! items (cons (cadr x) items)))
- ('remove-item (set! items (remove (cadr x) items)))
- ('set-items (set! items (cadr x)))
-
- ('set-interface (set! interface (cadr x)))
-
- ('set-room (set! room (cadr x)))
- ('move (begin
- (room 'remove-creature this)
- (this 'set-room (cadr x))
- (room 'add-creature this)))
- (else (void))))
- (else (void))))))
- this)))
-
- (define make-player
- (lambda ()
- (letrec ((status '())
- (super (make-creature))
- (this
- (lambda x
- (case (length x)
- ('0 (void))
- ('1
- (case (car x)
- ('get-status status)
- ('die
- (begin
- (newline)
- (display "OH NO!! You're DEAD!")
- (newline)
- (display "Play again? ")
- (let ((iput (read)))
- (if (member iput '(y yes Y Yes YES))
- (game)
- (begin
- (finis)
- (stop))))))
- (else (apply super x))))
- ('2
- (case (car x)
- ('set-status (set! status (cadr x)))
- ('add-status (set! status (cons (cadr x) status)))
- ('remove-status (set! status (remove (cadr x) status)))
-
- (else (apply super x))))
- (else (apply super x))))))
- this)))
-
- (define make-room
- (lambda ()
- (letrec ((name "")
- (desc "")
- (exits '())
- (items '())
- (creatures '())
- (interface dn)
- (this
- (lambda x
- (case (length x)
- ('0 (void))
- ('1
- (case (car x)
- ('get-name name)
- ('get-desc desc)
- ('get-exits exits)
- ('get-items items)
- ('get-creatures creatures)
- ('get-interface interface)))
- ('2
- (case (car x)
- ('set-name (set! name (cadr x)))
- ('set-desc (set! desc (cadr x)))
-
- ('set-exits (set! exits (cadr x)))
- ('add-exit (set! exits (cons (cadr x) exits)))
- ('remove-exit (set! exits (remove (assoc (cadr x) exits)
exits)))
-
- ('set-items (set! items (cadr x)))
- ('add-item (set! items (cons (cadr x) items)))
- ('remove-item (set! items (remove (cadr x) items)))
-
- ('set-creatures (set! creatures (cadr x)))
- ('add-creature (set! creatures (cons (cadr x) creatures)))
- ('remove-creature (set! creatures (remove (cadr x) creatures)))
-
- ('set-interface (set! interface (cadr x)))))
- (else (void))))))
- this)))
-
- (define make-item
- (lambda ()
- (letrec ((names '())
- (sdesc "")
- (ldesc "")
- (room 0)
- (interface dn)
- (this
- (lambda x
- (case (length x)
- ('0 (void))
- ('1
- (case (car x)
- ('get-names names)
- ('get-sdesc sdesc)
- ('get-ldesc ldesc)
- ('get-room room)
- ('get-interface interface)))
- ('2
- (case (car x)
- ('set-names (set! names (cadr x)))
- ('add-name (set! names (cons (cadr x) names)))
- ('remove-name (set! names (remove (cadr x) names)))
- ('is-name (if (member (cadr x) names) #t #f))
-
- ('set-sdesc (set! sdesc (cadr x)))
- ('set-ldesc (set! ldesc (cadr x)))
-
- ('set-room (set! room (cadr x)))
- ('put (begin
- (this 'set-room (cadr x))
- ((cadr x) 'add-item this)))
-
- ('set-interface (set! interface (cadr x)))))
- (else (void))))))
- this)))
-
- (define dew
- (lambda (verb noun)
- (let* ((p-room (player 'get-room))
- (p-items (player 'get-items))
- (r-creas (p-room 'get-creatures))
- (r-items (p-room 'get-items))
- (m-output
- (map
- (let ((done-anything #f))
- (lambda (iface)
- (if (not done-anything)
- (let ((res (iface verb noun)))
- (if (not (null? res))
- (begin
- (set! done-anything #t)
- res))))))
- `(,(player 'get-interface)
- ,@(map
- (lambda (obj)
- (obj 'get-interface))
- p-items)
- ,(p-room 'get-interface)