Cognitive Science Lectures
Genetic Algorithms
First LISP Sketch of a Program for Dobby
;--------------------------------------------------------------
; general information
;
; file:  dobby.l of clos of lisp of ai
; type:  pcl
; line:  dobby, as in Harry Potter #2
; date:  Spring 1999


  ( in-package "PCL" )

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  ( 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 )
  )

  ( defmethod loc ()
    ( display *location* )
  )

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  ( 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 )
    )
  )

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  ( defmethod place ( ( row number ) ( col number ) )
    ( setf *location* 
           ( make-instance 'position :row row :col col )
    )
  )

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  ( defmethod north ()
    ( setf ( position-row *location* ) 
           ( - ( position-row *location* ) 1 )
    )
    ( prin1 'north ) ( terpri )
  )

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  ( defmethod n ()
    ( setf row ( - ( position-row *location* ) 1 ) )
    ( setf col ( position-col *location* ) )
    ( if ( eq ( free row col ) 'yes )
      nil
      t
    )
  )