Please read the link https://en.wikipedia.org/wiki/Tamagotchi to know more about Tamagotchi This is a very basic simulation of the game using command line
In Tamagotchi world two types of event occurs
- external event (initiated by the user) [e.g feeding the pet, playing with the pet]
- internal event (auto generated by the game itself) [pet getting hungry, pet passing motion]
As such the game should be able to handle both of these events. The way these events are handled is by using the concept of single threaded event loop. The javascript engine in any browser is a classical example of single threaded event loop. Javascript in a similar fashion handles events like "mouse click", "network connection success" etc by having a single queue where event are added and a single threaded loop which processes those event.
There is also a need to maintain the notion of time passage. The passage of time in this case would be virtual time and has no relation with the real world time. This is generated in Tamagotchi world using the concept of ticks (similar to how processor generates tick based upon the clock). The system allows two things to be configured
- How often to generate tick. For example, we can specify that one tick should be generated every 1 sec. Where the 1 sec is real world 1 sec. Tthis is achieved through the java
sleep
API - What does one tick means in Tamagotchi world. In here we can say 1 tick = 1 hr in tamagotchi world. This conversion will be used by events to emulate the notion of passage of time
All event needs to extend from the abstract class Event
shown below
public abstract class Event {
public enum EventType {
FEED_EVENT,
AWAKE_EVENT,
SLEEP_EVENT,
PLAY_EVENT,
POOP_EVENT,
CLEAN_EVENT,
AGE_EVENT
}
protected final Configuration configuration;
protected final Consumer<Class<? extends Event>> generateEvent;
protected final Pet pet;
protected TimeUtils time;
public Event(Pet pet, Configuration configuration, Consumer<Class<? extends Event>>
generateEvent) {
this.pet = pet;
this.configuration = configuration;
this.generateEvent = generateEvent;
this.time = new TimeUtils(configuration);
}
public abstract Optional<Notification> action(long currTick);
}
Few key things to note
- Declaration of enum for new event (The universe uses this key to keep track of when an event occur)
- Configuration: class contains all configuration data e.g. how often to feel hungry, maximum pet age
- GenerateEvent: allows any event to also create another event. All you need to do is
generateEvent.accept(<Event Class Name>)
- Time: class to convert tick to virtual time
- action method is where the main logic goes. The universe class calls this method for every tick
- The action method can return an Optional Message Object which will be passed back to the outside world e.g. GUI, CLI etc
The universe is created by passing
- pet
- configuration information.
The universe is a single thread loop that does the following
- generate a tick to indicate the passage of time
- process any user generated events
- process implicit events like hunger etc
The universe ends when the pet dies
The Pet class maintains all the state
- name
- age
- sex
- stats like happiness , health
- state [AWAKE, SLEEP, PLAYING]
- event tracker to keep track when an event occurred
The pet dies if any of the following condition are met
- any of the stats fall below zero
- age > max age
Since the Tamagotchi world is designed as a Event Loop there is no need of locks to ensure the safety of the Pet object as at any given time only one event is acting on the Pet object
- java 1.8
- building the jar using the command
./gradlew jar
- locate the jar in the folder
build\libs
- launch the game via the command
java -jar tamagotchi-1.0-SNAPSHOT.jar
./gradlew clean
./gradlew jar
./gradlew test
./gradlew check
./gradlew test jacocoTestReport