Public Group

# Arrays

This topic is 5449 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have been confused about arrays for a long time. I thought I would actually try to make a game today by entirly by myself, but it looks like that wont happen, since I am still consfued about arrays. I want to make tic-tac-toe, and I know I should use the array to make the board, but I dont really understand how multidemensional arrays work, so I have no idea how I would be able to mark the boards, check if there is a match(I have NO idea how to do that) etc. Can somebody help me with this?

##### Share on other sites

//////////1D array:

//declare an int array with 10 elements:
int list[10];

//the first element can be accessed like this:
int element;
element = list[0];

//the second element:
element = list[1];

//and set like this:
list[1] = 5; //now index 1 in list holds the number 5

//note the first element is accessed with the index 0 and the 10th element has the index 9

//you can also initialize the array with numbers at once when declared
int list[] = {0, 3, 2, 1, 6, 7, 8, 4, 0, 3};

//////////2D array (works much the same way)

//declare an int array with 10 * 10 elements
int map[10][10];

//imagine the 2-dimensional array as a table with a number of rows and a number of columns on each row (in this case 10 rows and 10 columns, a total of 10 * 10 = 100 entries)

//to access the second column on the 1 row write like this:
element = map[1][2];

//set the 3rd column on the 4th row to hold the number 1:
map[4][3] = 1;

//2D arrays can also be initialized
int map[3][3] =
{{1, 4, 0},
{0, 3, 9},
{5, 9, 8}};

edited:
I put the access syntax wrong (changed it now), thanks for the correction Oluseyi.

\Jimmy H

[Edited by - Jimmy H on October 19, 2004 5:31:16 PM]

##### Share on other sites
Well, what is it about arrays that confuses you? They're really rather simple.

The basic concept with arrays is that they let you maintain a list of different variables all referenced by the same name. This is to avoid such nonsense as int board_tl, board_tm, board_tr,....

Since this is tic-tac-toe, you already know the array sizes you need: 3x3, so creating an array of that size is as simple as int board [3][3];. When creating multidimensional arrays, the two numbers represent the dimensions in either direction. That statement means to make aa array of 9 integers arranged into a 3x3 table.

Marking the board is entirely dependent on how you intend for the player to tell you where to put a mark. Unless you're using a GUI (which I highly doubt), odds are they're just typing in the coordinates at which to place the mark, right? Then it's just a simple matter of having a line of code like board [markX][markY] = /*whatever you use to represent their choice*/.

But there you have to be careful. You see, arrays in C-based languages (and some others) are zero-based, which means they count from zero. This goes against the traditional way of counting, but is done because of how the memory itself is structured. So an array of 3 elements (int array [3]; would give you elements 0, 1 and 2 to work with. Regardless of why, the important part is that you have to account for it. If you have your player entering 3,3 for the bottom right of the board, you would then have to subtract one when indexing the array, so in that case, it would be more like board [markX - 1][markY - 1] = /*whatever*/.

Checking for matches is much trickier. This requires you to loop through the array. While it takes a bit to get the hang of, it's a large part of why arrays are useful: they reduce the amount of code you need by allowing you to use loops to access every variable in the array. Now for traditional tic-tac-toe, there are exactly eight ways for matches to occur: each horizontal, each vertical and the two diagonals. Here's some example code to do just that:

// Assume that 0 is a match, 1 is an O and 2 is an X

// Check the board's horizontals
for (int horiz = 0; horiz < 3; ++horiz)
{
// If the three in a row match each other
if (board [horiz][0] == board [horiz][1] && board [horiz][1] == board [horiz][2])
// It's only a match if there's actually stuff in the spaces
if (board [horiz][0] != 0)
// You have a match! Do whatever is most appropriate here
}

// Check the board's verticals
for (int vert = 0; vert < 3; ++vert)
{
// If the three in a column match each other
if (board [0][vert] == board [1][vert] && board [1][vert] == board [2][vert])
// It's only a match if there's actually stuff in the spaces
if (board [0][vert] != 0)
// You have a match! Do whatever is most appropriate here
}

// Lastly we check the diagonals:

// If the center of the board is empty, no diagonals can occur
if (board [1][1] != 0)
{
// Check the top-left to bottom-right diagonal
if (board [0][0] == board [1][1] && board [1][1] == board [2][2])
// You have a match! Do whatever is most appropriate here
// Check the top-right to bottom-left diagonal
else if (board [2][0] == board [1][1] && board [1][1] == board [0][2])
// You have a match! Do whatever is most appropriate here
}

There are more optimal and tidier ways to do this, but this should be pretty clear.

So, look that over. If you absolutely must copy and paste, do so, but make sure you understand it before you move on from there. Arrays are fundamental and vital to almost anything you could want to develop so it's really in your best interest to understand them.

If you still don't get it, find a good book or tutorial and don' hesitate to ask for more help.

-Auron

##### Share on other sites
You can look at a multidimensional one of two ways. First, don't try to think about arrays with more than 3 dimensions. They don't make much sense - and are rarely used.

If you have a Tic Tac Toe board you can declare it like this:

char board[9];

which creates 9 spaces. In this case you're flattening the Tic Tac Toe board into one dimension.

0 | 1 | 2
3 | 4 | 5
6 | 7 | 8

Each space is represented by a single member of the array. This could also be represented as:

char board0;
char board1;
char board2;
char board3;
...
char board8;

The difference is that with an array you can use the same name to access all the elements - with the second type of declaration you're forced to handle a great deal of additional logic manually.

Now consider a two dimensional array:

Row 0: 0 | 1 | 2
Row 1: 0 | 1 | 2
Row 2: 0 | 1 | 2

Again we have 9 elements. The only difference is how they're being accessed. What was board[0] and board0 can now be accessed as board[0][0]. board[8] and board8 can be accessed using board[2][2]. We want to access the 3rd row (the first two in brackets, keeping in mind that computer begin counting at 0) and the third element of that row (the second two in brackets).

As an alternative explanation: Consider an index in a book. You go to the index knowing you want a certain word. The index of the book could be considered the first dimension of an array. From the index you find out that the word you're looking for has so many entries to it. Those entries can be considered the second dimension of the index. Using one of the second dimension entries to look up a value would be similar to using the first and second indexes into an array to find the value of the object.

Does that help, or make it worse? :)

##### Share on other sites
Thabks everyone! I quickly glanced over your posts, and they apear to be what I was looking for.

##### Share on other sites
@Jimmy H:
You index each dimension of a multi-dimensional array separately. ie, for a 2D array, it's array[d1][d2], not array[d1, d2].

##### Share on other sites
if you need any help on your game aim me, my sn is
the c programmer

##### Share on other sites
Here's one way to visualize four+-D arrays in 2-d, just think of an array of arrays. Think of a 4-D array as a 2-D array of 3-D arrays.
That sounds kind of weird, but it's the only way I can visualize more than 3 diminsions.

##### Share on other sites
Quote:
 Original post by nobodynewsArrays with dimensions greater than 3 make perfect sense, you just have to think of them differently than the way we think about 1, 2, and 3-dimensions in the real world. There are more ways to use arrays than as a way of storing coordinates in the real world is what I mean.Think about a way to group people into an array by their location. You might first consider a household and the number of people in that house. Then you might think about the number of houses in a city. How many cities are there? In each country how many cities are in each? What's the number of countries on the planet? How many planets are there in the solarsystem? How many solar systems are there? In a galaxy what is the number of solor systems? And how many galaxies are there? If you knew this information you could create on the stack this obscene multi-dimensional array:int PeopleArray[numUniv][numGalax][numSolSys][numPlanets][numCountry][numCity][numHouse];If you knew how many people are in this universe that live in the Milky Way galaxy that orbit the sun Sol on the planet Earth in the United States in SomeTown, OH in 123 Fake Address Ln. you might set the list like this:PeopleArray[0][MILKY_WAY][SOL][EARTH][USA][SOME_TOWN][FAKE_ADDRESS_123] = 4; // 4 people live in this placeYou could have a constant to refer to a common galaxies, solor systems, planets, countries, and cities.So you see, I have shown an exampe(albeit an absurd one) in which a multidimensional array makes sense. A more realistic array would stop at country and everything from planet and above would be assumed to be on this Earth. An even better way would be a multidimensional vector, but that'll be later.I hope this helped.

I will admit that arrays of more than 3 dimensions can be useful -- especially when using jagged arrays or the like where you're creating a lookup table of sorts. I've found myself using an array of 4 dimensions just once in eight years and that was for a data structure remarkably similar to the one you used -- that is to say, addresses. :)

Generally once I'm at 4 dimensions I want to refactor what I'm trying to store because I feel like I'm stuffing too much information into one variable. Either by creating an object to store the various bits of information and using an array of those objects, or some other method of simplification.

I stand by my original comment though: visualing arrays greater than 3 dimensions can cause a beginner a great deal of grief and should be avoided. :)

##### Share on other sites
thanks everyone. oh, and dan1088352 if I ever need help, ill IM you.(is it "the c programmer" or just "c programmer"?)

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 46