Tamagotchi emulator: Difference between revisions

→‎Java: Removed unnecessary package declaration, left over from IDE
(→‎Java: Removed unnecessary package declaration, left over from IDE)
Tags: Mobile edit Mobile web edit
 
(7 intermediate revisions by 5 users not shown)
Line 13:
 
But, above of all, have fun!
=={{header|Common Lisp}}==
1. Score
 
Parameters belong to the interval [0..9]. The score calculation rules are as follows :
 
<pre>while alive do
case event of
feed : need (feed -3)
wash : need (wash -9)
play : need (play -5)
cure : sick = no
when new turn do
condition
age > 77 : rip
sick w/ pr = 0.33 : rip
feed + wash + play > 24 w/ pr = 0.5 : sick = yes
need (feed +9)
need (wash +5)
need (play +3)
 
need : action, delta -> action + delta + random (2)</pre>
 
2. Program
 
A very simple program.
 
<lang lisp>; 22.07.19
 
(defconstant +length-of-day+ 60) ; seconds
 
(defmacro need (action delta &optional emoji)
`(progn
(if (plusp ,delta)
(setf ,action (min (+ ,action ,delta (random 2)) 9))
(setf ,action (max (+ ,action ,delta (random 2)) 0)))
(when ,emoji
(format t "🤖🪄 : 😀 ✨~a✨ (~a)~%" ,emoji ,action))))
 
(defun yokai ()
(let ((feed 5) (wash 5) (play 5) (age 0) (sick "no") time rip)
(setf time (get-universal-time))
(write-string "🤖 Feed, wash, play, cure, show ? ")
(loop until rip do
(when (listen)
(case (read-from-string (read-line) nil)
(feed (need feed -3 "🍼"))
(wash (need wash -9 "🛁"))
(play (need play -5 "🏓"))
(cure (setf sick "no"))
(show (format t "🍽️ ~a 💦 ~a 🎰 ~a 🌡️ ~a~%" feed wash play sick)))
(format t "yōkai (~a y) > " age))
(when (> (get-universal-time) (+ time +length-of-day+))
(setf time (get-universal-time))
(incf age)
(cond ((> age 77)
(setf rip "🗓️"))
((and (string= sick "yes") (zerop (random 2)))
(setf rip "🚑"))
((and (> (+ feed wash play) 24) (zerop (random 3)))
(setf sick "yes")))
(need feed +9)
(need wash +5)
(need play +3))
(sleep 1))
(format t "~%🎃 Rest in peace Yōkai ~a~%" rip)))</lang>
 
3. Execution
 
{{out}}
<pre>(yokai)
🤖 Feed, wash, play, cure, show ? show
🍼 5 🛁 5 🕹️ 5 🌡️ no
yōkai (0 y) > play
🤖🪄 : 😀 ✨🏓✨ (0)
...
🎃 Rest in peace Yōkai 🚑</pre>
 
That's all Folks !
 
''cyril nocton (cyril.nocton@gmail.com)''
 
=={{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).
<langsyntaxhighlight lang="scheme">
 
(define-constant CYCLE_TIME 30000) ;; 30 sec for tests, may be 4 hours, 1 day ...
Line 213 ⟶ 132:
(run-tamagotchi)
 
</syntaxhighlight>
</lang>
{{out}}
User commands are function calls : (albert 'clean). The rest is automatic display : one status line / cycle.
Line 293 ⟶ 212:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">( current object )
0 value o
' o >body constant 'o
Line 391 ⟶ 310:
spawn look
cr
</syntaxhighlight>
</lang>
 
Boredom kills tama faster than anything else.
Line 397 ⟶ 316:
=={{header|Go}}==
This is inspired by the EchoLisp entry but written as a terminal rather than a browser application and altered in a number of respects. In particular, it uses hard-coded word lists of transitive verbs and plural nouns rather than downloading a suitable 'free' dictionary (I couldn't find one anyway) and consequently the tamagotchi's vocabulary is somewhat limited!
<langsyntaxhighlight lang="go">package main
 
import (
Line 626 ⟶ 545:
health()
}
}</langsyntaxhighlight>
 
{{out}}
Line 721 ⟶ 640:
? feed 9
jeremy (🎂 39) 😇 16 R.I.P
</pre>
 
=={{header|Java}}==
 
This is a direct translation of [[Tamagotchi emulator#Go|the Go version]]. As such, the output will be similar if not identical.
 
The code does not use any Java 8+ features so it may function on lower versions.
<syntaxhighlight lang="Java">
 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
 
class Tamagotchi {
public String name;
public int age,bored,food,poop;
}
 
public class TamagotchiGame {
Tamagotchi tama;//current Tamagotchi
Random random = new Random(); // pseudo random number generator
String[] verbs = {
"Ask", "Ban", "Bash", "Bite", "Break", "Build",
"Cut", "Dig", "Drag", "Drop", "Drink", "Enjoy",
"Eat", "End", "Feed", "Fill", "Force", "Grasp",
"Gas", "Get", "Grab", "Grip", "Hoist", "House",
"Ice", "Ink", "Join", "Kick", "Leave", "Marry",
"Mix", "Nab", "Nail", "Open", "Press", "Quash",
"Rub", "Run", "Save", "Snap", "Taste", "Touch",
"Use", "Vet", "View", "Wash", "Xerox", "Yield",
};
String[] nouns = {
"arms", "bugs", "boots", "bowls", "cabins", "cigars",
"dogs", "eggs", "fakes", "flags", "greens", "guests",
"hens", "hogs", "items", "jowls", "jewels", "juices",
"kits", "logs", "lamps", "lions", "levers", "lemons",
"maps", "mugs", "names", "nests", "nights", "nurses",
"orbs", "owls", "pages", "posts", "quests", "quotas",
"rats", "ribs", "roots", "rules", "salads", "sauces",
"toys", "urns", "vines", "words", "waters", "zebras",
};
String[] boredIcons = {"💤", "💭", "❓"};
String[] foodIcons = {"🍼", "🍔", "🍟", "🍰", "🍜"};
String[] poopIcons = {"💩"};
String[] sickIcons1 = {"😄", "😃", "😀", "😊", "😎", "👍"};//ok
String[] sickIcons2 = {"😪", "😥", "😰", "😓"};//ailing
String[] sickIcons3 = {"😩", "😫"};//bad
String[] sickIcons4 = {"😡", "😱"};//very bad
String[] sickIcons5 = {"❌", "💀", "👽", "😇"};//dead
String brace(String string) {
return String.format("{ %s }", string);
}
void create(String name) {
tama = new Tamagotchi();
tama.name = name;
tama.age = 0;
tama.bored = 0;
tama.food = 2;
tama.poop = 0;
}
boolean alive() { // alive if sickness <= 10
return sickness() <= 10;
}
void feed() {
tama.food++;
}
void play() {//may or may not help with boredom
tama.bored = Math.max(0, tama.bored - random.nextInt(2));
}
void talk() {
String verb = verbs[random.nextInt(verbs.length)];
String noun = nouns[random.nextInt(nouns.length)];
System.out.printf("😮 : %s the %s.%n", verb, noun);
tama.bored = Math.max(0, tama.bored - 1);
}
void clean() {
tama.poop = Math.max(0, tama.poop - 1);
}
void idle() {//renamed from wait() due to wait being an existing method from the Object class
tama.age++;
tama.bored += random.nextInt(2);
tama.food = Math.max(0, tama.food - 2);
tama.poop += random.nextInt(2);
}
String status() {// get boredom/food/poop icons
if(alive()) {
StringBuilder b = new StringBuilder(),
f = new StringBuilder(),
p = new StringBuilder();
for(int i = 0; i < tama.bored; i++) {
b.append(boredIcons[random.nextInt(boredIcons.length)]);
}
for(int i = 0; i < tama.food; i++) {
f.append(foodIcons[random.nextInt(foodIcons.length)]);
}
for(int i = 0; i < tama.poop; i++) {
p.append(poopIcons[random.nextInt(poopIcons.length)]);
}
return String.format("%s %s %s", brace(b.toString()), brace(f.toString()), brace(p.toString()));
}
return " R.I.P";
}
//too much boredom/food/poop
int sickness() {
//dies at age 42 at the latest
return tama.poop + tama.bored + Math.max(0, tama.age - 32) + Math.abs(tama.food - 2);
}
//get health status from sickness level
void health() {
int s = sickness();
String icon;
switch(s) {
case 0:
case 1:
case 2:
icon = sickIcons1[random.nextInt(sickIcons1.length)];
break;
case 3:
case 4:
icon = sickIcons2[random.nextInt(sickIcons2.length)];
break;
case 5:
case 6:
icon = sickIcons3[random.nextInt(sickIcons3.length)];
break;
case 7:
case 8:
case 9:
case 10:
icon = sickIcons4[random.nextInt(sickIcons4.length)];
break;
default:
icon = sickIcons5[random.nextInt(sickIcons5.length)];
break;
}
System.out.printf("%s (🎂 %d) %s %d %s%n%n", tama.name, tama.age, icon, s, status());
}
void blurb() {
System.out.println("When the '?' prompt appears, enter an action optionally");
System.out.println("followed by the number of repetitions from 1 to 9.");
System.out.println("If no repetitions are specified, one will be assumed.");
System.out.println("The available options are: feed, play, talk, clean or wait.\n");
}
public static void main(String[] args) {
TamagotchiGame game = new TamagotchiGame();
game.random.setSeed(System.nanoTime());
System.out.println(" TAMAGOTCHI EMULATOR");
System.out.println(" ===================\n");
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of your tamagotchi : ");
String name = scanner.nextLine().toLowerCase().trim();
game.create(name);
System.out.printf("%n%s (age) health {bored} {food} {poop}%n%n", "name");
game.health();
game.blurb();
ArrayList<String> commands = new ArrayList<>(List.of("feed", "play", "talk", "clean", "wait"));
int count = 0;
while(game.alive()) {
System.out.print("? ");
String input = scanner.nextLine().toLowerCase().trim();
String[] items = input.split(" ");
if(items.length > 2) continue;
String action = items[0];
if(!commands.contains(action)) {
continue;
}
int reps;
if(items.length == 2) {
reps = Integer.parseInt(items[1]);
} else {
reps = 1;
}
for(int i = 0; i < reps; i++) {
switch(action) {
case "feed":
game.feed();
break;
case "play":
game.play();
break;
case "talk":
game.talk();
break;
case "wait":
game.idle();
break;
}
//simulate a wait on every third (non-wait) action
if(!action.equals("wait")) {
count++;
if(count%3==0) {
game.idle();
}
}
}
game.health();
}
scanner.close();
}
}
 
 
</syntaxhighlight>
{{out}}
<pre>
TAMAGOTCHI EMULATOR
===================
 
Enter the name of your tamagotchi : jeremy
 
name (age) health {bored} {food} {poop}
 
jeremy (🎂 0) 😀 0 { } { 🍜🍜 } { }
 
When the '?' prompt appears, enter an action optionally
followed by the number of repetitions from 1 to 9.
If no repetitions are specified, one will be assumed.
The available options are: feed, play, talk, clean or wait.
 
? feed 4
jeremy (🎂 1) 😥 3 { } { 🍟🍜🍟🍜 } { 💩 }
 
? wait 4
jeremy (🎂 5) 😫 5 { 💭💤 } { } { 💩 }
 
? clean 2
jeremy (🎂 6) 😫 6 { 💭💭💭 } { } { 💩 }
 
? talk 4
😮 : Grasp the names.
😮 : Vet the greens.
😮 : Grasp the urns.
😮 : Dig the zebras.
jeremy (🎂 7) 😰 3 { } { } { 💩 }
 
? feed 6
jeremy (🎂 9) 😊 2 { } { 🍼🍟 } { 💩💩 }
 
? play 2
jeremy (🎂 10) 😩 5 { } { } { 💩💩💩 }
 
? talk
😮 : Touch the eggs.
jeremy (🎂 10) 😫 5 { } { } { 💩💩💩 }
 
</pre>
 
=={{header|Julia}}==
GUI version with the Gtk library.
<langsyntaxhighlight lang="julia">using Gtk, GtkUtilities, Cairo, Images
 
mutable struct Pet
Line 1,017 ⟶ 1,212:
 
TamagotchiApp()
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import random, strutils
 
const
Line 1,178 ⟶ 1,373:
tama.wait()
 
tama.displayHealth()</langsyntaxhighlight>
 
{{out}}
Line 1,185 ⟶ 1,380:
=={{header|Phix}}==
{{trans|Go|but with a dirt simple GUI and much harsher gameplay}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo/rosetta/tamagotchi.exw
Line 1,321 ⟶ 1,516:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Objeck}}==
GUI based Tamagotchi that sleeps at night. Not cleaning up poop makes Tamagotchi sicker faster. Implementation has associated sound effects.
 
<langsyntaxhighlight lang="objeck">use Game.SDL2;
use Collection;
use Game.Framework;
Line 1,695 ⟶ 1,890:
WAIT := 5
}
</syntaxhighlight>
</lang>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<langsyntaxhighlight lang="go">import os
import strconv
import rand
Line 1,910 ⟶ 2,105:
tama.health()
}
}</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 1,918 ⟶ 2,113:
{{libheader|Wren-ioutil}}
{{libheader|Wren-str}}
<langsyntaxhighlight ecmascriptlang="wren">import "./dynamic" for Struct
import "random" for Random
import "./fmt" for Fmt
import "./ioutil" for Input
import "./str" for Str
 
var fields = ["name", "age", "bored", "food", "poop"]
Line 2,080 ⟶ 2,275:
}
health.call()
}</langsyntaxhighlight>
 
{{out}}
5

edits