Picture
Welcome to Day 9! Today's lesson will be short, but full of information. We are building towards loading pre-made Level designs and rendering tiles to the screen. To do so, we must first go over some basic fundamentals. Therefore, we will be creating a new project to practice and study these new topics!
Lesson #2-26: Arrays
One of the most powerful tools in a programmer's arsenal is the array. You can think of an array as a list of items. Let's say you had three favorite things:

1. Playing
2. Eating
3. Sleeping

These three things could easily be categorized into one list so that you can refer people to this list rather than three separate variables. That's what an array does, in principle.

1. To see this in action, create a new Project, and name it whatever you want.
2. Within the src folder, create a new Java class called: Array.
3. Add the main method:

  public static void main(String args[]) {

}

4. Within the main method, declare the following:

String[] favoriteThings = new String [3];


This creates an Array (list) of String objects called stringArray, and allocates memory to accommodate three (3) items.

Alternatively, this same line can be written as below, by shifting the brackets:
String favoriteThings[] = new String [3];

Astute observers will recognize this syntax. If you examine the parameter of the main method:
String args[], you will now see that the main method takes in an array of Strings as a parameter. These arrays are passed in at a lower level, and you will not encounter them in Eclipse.

Your code should be looking like this:

Figure 2-34: Array Class

 public class Array {

         public static void main(String args[]) {
String[] favoriteThings = new String [3];
}

}
Now let's talk about how to fill this Array.

We can refer to positions in the array as indices. The first item in the list will have an index of 0, the second will have 1, and so on.

To refer to a specific index of an Array, we can write:
array[index].

Going back to our code, we can list our three favorite things as follows:

...
public static void main(String args[]) {
String[] favoriteThings = new String [3];

favoriteThings[0] = "Playing";
favoriteThings[1] = "Eating";
favoriteThings[2] = "Sleeping";

}
...

What if we now want to print all these items?
It's tempting to use System.out.println(favoriteThings), but this will return what seems to be nonsense.

Instead, we must use a for loop, and iterate over an index to print each line.

This is how I would approach it:

for (int i = 0; i < favoriteThings.length; i++) {
System.out.print(i+1 + ". ");
System.out.println(favoriteThings[i]);
}


This code introduces nothing new. It is just application of previously-covered topics. Make sure you understand this before moving on!

If we place this for loop at the bottom of the main method, we will see the following output:
Picture
Understand how it works? Then you are ready for the next level.
Lesson #2-27: Two-Dimensional Arrays
The most practical application of a two-dimensional Array is when you need to store a list of values with (x, y) positions. 

For our game's purposes, we will use a 2D array to hold a value at each position, and then paint the object/character/platform associated with said value at its (x, y) coordinate.


2D Arrays are very similar to single dimensional arrays, but we add a second dimension (like adding a width).

Two create a 2D array, we simply state:
int[][] intArray = new int[30][50]; //Creates a 2D array of integers with height 30 and width 50.

1. Create a new class called Tilemap
2. Add the following code:

Figure 2-35: Tilemap Class

import java.util.Random;

public class Tilemap {


public static void main(String args[]) {
Tilemap tp = new Tilemap();

}

public Tilemap() {
int[][] tilemap = new int[30][50];

System.out.println("New Tilemap created.");
Random r = new Random();

int rows = tilemap.length;
int columns = tilemap[1].length;

printTiles(rows, columns, tilemap, r);

}

private void printTiles(int rows, int columns, int[][] tilemap, Random r) {

for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
tilemap[i][j] = r.nextInt(5);
System.out.print(" " + tilemap[i][j]);
}

System.out.println("");

}
}

}
Before you run this code, let's talk about what each segment of code does.

1. We import the Random class (to be used later)
2. In the main method, we create a new Tilemap object called tp. This calls the constructor below the main method.

3. In the constructor, we create a new two dimensional array called tilemap, with height 30, and width 50.
- We also create a Random object called r

Two-dimensional arrays can be thought of as Arrays of Arrays. Meaning that it is a list of lists. The larger list has 30 empty spots. Each of those 30 spots has 50 spots across.

Therefore, when we refer to tilemap.length, we get the length of the larger list, which is 30.
When we refer to tilemap[1].length, we get the length of the small list with index 1 in the large list, which is the second small list in the large list. This will have a value of 50.

4. We then call the printTiles() method, which takes in various parameters, uses two for loops (one for the columns and another for the rows).

This printTiles() method gives each space a value between 0 to 4 (at random). Then we print each row and column.

The result should look something like this:
Picture
In the next lesson, we will associate each integer from 0 to 4 with a colored rectangle and fill the screen. This will then segue into a series of lessons on Level design/rendering and collision physics!

Stay Tuned!
 


Comments

ale
11/11/2012 8:49pm

Excellent!! I was needing this :D

Reply
Gligor
11/12/2012 5:33pm

Hi James,

Your link on page 8 to page 9 doesn't work correctly.

Great tutorial though ;-)

Cheers,
G

Reply
Vaaal
11/13/2012 3:39pm

I don't understand this lessons. Should I have to create the class Tilemap into the kilobolt project?

Reply
Peter
11/13/2012 5:01pm

it says

"1. To see this in action, create a new Project, and name it whatever you want."

Reply
Vaaal
11/14/2012 8:30am

Sorry, I missed it :)

Reply



Leave a Reply

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge.