In most programming languages, the counting of instances start with 0. So if there are 10 instances of rabbits, then the first instance of the rabbits is rabbit #0, and the last one is rabbit #9. To specify a single instance in the set, simply use square brackets immediately after the character, such as rabbit[2] -- this is the rabbit instance #2. On the other hand, to specify a range, we need two numbers within the brackets, such as rabbits[2,6] -- this includes rabbit instance #2, #3, #4, #5, and #6.
Consider the following game plans:
There are 10 rabbits. Rabbit[0] moves down. Rabbits[1,3] move right. Rabbits[4,9] move left. | Rabbit #0 will move down by itself. Instances #1, #2, and #3 will move right. Instances #4 through #9 will move left. |
There are 10 rabbits and 5 foxes. When rabbit[0] is not dead, it turns yellow. | When pronouns (it/they/etc) are used with subsets of characters, they will refer to the instances mentioned. In this example, 'it' refers to rabbit[0]. |
There are 10 rabbits and 5 foxes. When rabbits[2,5] see a fox, they flee from the fox. | When a whole range of instances is mentioned, the pronoun refers to any one instance in the range. In this example, if any rabbit instances #2 through #5 sees a fox, they will flee from the fox. |
Look at the following game plan, what do you think it will do?
There are 2 balls.
When the up arrow is pressed and ball[0] is not dead, it moves up.
When the down arrow is pressed and ball[0] is not dead, it moves down.
When the right arrow is pressed and ball[0] is not dead, it moves right.
When the left arrow is pressed and ball[0] is not dead, it moves left.
When ball[0] touches ball[1], game over.
You can address the array index in a different way, as shown in the following example:
There are 20 rabbits near the bottom.
When the index of a rabbit is less than 1, it turns yellow.
When the up arrow is pressed and the rabbit is yellow, it moves up.
When the down arrow is pressed and the rabbit is yellow, it moves down.
When the right arrow is pressed and the rabbit is yellow, it moves right.
When the left arrow is pressed and the rabbit is yellow, it moves left.
When the space bar is pressed and the rabbit is yellow, it shoots.
There are 20 foxes near the top.
The foxes chase the rabbits.
When a fox touches a rabbit, the rabbit explodes.
When the yellow rabbit is dead, game over.
When a fox is shot, it explodes.
Can you design a game involving 1 rabbit, 20 carrots, and 3 foxes. One of the foxes always chase the rabbit, while the other two foxes chase the rabbit only if they see the rabbit?
Complete Sample Games for Subset of Characters
// Game #1. Get the Bones.
// Game #2. Survival.