Virtual Pet: State Transition Design Assignment

Pam Cutter and Kelly Schultz 2003, Alyce Brady 2017, 2021, Kalamazoo College, Kalamazoo, MI
Based on work done at Drexel University by JL Popyack & Paul Zoski

This is the third assignment (of 5) in a series to create a virtual pet.

In this assignment you will identify what might cause the virtual pet you designed and created in previous assignments to change from one state to another. You will document the possible state changes in a state diagram, as explained below.


EXAMPLE:

Consider a very simple "virtual dog" that is perpetually in one of four states: happy, angry, provoked, or lonely. This example also has two possible actions that the dog's owner can take: pat the dog and ignore the dog. A virtual dog that is lonely remains lonely until its owner pats it, which makes it happy. (Here, we say the dog has changed states from lonely to happy.) A virtual dog that is happy remains happy 90% of the time, but may also become angry 10% of the time, regardless of what the owner does. This means that the dog's state is evaluated periodically (say, every 30 seconds) and 10% of the time, the state will change. Note that this state change is not caused by any action by the owner; its mood (state) changes randomly. An angry dog stays angry until the owner pats it, which causes it to become provoked. A provoked dog remains in its provoked state 80% of the time, but may become lonely the other 20% of the time. (We could obviously have more states and more state changes than this, but these will serve for this example.)

The diagram above representing this behavior is known as a state diagram. A state diagram can be used to describe any system that has multiple states. In this case, it describes a system (the dog) with four states, signified by the circles labeled with the state's name. The system is always in exactly one of these states. The system may make a transition from one state to another as specified by the lines connecting the circles. The arrows signify which states are reachable from other states. For example, there are arrows connecting the happy state to itself or to the angry state, which means a happy dog can either remain happy or become angry. In this example, a happy dog cannot directly become lonely, but a lonely dog can become happy.

The virtual pets available commercially generally have many states and also many ways in which their owners can interact with them. They also sometimes have terminal states, that is, states from which the pet cannot exit. Typically, the pet is said to have died if this happens.


Design to Complete Before Lab:

  1. In the previous design assignment you designed a pet with 5 states. Now identify two owner actions that could cause the pet to change state, such as petting it, feeding it, ignoring it, etc. (You will eventually simulate owner actions with buttons on your page, such as a "Pat dog" button.)
  2. Create a state diagram, similar to the example above but with 5 states instead of 4, that depicts which states may be reached from other states, and under what conditions. Your state diagram should show your 5 states, the transitions caused by owner actions, and also transitions that have a certain probability of happening spontaneously at any given time.

    In particular, your state diagram should have at least one state that the pet can only leave by chance (such as "happy" and "provoked" in the diagram above) and at least one state that the pet can only leave based on an owner action (such as "angry" and "lonely" in the example). The other states can have transitions that are by chance, by owner action, or a combination of the two. If the state can only be left by chance the probabilities must add up to 100%. Note that an owner action may just cause the pet to stay in the current state. That is indicated by a loop. The "Ignore dog" action in our example just causes the pet to remain in the same state.