#lang r7rs ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Position ADT ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; A position represents a location on the logical maze grid. ;; Coordinates are in grid units (row, col), NOT pixels. (define-library (pacman-project adt position) (import (scheme base)) (export make-position) (begin ;; make-position :: number, number -> position ;; Creates a new position object with row and column on the grid. (define (make-position row col) ;; row! :: number -> / (define (row! new-row) (set! row new-row)) ;; col! :: number -> / (define (col! new-col) (set! col new-col)) ;; equal? :: position -> boolean ;; Checks whether two positions have the same coordinates. (define (equal? other) (and (= row (other 'row)) (= col (other 'col)))) ;; move :: number, number -> position ;; Returns a new position shifted by delta-row and delta-col. (define (move delta-row delta-col) (make-position (+ row delta-row) (+ col delta-col))) ;; dispatch-position :: symbol -> any (define (dispatch-position msg) (cond ((eq? msg 'row) row) ((eq? msg 'col) col) ((eq? msg 'row!) row!) ((eq? msg 'col!) col!) ((eq? msg 'equal?) equal?) ((eq? msg 'move) move) (else (error "Position ADT -- Unknown message:" msg)))) dispatch-position)))