forums

TNT Basic Forums > Programming Q&A
While Loop Won't Stop
< Last Thread     Next Thread >
Author
Thread        Post A Reply

10-18-2003 17:13

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

I'm not sure if I should be posting this in the bugs sections, but I have a While loop that won't stop whiling.

I am using the following code, and the array s[] has 800 indexes in it. (s[800])

I have it set up so that the number of values it goes through equals turn minus one, multiplied by two hundred plus fifty. ( ((turn - 1)*200) + 50 ).

When turn = 3, the procedure should stop searching through array s, when num equals 450. But it dosnt. It just keeps going on until it gets past 800 and I get an error message:

*** Runtime exception caught ***
Line# : 1126
Code : if s[((turn - 1)*200) + num] = true
Error : Array index out of bounds.


Has anyone seen this before, or see where I went wrong?

Thanks.


procedure colonise()
int num = 1
int pnum = 1

while num < ((turn - 1)*200) + 50
while pnum < pplanets
if s[((turn - 1)*200) + num] = true
if (((sx[((turn - 1)*200) + num] - soffsety[((turn - 1)*4 + 1)]) = pplanrandx[pnum])) and ((sy[((turn - 1)*200) + num] - soffsety[((turn - 1)*4 + 1)] = pplanrandy[pnum])) and powner[pnum + (turn*1000)] = 0
s[((turn - 1)*200) + num]= false

set sprite color pnum + (turn*1000),color
powner[pnum + (turn*1000)] = turn

end if
end if
pnum = pnum + 1
if pnum = pplanets and num < ((turn - 1)*200) + 50
pnum = 0
num = num + 1
end if
wend
wend
end proc

10-19-2003 00:42

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Sorry I can't see the purpose of the code could you explain what's it intended for and why the num*200+50 thing operation?

I think num increases of 1 at each loop. you might then prefer to use a for loop.

10-19-2003 02:47

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Ya, the num increases by one for every time the pnum does a whole loop.

First, there are 4 races. Each race has its own starting system with a home planet, and 5 other randomly selected planets (pplanets). Only the race who lives in that system can take the planets, hence the code powner[pnum + (turn*1000)] = turn. (The planets ID's are 1001 to 1005 for the first player's system, 2001 to 2005 for the second player's system, and so on up to 4005).

The purpose of the pocedure is to check if each of the x and y locations ( sx[((turn - 1)*200) + num] and sy[((turn - 1)*200) + num] ), minus the offset of the ship ( soffsetx[((turn - 1)*4 + 1)] and soffsety[((turn - 1)*4 + 1)] ) of all the coloniser ships that are currently being shown ( s[((turn - 1)*200) + num] = true ) equal any of the x and y locations of the planets ( pplanrandx[pnum] and pplanrandy[pnum] ). If any of the coloniser's locations do equal those of a planet, the planet sprite is turned a color ( set sprite color pnum + (turn*1000),color ), and its ownership is changed to whoevers turn it is ( powner[pnum + (turn*1000)] = turn ).

The ((turn - 1)*200) + 50 is the ID's of the colonisers, which are stored in the array s[800], but the colonisers are only part of this array; 1- 50 are player 1's colonisers, 201- 250 are player 2's colonisers, 401- 450 are player 3's colonisers, and 601- 650 are player 4's colonisers.

Thus when I use the code ((turn - 1)*200) + 50, (say it is player 3's turn), it will give the script ((3-1)*200 + 50), which is 450, the maximum number of colonisers for player 3, as stated above.

The code ((turn - 1)*200) + num thus checks the ID of all the sprites from 1 to 50, plus ((turn - 1)*200).

Hope this helps and isnt too confusing!

Thanks,
MapMaker

10-19-2003 02:52

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

O ya, and the s[((turn - 1)*200) + num]= false turns off the sprite that colonised the planet (in a different procedure)

If this is all really confusing I can e-mail you the whole game (its really small).

Thanks again,
MapMaker

10-19-2003 13:51

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

i guess you could do that!

10-19-2003 15:07

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Sorry, a bug just appeared out of nowhere and I'm attempting to fix it. Can Sprite col work between different layers?

10-19-2003 15:07

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Sorry, not layers, cavases*

10-19-2003 22:44

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I guess not cause they can,t appear on top of each other they're always one next to the other so collision is useless

10-21-2003 23:02

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

I've not tried fixing it, but I'm pretty sure the problem is you considered s[800] included indexes 1 to 800 but it includes 0 to 799, wich means index 800 would cause an error.

You need to stop the loop before it reaches 800, not before it goes beyond.

10-22-2003 00:28

Posted by:
MapMaker

Location:
Edmonton, Alberta (Canada)

Click Here to Email MapMaker   Find more posts by MapMaker

Ah....

That wasnt it, but it brought the key fact to my attention.

I was using the code while num < ((turn - 1)*200) + 50 and then later using s[((turn - 1)*200) + 50].

I shouldnt have been using them both, just the code while num < 50 was all I needed.

Thanks for taking the time to help me anyways!

MapMaker

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.