Lab 5.3: Practicing Arrays

An Introduction to Using Arrays

Goals

The goals of this lab are to learn the following:

  • Understand the basic concepts of an array container (data structure)
  • How to create and modify arrays
  • How to apply generic concepts to an array
  • More practice with class and object concepts

Computer Science Principles Curriculum

  • Big Idea: Abstraction: EU 2.1, LO 2.1.1, EK 2.1.1A, EK 2.1.1C
  • Big Idea: Abstraction: EU 2.2, LO 2.2.1, EK 2.2.1A, EK 2.2.1B, EK 2.2.1C
  • Big Idea: Abstraction: EU 2.2, LO 2.2.2, EK 2.2.2A, EK 2.2.2B
  • Big Idea: Data and Information: EU 3.1, LO 3.1.1, EK 3.1.1C
  • Big Idea: Data and Information: EU 3.1, LO 3.1.3, EK 3.1.3A, EK 3.1.3B, EK 3.1.3E
  • Big Idea: Data and Information: EU 3.2, LO 3.2.1, EK 3.2.1G, EK 3.2.1H, EK 3.2.1I

Common Core Standards

  • English Language Arts Standards » Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3, CCSS.ELA-Literacy.RST.9-10.1, CCSS.ELA-Literacy.RST.9-10.8
  • Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8, CCSS.Math.Content.HSF.IF.A.1

Vocabulary

  • Array
  • Container
  • Data Structure
  • Generic
  • Indexed
  • Metadata
  • Inventory
  • Spawning
  • Vector

Overview

First, we will have a quick overview of Array objects and how to use them, and then you will be shown how to use Arrays in an example: an inventory in a video game. You will need the following template for this task:

Lab 5.3 Template

Goal 1 : The main characteristics of arrays

Before you start working with your game, let’s review the definition of an array. An array is an indexed data structure or container. The rules of using an array are the following:

  • It can hold any number of items.
  • It must use the same type (primitive or object) for items in the array.
  • It always starts with 0 for an index. Items are stored in sequence and associated with an index or location in the array.

Goal 2 : Spawning Pickables and putting them in an inventory

Start by opening the project you downloaded at the beginning of this lesson. Now that remember some of the characteristics of arrays, you will use them to build an inventory in a 3D game. The goal is to make a simple game: you walk on a flat map, pick up cubes and cylinders that spawn randomly, and store them into your inventory. When you have enough cubes and cylinders, you win. If too much time passes, you lose. Look over the template and notice how the world was created...we have classes for the forest, sky, terrain, trees, etc. We're also using sound files for the footsteps and other sound affects.

Activity: Create a PickableSpawner

On our map, we will have several pickable objects. Each of these objects are of the class Pickable. This class has a action which creates either a cube or a cylinder of a random color. We want to put several of these Pickable objects on our map, and to keep a trace of them. For this purpose we will create class PickableSpawner. The purpose of this class is to create a certain amount of pickables within a certain area, and to store them in an Array. In the code provided class PicakbleSpawner has several actions. First, action Create(number width, number depth, integer nb, Game game) will set the attributes of our object. 'Width' and 'Depth' are used to know the size of the area where the pickables will spawn, 'nb' sets the number of pickables that will spawn at a time, and 'game' is the current Game. The pickables will, by default, spawn around the origin, but you could add another parameter that would set the center around which pickables will spawn.

In order to create a pickable object, you need just one parameter: a Vector3 that represents the position at which the pickable will be created. This is how a pickable would be created with a Vector3 called position:

Pickable pickable
pickable:CreatePickable(position)

You will need to complete two actions. First, action Spawn(). This action's goal is to create all the pickables, and to put them in an Array. The Array is an attribute of the class: Array pickables. In order to set the position Vector3, you will have to use two already written actions: randomX() and randomZ(). Both of these actions will generate a random number: one according to the width of the ground, and the other according to the length. The Y value of the position vector should always be 50. Each time a pickable is instantiated, it should be added to the array immediately. Then call its CreatePickable action. You will need to use a loop to do all of this. A related example below shows how to Add all the integers from one to ten into an Array:

Array example
integer i = 1
repeat 10 times
    example:Add(i)
    i = i+1
end

Based on this example, write the Spawn() action, so that max new pickables will be instantiated, added to the pickables array, and then created using CreatePickable().

The second action is action AddAll(). It goes through the Array of pickables, and Adds them to the game. For this action too, you will need a loop. This related example shows how to output every number from the array created in the previous example.

integer c = 0
repeat 10 times
    output example:Get(c)
    c = c+1
end

Now, based on this example, finish the AddAll() action.

Activity : Create the Inventory

You can now spawn pickables in your game. Next we want the player to be able to pick them up and to put them in the inventory. For this purpose, we create the class Inventory. This class has 3 attributes : An array containing every pickable the player has picked up, and two numbers: metadata representing how many cubes and how many cylinders the player has. The most important action of this class is action PickUp(Camera cam, Array pickables). This action is called every time the space key is pressed. The parameter 'cam' is used to see if the player is next to a pickable. The parameter 'pickables' is actually the same Array that was created in the class PickableSpawner. The game passes this array to the action, so the inventory knows all the pickables not yet picked up, and their position. First you will have to finish this action. Code what happens when a pickable is next to the player. The pickable should be:

  • Added to the Array in the Inventory
  • Removed from the game.
  • Removed from the Array in PickableSpawner.

Here is how to remove something from an Array at a certain position: example:RemoveAt(0). To make the Pickable disappear from the game, use action Destructor() from the Pickable class. Another important action is action Number(). It provides metadata (data about our data): how many cubes and cylinders are in the inventory. Write this action using loops. Every pickable has a description when it is created in the class Pickable. Use the action GetDescription() to know whether a pickable is a cube or a cylinder. A related example can be found below. This code will count how many even and odd numbers are held in an Array of integers called 'example'

integer i = 0
integer NEven = 0
integer NUneven = 0
repeat example:GetSize() times
    if example:Get(i) mod 2 = 0
        NEven = NEven + 1
    else NUneven = NUneven + 1
    end
    i = i +1

Now, based on this example, in the action PickUp(), implement the loop to calculate the metadata (count how many cylinders and cubes are held in the pickables array). Now you can play your game. Remember you will need to press the space key to pick up the Pickables. Good luck!

Next Tutorial

In the next tutorial, we will discuss Assignment 5.1, which describes an introduction to validating input and using multiple files..