;; This file contains excerpts from the textbook Concrete ;; Abstractions: An Introduction to Computer Science Using Scheme, by ;; Max Hailperin, Barbara Kaiser, and Karl Knight, Copyright (c) 1998 ;; by the authors. Full text is available for free at ;; http://www.gustavus.edu/+max/concrete-abstractions.html ;; Chapter 14: Object-Oriented Programming ;; 14.5 An Application: Adventures in the Imaginary Land of Gack (define-class 'auto-person person-class '(threshold restlessness) '(maybe-act act)) (class/set-method! auto-person-class 'init (lambda (this name place threshold) (person^init this name place) (auto-person/set-threshold! this threshold) (auto-person/set-restlessness! this 0) (registry/add registry this))) (class/set-method! auto-person-class 'maybe-act (lambda (this) (let ((threshold (auto-person/get-threshold this)) (restlessness (auto-person/get-restlessness this))) (if (< restlessness threshold) (auto-person/set-restlessness! this (+ 1 restlessness)) (begin (auto-person/act this) (auto-person/set-restlessness! this 0)))))) (class/set-method! auto-person-class 'act (lambda (this) (let ((new-place (random-element (place/neighbors (auto-person/place this))))) (if new-place (auto-person/move-to this new-place)))))