Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## C++ Loop

Hey how you all doing, havent posted here in a while, been busy at Uni doing batchelor of interactive entertainment.

anyways im currently in the process of creating a minesweeper game. im having problems rendering all the images how i need (efficiently). currently i have. however this is discusting to look at. i am looking to simplify.

Code:
```if (i == 20)
{
xSpacer = 10;
ySpacer -= 20;
}
else if (i == 40)
{
xSpacer = 10;
ySpacer -= 20;
}
else if (i == 40)
{
xSpacer = 10;
ySpacer -= 20;
}
else if (i == 60)
{
xSpacer = 10;
ySpacer -= 20;
}

..... ect

else if (i == 400)
{
xSpacer = 10;
ySpacer -= 20;
}```
all the way up to i = 400. i was wondering if there was an easier and tider way of doing this.

Code:
```for (int i = 20; i <= 400; i += 20)
{
if (i % 20)
{
xSpacer = 10;
ySpacer -= 20;
}
}```
i tried a for loop as above using modulous but all that did was render them in a horizontal line.

if you have any suggestions please let me know. thanks

srx

• Just at a stab, try this:

Code:
```for (int i = 20; i <= 400; i += 20)
{
if (i % 20)
{
xSpacer = 10;
ySpacer -= 20;

break;
}
}```

• already thought of that lol. ta anyways. i think it could be my lack of knowing %

• If you want to test modulus (%) like that, then you need to compare it to zero, because otherwise it will always be false in this loop..
Code:
```for (int i = 20; i <= 400; i += 20)
{
if (i % 20 == 0)
{
xSpacer = 10;
ySpacer -= 20;

break;
}
}```
But since you are using "if / else if" testing, you don't even need a loop. You can acheive the same result with one line:
Code:
```	if( i <= 400 && !(i % 20) ) {
xSpacer = 10;
ySpacer -= 20;
}```

• thanks heaps for explaining aman. now only problem is i have one block rendering at the top when in should be at the bottom

[ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ]

just like that
any ideas?

• Do you mean that one button you created shows up in the wrong spot? Posting a little of the button creation code might help some.

EDIT: D'oh! That is the button creation code. Oops...

• Just a guess, but you're original if/else if code doesn't test against 0. If thats the case then we should change the code slightly..
Code:
```	if( i > 0 && i <= 400 && !(i % 20) ) {
xSpacer = 10;
ySpacer -= 20;
}```

• thanks once again. fixed the bottom box not being rendered. nw i get

Code:
```[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ]```
1 too many blocks at the top. i been looking through my render code to see if its anything in there but cant seem to find it.

Code:
```// add block sprites
for (int i = 0; i <= MAX_BLOCKS; ++i)
{
m_block[i].bVisible = true;
m_block[i].pSprite = ClientObjectManager::Instance().AddSprite("data\\defaultBlock.tga", 20, 20, 1, Vector3((-200.0f + xSpacer), (200.0f + ySpacer), 0));
xSpacer += 20;

if ( i > 0 && i <= 400 && !(i % 20) )
{
xSpacer = 10;
ySpacer -= 20;
}

}```
this code adds all the needed blocks to the client object manager, this deals with all the rendering/transparency and verification so thats all good.

Code:
```// render all images if they are visible
for (int rCntr = 0; rCntr <= MAX_BLOCKS; rCntr++)
{
if (m_block[rCntr].bVisible) m_block[rCntr].pSprite->Render();
}```
my render function that loops through all blocks and checks their visibility. if they are true then it will render them. if not it leaves them out.

const int MAX_BLOCK = 20;
const int MAX_ROW = 20;
const int MAX_BLOCKS = MAX_BLOCK * MAX_ROW;

thats how max blocks are stored.
aman i need you again

thanks heaps

• Remember that Array[n] starts at Array[0] and progress up to Array[n-1].
You are accessing Array[n] from Array[0] up to Array[n]... big problem, easy solution. This is why you have one block too many.

Assuming that you are initializing m_block like this: m_block[MAX_BLOCKS], then in your for loop you are actually overwriting past the bounds of the array when (i == MAX_BLOCKS). To fix that, change both of your for loops to increment up to (i < MAX_BLOCKS).

Now your loop will be incrementing from 0 to 399... thats a total of MAX_BLOCKS, instead of MAX_BLOCKS + 1, and you'll stay within the array bounds.

Problem two, which is the reason you have the extra block in the top row, is because you are including 0 in the loop. On your first check at (i % 20), you are actually at loop index of 21 because of starting at 0.

To solve that, simply change the code to check for %20 at i+1.
Code:
```	// add block sprites
for (int i = 0; i < MAX_BLOCKS; ++i)
{
/* code removed */

if ( !((i+1) % 20) )
{
xSpacer = 10;
ySpacer -= 20;
}

}```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•