Quick actions

cmd+k|ctrl+k

Navigation

Languages

TP2

Snippet info

Language

Clisp

Visibility

public

Author

mouhameddieng416

Created

2024-11-11T18:36:59.413933Z

Updated

2024-11-15T19:31:44.11178Z

(defvar *state* '((red-riding-hood (alive t) (place mum-home)) 
                  (wolf (alive t) (place wood)) 
                  (granny (alive t) (place granny-home))
                  (hunter (alive t) (place hunter-home))))

(defun set-value (person property val state)
  "Fonction permettant de mettre à jour la valeur  
   d'une propriété d'un protagoniste dans l'état du monde"
  (setf (cadr (assoc property (cdr (assoc person state)))) val))
(defun get-value (person property state)
  (cadr (assoc property (cdr (assoc person state)))))

(defun apply-effect (effect person value state)
  "Fonction qui applique un seul changement de valeur dans 
   l'état du monde après une action"
  (format t "~%APPLY EFFECT : ~s ~s ~s" effect person value)
  (cond 
    ((equal effect 'moved)  
     (set-value person 'place value state))
    ((equal effect 'eaten) 
     (progn 
       (set-value person 'alive nil state)
       (set-value person 'place 'wolf-belly state)))
    ((equal effect 'killed) 
     (set-value person 'alive nil state))
    ((equal effect 'risen) 
     (if value 
         (set-value person 'alive t state)))))

;; Appels de la fonction apply-effect à l'intérieur du main
 ; (print (apply-effect 'moved 'wolf 'granny-home *state*))
 ; (print *state*)
 ; (apply-effect 'eaten 'granny nil *state*) 
 ;  (print *state*)
 ; (apply-effect 'moved 'hunter 'granny-home *state*) 
 ;(print *state*)
; (apply-effect 'killed 'wolf nil *state*) 
 ; (print *state*)
 ; (apply-effect 'risen 'granny t *state*) 
 ; (print *state*)

;; Fonction pour obtenir la valeur d'une propriété pour une personne dans l'état

(defun rules (actor action &optional person cc state)
  "Fonction qui applique les effets des actions ou des dialogues."
  (cond
    ;; Action "kill"
    ((equal action 'kill)
     (apply-effect 'killed person cc state)  ; Applique l'effet killed
     (when (equal person 'wolf)  ; Si la personne tuée est le loup
       ;; Vérifie si le chaperon rouge est dans le ventre du loup
       (when (equal (get-value 'red-riding-hood 'place state) 'wolf-belly)
         (apply-effect 'risen 'red-riding-hood t state)
         (apply-effect 'moved 'red-riding-hood 'granny-home state))  ; Déplace le chaperon rouge
       ;; Vérifie si la grand-mère est dans le ventre du loup
       (when (equal (get-value 'granny 'place state) 'wolf-belly)
         (apply-effect 'risen 'granny t state)
         (apply-effect 'moved 'granny 'granny-home state))))  ; Déplace la grand-mère

    ;; Action "eat"
    ((equal action 'eat)
     (apply-effect 'eaten person nil state)  ; Applique l'effet eaten sur la personne
     (apply-effect 'moved person 'wolf-belly state))  ; Déplace la personne dans le ventre du loup

    
    ((equal action 'move)
     (apply-effect 'moved actor cc state))  

    
    ((equal action 'greet)
     (format t "Dialogue: ~a says hello to ~a." actor person))
    ((equal action 'give)
     (format t "Dialogue: ~a gives an item to ~a." actor person))
    ((equal action 'tell)
     (format t "Dialogue: ~a tells something to ~a." actor person))))

    (defvar *states*  '((RED-RIDING-HOOD (ALIVE T) (PLACE MUM-HOME)) 
  (WOLF (ALIVE T) (PLACE GRANNY-HOME)) 
  (GRANNY (ALIVE T) (PLACE GRANNY-HOME)) (HUNTER (ALIVE T) (PLACE WOOD))))
 (rules 'wolf 'eat 'granny 'granny-home *states*) 
 (print *states*)
INFO