0 Replies - 955 Views - Last Post: 23 March 2013 - 01:58 PM

#1 ShawnStovall  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 11
  • Joined: 26-May 08

Code review request for simple Scheme project

Posted 23 March 2013 - 01:58 PM

I've been going though SICP (Reading though section 3.5, currently.) and have written a small set of procedures to help with making builds for one of my favorite RPGs, Dark Souls. I know this code isn't very idiomatic of Scheme, nor is it functional, (If posting in this sub-forum is incorrect, please move this thread.) but I would like to program closer to those styles. Any tips, criticisms, or recommendations will be greatly appreciated.

The main problem I think I have right now is that I seem to be stuck in a very imperative mindset. My first languages were C++ and Java (never really used outside academic exercise, though) and when solving programming problems, I instinctively want to go back to a very imperative, object-oriented style.

;; Slightly modified version of the disp function
;; from http://courses.csail.mit.edu/6.034s/04/scheme.html
(define displ
  (lambda args
    (letrec ((disp-in
              (lambda (arg)
                (if (null? arg)
                    'Done
                    (begin (display (car arg))
                           (disp-in (cdr arg)))))))
      (disp-in args)
      (newline))))

;; The following is heavily influenced by SICP...
(define (character class)
  (let ((vitality 0) (attunement 0)
        (endurance 0) (strength 0)
        (dexterity 0) (resistance 0)
        (intelligence 0) (faith 0)
        (soul-level 0) (soul-level-cap 120))
    (define (set-class!)
      (define (set-stats! sl v a e s d r i f)
        (set! soul-level sl)
        (set! vitality v)
        (set! attunement a)
        (set! endurance e)
        (set! strength s)
        (set! dexterity d)
        (set! resistance r)
        (set! intelligence i)
        (set! faith f))
      (cond ((eq? class 'warrior)
             (set-stats! 4 11 8 12 13 13 11 9 9))
            ((eq? class 'knight)
             (set-stats! 5 14 10 10 11 11 10 9 11))
            ((eq? class 'wanderer)
             (set-stats! 3 10 11 10 10 14 12 11 8))
            ((eq? class 'thief)
             (set-stats! 5 9 11 9 9 15 10 12 11))
            ((eq? class 'bandit)
             (set-stats! 4 12 8 14 14 9 11 8 10))
            ((eq? class 'hunter)
             (set-stats! 4 11 9 11 12 14 11 9 9))
            ((eq? class 'sorcerer)
             (set-stats! 3 8 15 8 9 11 8 15 8))
            ((eq? class 'pyromancer)
             (set-stats! 1 10 12 11 12 9 12 10 8))
            ((eq? class 'cleric)
             (set-stats! 2 11 11 9 12 8 11 8 14))
            ((eq? class 'deprived)
             (set-stats! 6 11 11 11 11 11 11 11 11))
            (else
             (error "That is not an available class!" class))))
    (set-class!)
    (define (level-update! level)
      (let ((new-level (+ level soul-level)))
        (if (> new-level soul-level-cap)
            (begin (displ "Cannot exceed maximum soul level: "
                          new-level " > " soul-level-cap)
                   (displ "Stat update ignored...")
                   #f)
            (begin (set! soul-level new-level)
                   #t))))
    (define (stat-update! stat amount)
      (define (set-stat! amount)
        (cond ((eq? stat 'vitality)
               (set! vitality (+ vitality amount)))
              ((eq? stat 'attunement)
               (set! attunement (+ attunement amount)))
              ((eq? stat 'endurance)
               (set! endurance (+ endurance amount)))
              ((eq? stat 'strength)
               (set! strength (+ strength amount)))
              ((eq? stat 'dexterity)
               (set! dexterity (+ dexterity amount)))
              ((eq? stat 'resistance)
               (set! resistance (+ resistance amount)))
              ((eq? stat 'intelligence)
               (set! intelligence (+ intelligence amount)))
              ((eq? stat 'faith)
               (set! faith (+ faith amount)))
              (else
               (error "That is not a valid stat!" stat)
               (set! soul-level (- soul-level amount)))))
      (if (level-update! amount)
          (set-stat! amount)
          #f))
    (define (display-stats)
      (displ "Soul Level: " soul-level " (Cap: " soul-level-cap ")")
      (displ "Vitality: " vitality)
      (displ "Attunement: " attunement)
      (displ "Endurance: " endurance)
      (displ "Strength: " strength)
      (displ "Dexterity: " dexterity)
      (displ "Resistance: " resistance)
      (displ "Intelligence: " intelligence)
      (displ "Faith: " faith))
    (define (dispatch message)
      (cond ((eq? message 'stat-update!) stat-update!)
            ((eq? message 'display-stats) display-stats)
            (else (error "Uknown message sent" message))))
    dispatch))

(define (stat-update! character stat amount)
  ((character 'stat-update!) stat amount)
  ((character 'display-stats)))
(define (display-stats character)
  ((character 'display-stats)))


Is This A Good Question/Topic? 0
  • +

Page 1 of 1