...

View Full Version : C++ Loop

Serex
11-01-2004, 01:29 AM
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.

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.

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

darkmonkey
11-01-2004, 03:30 AM
Just at a stab, try this:

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

break;
}
}

Serex
11-01-2004, 03:34 AM
already thought of that lol. ta anyways. i think it could be my lack of knowing %

aman
11-01-2004, 05:59 AM
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..

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:

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

Serex
11-01-2004, 03:52 PM
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 :p
any ideas?

Dr. Evil
11-01-2004, 06:59 PM
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...

aman
11-01-2004, 07:37 PM
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..

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

Serex
11-02-2004, 03:32 AM
thanks once again. fixed the bottom box not being rendered. nw i get

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

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.

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.

// 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 :p

thanks heaps

aman
11-02-2004, 10:44 AM
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.

for (int i = 0; i < MAX_BLOCKS; ++i)
{
/* code removed */

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

}