1

I am in the process of designing a small MUD game using racket. In my game, gems which are collected by the player are used to bribe guards. Currently if the player possesses more than 1 gem, the game will not let them bribe a guard.

Here is the relevant code:

;; This code processes the bribe command.
((equal? cmd 'bribe)
 (if (get-area-item rid 'guard)
     (begin
       (if (eq? (user-attribute 'gem) 1)
           (begin
             (hash-table-set! areasdb rid (replace-area-item (get-area rid) 'guard '(guard #f)))
             (user-add-value 'gem -1))
           (describe 'bribe-no-gem))
       (describe 'bribe))
     (describe 'bribe-blank)))
Oscar
  • 511
  • 2
  • 10
  • 25
  • I will say—if you're doing this in Racket, your code is a little wonky for Racket code. Especially the `require`, which should at the very least be `(require srfi/69)` instead of using the full `lib` syntax. If you're writing pure Scheme, though, you might want to use `#lang r5rs` instead of `#lang racket`. – Alexis King Apr 26 '15 at 21:25

1 Answers1

2

Change the (eq? (user-attribute 'gem) 1) to this, instead:

(>= (user-attribute 'gem) 1)

By the way, don't use eq? to compare numbers. Use = or (if you need to compare against generic objects) eqv?.


The OP asked how to suppress the bribe message if there are no gems. Here's how it might be done (I'm going for minimal changes to existing code, not for best style):

((equal? cmd 'bribe)
 (if (get-area-item rid 'guard)
     (if (>= (user-attribute 'gem) 1)
         (begin
           (hash-table-set! areasdb rid (replace-area-item (get-area rid) 'guard '(guard #f)))
           (user-add-value 'gem -1)
           (describe 'bribe))
         (describe 'bribe-no-gem))
     (describe 'bribe-blank)))

By the way, if the only reason you're using (describe 'bribe-blank) is because if requires an else branch, you can use when instead, like so:

((equal? cmd 'bribe)
 (when (get-area-item rid 'guard)
   (if (>= (user-attribute 'gem) 1)
       (begin
         (hash-table-set! areasdb rid (replace-area-item (get-area rid) 'guard '(guard #f)))
         (user-add-value 'gem -1)
         (describe 'bribe))
       (describe 'bribe-no-gem))))
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • This worked great, thanks so much for your help. One more question, do you know how I might only display the "You have no gems to bribe the guard with!" if the user has no gems? Currently it gets printed with "You search yourself for a gem..." following after it, which doesnt make sense :( – Oscar Apr 26 '15 at 21:11
  • 1
    Lemme reformat your code (only the first one; don't have time to reformat your second one) and see if the method is obvious. :-D – C. K. Young Apr 26 '15 at 21:15
  • @JamesPatterson Updated my post to answer your new question. – C. K. Young Apr 26 '15 at 21:23