Tamagotchi emulator

From Rosetta Code
Revision as of 08:16, 4 December 2015 by rosettacode>G.Brougnard (Added EchoLisp)
Tamagotchi emulator is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

If you don't know what Tamagotchi is, take a look at the Wikipedia page about it.

This task is about creating a Tamagotchi emulator, a virtual pet that you must take care of.

Your virtual pet must, like real pets, at least: get hungry, get bored, age and poop!
Against hunger, you must create a way to feed it. Against boredom, you must play with or pet it. The poop must be cleaned, otherwise the pet might get sick and if it is not cured, it might die from its disease. Finally, the pet should grow older and eventually die.

On screen, your program must display the virtual pet status data - age, hunger and happiness levels, when the pet poops, its poop must also be displayed. Ah, well, an avatar of the pet must be there too, but I guess that's obvious!

What else? Well, use your creativity…
Every pet needs a name. What kind of games, or ‘mini games’ one can play with his pet? And so on!

But, above of all, have fun!

EchoLisp

The tamagotchi status is saved in permanent storage. The tamagotchi cycles can be started manually, or in the preferences function, or at predefined intervals : every function. The following code may be loaded from the EchoLisp library : (load 'tamagotchi). This tamagotchi does not play, but gets bored, and needs to talk. It must be feed two times between each cycle. It will die at age around 42 (42 cycles). <lang scheme>

(define-constant CYCLE_TIME 30000) (string-delimiter "") (struct tamagotchi (name age food poop bored))

utility
display tamagotchi thoughts
transitive verb + complement

(define (tama-talk tama) (writeln (string-append

  "😮 : "
  (word-random #:any '( verbe trans inf -vintran)))
  " les " 
  (word-random #:any '(nom pluriel))))
load tamagotchi from persistent storage into *tama* global

(define (run-tamagotchi) (if (null? (local-get '*tama*)) (writeln "Please (make-tamagotchi <name>)") (begin (make-tamagotchi (tamagotchi-name *tama*) *tama*) (tama-cycle *tama*) (writeln (tama-health *tama*)))))

make a new tamagotchi
or instantiate an existing
tama
instance ot tamagotchi structure

(define (make-tamagotchi name (tama null)) (when (null? tama) (set! tama (tamagotchi name 0 2 0 0)) (define-global '*tama* tama) (local-put '*tama*))

define the <name> procedure
perform user action / save tamagotchi / display status

(define-global name (lambda (action) (define tama (local-get '*tama*)) [case action ((feed) (set-tamagotchi-food! tama (1+ (tamagotchi-food tama)))) ((talk) (tama-talk tama) (set-tamagotchi-bored! tama (max 0 (1- (tamagotchi-bored tama))))) ((clean) (set-tamagotchi-poop! tama (max 0 (1- (tamagotchi-poop tama))))) ((look) #t)

debug actions

((_cycle) (tama-cycle tama)) ((_reset) (set! *tama* null) (local-put '*tama*)) ((_kill) (set-tamagotchi-age! tama 44)) ((_self) (writeln tama)) (else (writeln "actions: feed/talk/clean/look"))]

(local-put '*tama*) (tama-health tama))))

every n msec
get older / eat food / get bored / poop

(define (tama-cycle tama) (when (tama-alive tama) (set-tamagotchi-age! tama (1+ (tamagotchi-age tama))) (set-tamagotchi-bored! tama (+ (tamagotchi-bored tama) (random 2))) (set-tamagotchi-food! tama (max 0 (- (tamagotchi-food tama) 2))) (set-tamagotchi-poop! tama (+ (tamagotchi-poop tama) (random 2)))) (local-put '*tama*))

compute sickness (too much poop, too much food, too much bored)

(define (tama-sick tama) (+ (tamagotchi-poop tama) (tamagotchi-bored tama) (max 0 (- (tamagotchi-age tama) 32)) ;; die at 42 (abs (- (tamagotchi-food tama) 2))))

alive if sickness <= 10

(define (tama-alive tama) (<= (tama-sick tama) 10))

display num icons from a list

(define (icons list num) (for/fold (str " ") ((i [in-range 0 num] )) (string-append str (list-ref list (random (length list))))))

display boredom/food/poops icons

(define (tama-status tama) (if (tama-alive tama) (string-append " [ " (icons '(💤 💭 ❓ ) (tamagotchi-bored tama)) (icons '(🍼 🍔 🍟 🍰 🍜 ) (tamagotchi-food tama)) (icons '(💩) (tamagotchi-poop tama)) " ]") " R.I.P" ))

display health status = f(sickness)

(define (tama-health tama) (define sick (tama-sick tama)) ;;(writeln 'health:sick°= sick) (string-append (format "%a (🎂 %d) " (tamagotchi-name tama)(tamagotchi-age tama)) (cond ([<= sick 2] (icons '(😄 😃 😀 😊 😎️ 👍 ) 1 ))  ;; ok <= 2 ([<= sick 4] (icons '(😪 😥 😰 😓 ) 1)) ([<= sick 6] (icons '(😩 😫 ) 1)) ([<= sick 10] (icons '(😡 😱 ) 1)) ;; very bad (else (icons '(❌ 💀 👽 😇 ) 1))) ;; dead

   (tama-status tama)))
   
timer operations
run tama-proc = cycle every CYCLE_TIME msec

(define (tama-proc n) (define tama (local-get '*tama*)) (when (!null? tama) (tama-cycle tama) (writeln (tama-health tama))))

boot
manual boot or use (preferences) function

(every CYCLE_TIME tama-proc) (run-tamagotchi)

</lang>

Output:
(lib 'struct)
(lib 'sql) ;; for words
(lib 'words)
(lib 'timer)
(lib 'dico.fr);; will talk in french
(load 'tamagotchi)
Please (make-tamagotchi <name>)   
    
    (make-tamagotchi 'albert)

    albert (🎂 1) 😓 [ 💭 ]    
    albert (🎂 2) 😰 [ 💭❓ ]    ;; needs to talk
    (albert 'talk)
    😮 : déléaturer     les      fidèles    
    albert (🎂 2) 😓 [ ❓ ]
    (albert 'talk)
    😮 : facetter     les      décorations    
    albert (🎂 2) 😃 [ ]
    albert (🎂 3) 😥 [ 💤 💩 ]    
    (albert 'feed)
    albert (🎂 3) 😪 [ 💤 🍔 💩 ] ;; needs cleaning
    (albert 'clean)
    albert (🎂 3) 😊 [ 💭 🍟 ]
    (albert 'talk)
    😮 : manifester     les      canyons    
    albert (🎂 3) 😃 [ 🍰 ] ;; all is ok
    albert (🎂 4) 👍 [ ]    
    albert (🎂 5) 😃 [ ]    
    albert (🎂 6) 😥 [ ❓ 💩 ]    
    (albert 'clean)
    albert (🎂 6) 😪 [ 💤 ]
    albert (🎂 7) 😰 [ 💭 💩 ]    
    albert (🎂 8) 😓 [ 💭 💩 ]    
    (for ((i 10)) (albert 'feed))
    albert (🎂 8) 😱 [ 💭 🍔🍼🍔🍼🍜🍼🍟🍔🍔🍟 💩 ] ;; very sick
    albert (🎂 9) 😱 [ ❓ 🍰🍰🍟🍟🍼🍟🍜🍜 💩💩 ]    
    (albert 'talk)
    😮 : assortir     les      déchiffrages    
    albert (🎂 9) 😱 [ 🍼🍼🍟🍟🍜🍔🍼🍰 💩💩 ]
    albert (🎂 10) 😡 [ 💭 🍰🍟🍟🍟🍟🍜 💩💩💩 ]   
 
    (for ((i 14)) (albert 'feed)) ;; do'nt feed it too much .. it will die
    albert (🎂 10) 😇 R.I.P ;; died at age 10
    albert (🎂 10) ❌ R.I.P    
    albert (🎂 10) 👽 R.I.P    
    albert (🎂 10) 😇 R.I.P    
    albert (🎂 10) 😇 R.I.P  
  
    (make-tamagotchi 'simon)
    simon
    simon (🎂 1) 😓 [ 💤 💩 ]