; general information ; ; file: dobby.l of wfr of gp of clos of lisp of ai ; type: pcl (clos) ; line: named after Dobby, as in Harry Potter #2 ; assign: number 1 ; date: Sept 9, 1999 ; ; elaboration: ; ; A program which toys with a little creature, a robot ; we will call Dobby -- after Dobby in "Harry Potter ; and the Chamber of Secrets" who was constanly banging ; his head into the wall. ; ;---------------------------------------------------------- ; Enter "clos" ( in-package "PCL" ) ;---------------------------------------------------------- ; Define a position class to represent a position in ; Dobby's world. ( defclass position () ( ( row :accessor position-row :initarg :row :initform 0 ) ( col :accessor position-col :initarg :col :initform 0 ) ) ) ( defmethod display ( ( p position ) ) ( princ "(" ) ( princ ( position-row p ) ) ( princ "," ) ( princ ( position-col p ) ) ( princ ")" ) ( terpri ) ) ;------------------------------------------------------- ; Define a method called FREE which returns YES if the position ; given by row and column values is free for Dobby to move in to, ; NO if it is not -- assuming Dobby is not interested in moving ; nowhere ( defmethod free ( ( row number ) ( col number) ) ( cond ( ( < row 1 ) 'no ) ( ( < col 1 ) 'no ) ( ( > row 8 ) 'no ) ( ( > col 8 ) 'no ) ( ( and ( = row 7 ) ( = col 4 ) ) 'no ) ( ( and ( = row 7 ) ( = col 5 ) ) 'no ) ( ( and ( = row 8 ) ( = col 4 ) ) 'no ) ( ( and ( = row 8 ) ( = col 5 ) ) 'no ) ( t 'yes ) ) ) ;------------------------------------------------------------ ; Define a method to place Dobby somewhere in his world. ( defmethod place ( ( row number ) ( col number ) ) ( setf *location* (make-instance 'position :row row :col col ) ) ) ;----------------------------------------------------------- ; Define a method to move Dobby north -- presuming the cell to ; the north is free ( defmethod north () ( setf ( position-row *location* ) ( - ( position-row *location* ) 1 ) ) ( prin1 'north ) ( terpri ) ) ;--------------------------------------------------------- ; Define a method to move Dobby south -- presuming the cell to ; the south is free ( defmethod south () ( setf ( position-row *location* ) ( + ( position-row *location* ) 1 ) ) ( prin1 'south ) ( terpri ) ) ;--------------------------------------------------------------- ; Define a method to move Dobby east -- presuming the cell to ; the east is free ( defmethod east () ( setf ( position-col *location* ) ( + ( position-col *location* ) 1 ) ) ( prin1 'east ) ( terpri ) ) ;------------------------------------------------------------------ ; Define a method to move Dobby west -- presuming the cell to ; the west is free ( defmethod west () ( setf ( position-col *location* ) ( - ( position-col *location* ) 1 ) ) ( prin1 'west ) ( terpri ) ) ;--------------------------------------------------------------------- ; Define a method called n which returns 0 if the cell to the ; north of Dobby is free, 1 if it is not free. ( defmethod n () ( setf row ( - ( position-row *location* ) 1 ) ) ( setf col ( position-col *location* ) ) ( if ( eq ( free row col ) 'yes ) nil t ) ) ;--------------------------------------------------------------------- ; Define a method called ne which returns 0 if the cell to the ; north-east of Dobby is free, 1 if it is not free. ( defmethod ne () ( setf row ( - ( position-row *location* ) 1 ) ) ( setf col ( + ( position-col *location* ) 1 ) ) ( if ( eq ( free row col ) 'yes ) nil t ) ) ;------------------------------------------------------------------- ; Define a method called e which returns 0 if the cell to the ; east of Dobby is free, 1 if it is not free. ( defmethod e () ( setf row ( position-row *location* ) ) ( setf col ( + ( position-col *location* ) 1 ) ) ( if ( eq ( free row col ) 'yes ) nil t ) ) ;-------------------------------------------------------------------- ; Define a method called se which returns 0 if the cell to the ; south-east of Dobby is free, 1 if it is not free. ( defmethod se () ( setf row ( + ( position-row *location* ) 1 ) ) ( setf col ( + ( position-col *location* ) 1 ) ) ( if ( eq ( free row col ) 'yes ) nil t ) ) ;-------------------------------------------------------------------- ; Define a method called s which returns 0 if the cell to the ; south of Dobby is free, 1 if it is not free. ( defmethod s () ( setf row ( + ( position-row *location* ) 1 ) ) ( setf col ( position-col *location* ) ) ( if ( eq ( free row col ) 'yes ) nil t ) ) ;--------------------------------------------------------------------- ; Define a method called sw which returns 0 if the cell to the ; south-west of Dobby is free, 1 if it is not free. ( defmethod sw () ( setf row ( + ( position-row *location* ) 1 ) ) ( setf col ( - ( position-col *location* ) 1 ) ) ( if ( eq ( free row col ) 'yes ) nil t ) ) ;--------------------------------------------------------------------- ; Define a method called w which returns 0 if the cell to the ; west of Dobby is free, 1 if it is not free. ( defmethod w () ( setf row ( position-row *location* ) ) ( setf col ( - ( position-col *location* ) 1 ) ) ( if ( eq ( free row col ) 'yes ) nil t ) ) ;---------------------------------------------------------------------- ; Define a method called nw which returns 0 if the cell to the ; north-west of Dobby is free, 1 if it is not free. ( defmethod nw () ( setf row ( - ( position-row *location* ) 1 ) ) ( setf col ( - ( position-col *location* ) 1 ) ) ( if ( eq ( free row col ) 'yes ) nil t ) ) ;----------------------------------------------------------------------- ; Define a method called move which will cause Dobby to take a step in ; one of four directions -- north, south, east, or west. ( defmethod move () ( if ( and ( or (n) (ne) ) ( not (e) ) ) (east) ( if ( and ( or (e) (se) ) ( not (s) ) ) (south) ( if ( and ( or (s) (sw) ) ( not (w) )) (west) (north) ) ) ) )