Tamagotchi and Giga Pets were small electronic devices which simulated a little virtual pet. This pet had several stats, like health, hunger, etc.
I recently wrote this example:
import msvcrt,os,sys;d=m=100;h=s=t=p=0;f=10
while 1:
os.system('cls'if os.name=='nt'else'clear');print("health:",d,"\nhunger:",h,"\nsleep:",s,"\nfood:",f,"\npotions:",p,"\nmoney:",m);t+=1
if msvcrt.kbhit():
k=ord(msvcrt.getch())
if k==102 and h>8 and f:f-=1;h-=9
if k==115:s=0
if k==112 and p:p-=1;d+=9
if k==98 and m>8:m-=9;p+=1
if k==116 and m>8:m-=9;f+=1
if t>99:
t=0;h+=1;s+=1
if s>80:s=0;h+=9
if h>80:d-=1
if d<1:sys.exit(0)
if d>79:m+=1
This is a bare-bones virtual pet in 467 bytes! I then wondered how well the code golf pros could do, so now, the challenge.
The Challenge
Make a program that tracks 6 stats of a virtual pet, and updates them over time and in response to user input. The stats are: health and money (starting at 100), food (starting at 10), and hunger, sleep, and potions (starting at 0).
The program should update the values in response to the following events:
While the program is receiving no input, it should perform updates at regular intervals (the interval between updates should not be shorter than half a second nor longer than one second). Each update does the following:
- Hunger and Sleep each increase by 1.
- If Hunger is 80 or above, Health decreases by 1.
- If Sleep is 80 or above, it is reset to 0, and Hunger increases by an additional 9.
- If Health is 80 or above, Money increases by 1.
- If Health is 0, the program exits.
The program must also respond immediately upon the following keypresses by the user (this means that you will need to use a language feature or library that can detect a key being pressed and respond to it immediately, rather than just reading from standard input), performing the following actions:
f
: If Hunger is greater than 8 and Food is nonzero, then Food is decreased by 1 and Hunger is decreased by 9.s
: Sleep is reset to 0.p
: If Potions is greater than zero, Potions is decreased by 1 and Health is increased by 9.b
: If Money is greater than 8, then Money is decreased by 9 and Potions are increased by 1.t
: If Money is greater than 8, then Money is decreased by 9, and Food is increased by by 1.
Whenever the values of the stats change, they must be displayed on screen in the form Stat: value
. All six stats must be displayed whenever any of them changes; and the stats within a display must be separated either by commas or by newlines.
This challenge follows normal code-golf rules: the shortest program complying with the specification above wins. (Note that as usual, if the language is newer than the competition, the submission must be marked as non-competing.)