# Traversing 2D Arrays

• (2 Pages)
• 1
• 2

## 15 Replies - 943 Views - Last Post: 04 May 2013 - 02:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=319650&amp;s=565f9043dd9798fdaa6f6e41b6358095&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 UKCharlie

• New D.I.C Head

Reputation: -1
• Posts: 17
• Joined: 19-April 13

# Traversing 2D Arrays

Posted 26 April 2013 - 05:58 PM

We have an array to basically outline where there are 'tiles'. The tiles are 25X25 and represent walls. We use 1s and 0s in the array. '1' represents that a tile is located there and a '0' means there isn't. The character can't move where there is a '1'.
This is the code for the array:
```int[,] Tiles = new int[31,23]
{
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
```

What we want to do is whenever there is a '1', we want a tile placed. So, we would need to traverse the array and see where there are '1's... right?
I wrote this code to draw the rectangles but it doesn't work:
```foreach (int element in Tiles)
{
if (element == 1)
{
wallX += 25;
wallY += 25;
recwall2 = new Rectangle(wallX, wallY, 25, 25);
spriteBatch.Draw(wall, recwall2, Color.White);
}
}
```

I was thinking we'd have to use Rectangles for each little tile that we place but I'm sure there's an easier way than sitting and writing out 50+ rectangles. I was looking at how to make a list to hold those rectangles but I have no idea how to incorporate that.
Can anyone help up on how we would traverse the array to place the tiles on the screen wherever there is a '1'?

Is This A Good Question/Topic? 0

## Replies To: Traversing 2D Arrays

### #2 Skydiver

• Code herder

Reputation: 6114
• Posts: 21,049
• Joined: 05-May 12

## Re: Traversing 2D Arrays

Posted 26 April 2013 - 07:00 PM

Instead of using a foreach, use two nested for loops that iterate over the x and y range. When it is time to draw something on screen, compute the on screen position to be x times your tile width, and y times your tile height.

This post has been edited by Skydiver: 26 April 2013 - 07:00 PM

Was This Post Helpful? 0

### #3 UKCharlie

• New D.I.C Head

Reputation: -1
• Posts: 17
• Joined: 19-April 13

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 08:24 AM

Can you give me an example on how that would look code wise? I'm not entirely sure how to do that.
Was This Post Helpful? 0

### #4 Michael26

• Futurama: Insert funny joke here

Reputation: 414
• Posts: 1,664
• Joined: 08-April 09

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 09:21 AM

Divide Skydivers instructions into smaller more manageable ones and the do them one by one
Was This Post Helpful? 0

### #5 UKCharlie

• New D.I.C Head

Reputation: -1
• Posts: 17
• Joined: 19-April 13

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 10:11 AM

Ok, I've done some research and I've seen that these are the loops:
``` int bound0 = Tiles.GetUpperBound(0);
int bound1 = Tiles.GetUpperBount(1);

for (int i = 0; i <= bound0; i++)
{
for (int j = 0; j <= bound1; j++)
{

}
}

```

I'm just checking, would those work? I've tried putting them into my code but now I've realized I don't know how to draw based off of that.
Was This Post Helpful? 0

### #6 AdamSpeight2008

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 10:21 AM

Type that code into a IDE and run it, and find out.
Was This Post Helpful? 0

### #7 UKCharlie

• New D.I.C Head

Reputation: -1
• Posts: 17
• Joined: 19-April 13

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 10:23 AM

Running won't do anything. I don't know what to put into the for loops so even if it does work, I won't know.
Was This Post Helpful? 0

### #8 AdamSpeight2008

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 10:25 AM

How do you know if you don't try?
Was This Post Helpful? 0

### #9 UKCharlie

• New D.I.C Head

Reputation: -1
• Posts: 17
• Joined: 19-April 13

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 10:29 AM

Alright, for your sake, I put it in. I pressed the run button. And nothing happened. Now, do you have any ideas on what I should put into the for loops to draw the tiles? I'm trying lots of things right now and nothing is working.
Was This Post Helpful? -1

### #10 AdamSpeight2008

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 10:39 AM

Was This Post Helpful? 0

### #11 UKCharlie

• New D.I.C Head

Reputation: -1
• Posts: 17
• Joined: 19-April 13

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 10:51 AM

... I know perfectly well how to debug AdamSpeight2008. Debugging isn't the problem. I don't know how to draw the Tiles using the for loops.
Was This Post Helpful? 0

### #12 AdamSpeight2008

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 11:18 AM

How would draw a tile without the forloop?
Was This Post Helpful? 0

### #13 UKCharlie

• New D.I.C Head

Reputation: -1
• Posts: 17
• Joined: 19-April 13

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 11:24 AM

I have a for loop. I stated those above
Was This Post Helpful? 0

### #14 andrewsw

• blow up my boots

Reputation: 6541
• Posts: 26,478
• Joined: 12-December 12

## Re: Traversing 2D Arrays

Posted 28 April 2013 - 11:45 AM

You need to investigate the Graphics library and the DrawRectangle method. In your loops you would modify the x and y coordinates (perhaps the width and height) when drawing the rectangles.

I found the link by searching "C# draw rectangle".

This post has been edited by andrewsw: 28 April 2013 - 11:45 AM

Was This Post Helpful? 0

### #15 UKCharlie

• New D.I.C Head

Reputation: -1
• Posts: 17
• Joined: 19-April 13

## Re: Traversing 2D Arrays

Posted 04 May 2013 - 02:11 PM

Ok, sorry I've been away for a while. I got sick. But anyway, I've been doing some work on my code and I created a Rectangle array.
``` Rectangle[,] tileRec = new Rectangle[31,23];
```

So, I haven't gotten it to draw rectangles based off of the Tiles array data yet but I just tried to get it to draw. Now, it does draw but then it disappears. I have no idea why it is disappearing. This is the code:
``` //Traversing Array and getting values
int bound0 = Tiles.GetUpperBound(0);
int bound1 = Tiles.GetUpperBound(1);
int number;
int ArrayX =0;
int ArrayY =0;

for (int i = 0; i < tileRec.GetLength(0); i++)
{
for (int j = 0; j < tileRec.GetLength(1); j++)
{
for (int k = 0; k < Tiles.GetLength(0); k++)
{
for (int l = 0; l < Tiles.GetLength(1); l++)
{
tileRec[i, j] = new Rectangle(wallX, wallY, 25, 25);
wallX += 25;
spriteBatch.Draw(wall, tileRec[i, j], Color.White);
}
}
}
wallY += 25;
wallX = 0;
}

```

Why are all the Tiles disappearing?
Was This Post Helpful? 0

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }