Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-01-2004, 01:29 AM   PM User | #1
Serex
Regular Coder

 
Join Date: Mar 2004
Location: Australia
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Serex is an unknown quantity at this point
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
Serex is offline   Reply With Quote
Old 11-01-2004, 03:30 AM   PM User | #2
darkmonkey
New Coder

 
Join Date: Nov 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
darkmonkey is an unknown quantity at this point
Just at a stab, try this:

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

            	break;
      }
}
__________________
Darkmonkey Designs
darkmonkey is offline   Reply With Quote
Old 11-01-2004, 03:34 AM   PM User | #3
Serex
Regular Coder

 
Join Date: Mar 2004
Location: Australia
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Serex is an unknown quantity at this point
already thought of that lol. ta anyways. i think it could be my lack of knowing %
Serex is offline   Reply With Quote
Old 11-01-2004, 05:59 AM   PM User | #4
aman
Regular Coder

 
Join Date: Oct 2004
Posts: 230
Thanks: 0
Thanked 0 Times in 0 Posts
aman is an unknown quantity at this point
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;
	}
aman is offline   Reply With Quote
Old 11-01-2004, 03:52 PM   PM User | #5
Serex
Regular Coder

 
Join Date: Mar 2004
Location: Australia
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Serex is an unknown quantity at this point
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?
Serex is offline   Reply With Quote
Old 11-01-2004, 06:59 PM   PM User | #6
Dr. Evil
New Coder

 
Join Date: Nov 2004
Location: Netherlands
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Dr. Evil is an unknown quantity at this point
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...

Last edited by Dr. Evil; 11-01-2004 at 07:59 PM..
Dr. Evil is offline   Reply With Quote
Old 11-01-2004, 07:37 PM   PM User | #7
aman
Regular Coder

 
Join Date: Oct 2004
Posts: 230
Thanks: 0
Thanked 0 Times in 0 Posts
aman is an unknown quantity at this point
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;
	}
aman is offline   Reply With Quote
Old 11-02-2004, 03:32 AM   PM User | #8
Serex
Regular Coder

 
Join Date: Mar 2004
Location: Australia
Posts: 217
Thanks: 0
Thanked 1 Time in 1 Post
Serex is an unknown quantity at this point
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
Serex is offline   Reply With Quote
Old 11-02-2004, 10:44 AM   PM User | #9
aman
Regular Coder

 
Join Date: Oct 2004
Posts: 230
Thanks: 0
Thanked 0 Times in 0 Posts
aman is an unknown quantity at this point
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;
			}

	}
aman is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:43 AM.


Advertisement
Log in to turn off these ads.