Tamagotchi emulator: Difference between revisions
Content added Content deleted
No edit summary |
(Added EchoLisp) |
||
Line 13: | Line 13: | ||
But, above of all, have fun! |
But, above of all, have fun! |
||
=={{header|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> |
|||
{{out}} |
|||
<pre> |
|||
(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) ๐ [ ๐ค ๐ฉ ] |
|||
</pre> |