Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Nested IF..THEN problems

Author
Message
Aiptasia
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location:
Posted: 9th Feb 2003 20:24
Hey there. Having another little syntax problem again regarding nested IF..THEN loops. I figured you guys would be able to tell me what's up. What i'm attempting to code is a simple routine that would compare randomly generated X,Y,Z map coordinates against a set of 20 pre-fixed coordinates. For example, let's say this is a game where the planet earth is assigned a fixed coordinate of X=1, Y=1 and Z=1. Now let's say, I wanted to compare the X,Y,Z coordinates of a new, randomly generated planet on my map against the fixed coordinate of planet earth, to make sure the randomly generated planet doesn't occupy the same coordinates as the fixed planet. Normally, i'm used to doing this in a nested IF...Then...Else scenario. Here's what i'm doing:

Variable J is equal to the total number of random planets to compare to fixed planet locations. Variable I is equal to the number of fixed planets to compare in the loop. Gsize# is the number of random planets in the galaxy. Coord# is the X,Y,Z DIM array for the fixed planet locations. Rcoord# is the X,Y,Z, DIM array for the random planets.

Compare:
For J=1 to gsize#
for I =1 to 20
if rcoord#(gsize#,1)=coord#(I,1) then
if rcoord#(gsize#,2)=coord#(I,2) then
if rcoord#(gsize#,3)=coord#(I,3) then
rcoord#(gsize#,1)=RND(1000)
rcoord#(gsize#,2)=RND(1000)
rcoord#(gsize#,3)=RND(1000)
endif
else
Next I
Next J

The reason for using the nested IF..Then statements is so that IF condition #1 = true then if condition #2=true then if condition #3=true, regenerate the random number X,Y,Z coordinates, else do nothing and proceed to comparing the coordinates of the next planet.

Apparently, Darkbasic doesn't handle nested IF...Then statements that well. Can anyone think of a solution that would accomplish the same thing in Darkbasic?
Yes.... no... wait!
Aiptasia
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location:
Posted: 9th Feb 2003 21:17
Well, I fixed my own problem. For anyone interested, here's how I did it. I also spotted a problem with my code in regard to the J variable (constantly comparing gsize# instead of J in the for.. next loop). Oops!

Anyway, here's how I managed to solve my own problem:

for j=1 to gsize#
for I =1 to 20
if rcoord#(j,1)=coord#(I,1) then goto coord2 else goto nloop
coord2:
if rcoord#(j,2)=coord#(I,2) then goto coord3 else goto nloop
coord3:
if rcoord#(j,3)=coord#(I,3) then rcoord#(j,1)=RND(1000) else goto nloop
rcoord#(j,2)=RND(1000)
rcoord#(j,3)=RND(1000)
nloop:
Next I
next j

Yes.... no... wait!
D Man
21
Years of Service
User Offline
Joined: 3rd Oct 2002
Location: Germany
Posted: 9th Feb 2003 21:27
Why don't you use "and"?



"If you can't make it good
make it look good."
Bill Gates
Flashing Blade
21
Years of Service
User Offline
Joined: 19th Oct 2002
Location: United Kingdom
Posted: 9th Feb 2003 21:46
'lo aiptasia
1st thing is you can't use real numbers for loop control (for J= 1 to gsize#)
2nd i think you wanna compare i,j not i,gsize.
Try this:


3rd may i suggest you use types? try this:



4th you should maybe take into acount width of planets when calculating if they clash.
Aiptasia
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location:
Posted: 9th Feb 2003 22:13
Flashingblade, yeah, caught that error. I'm actually not interested in collision detection. The idea is to have a 1000x1000x1000 sector map and assign X,Y,Z coordinates of various planets to the map. The only thing I needed the code to do was to check the static 20 planet coordinates vs. the randomly generated x,y,z coordinates in the galaxy and make sure they didn't occupy the same sector.

For example, earth would be assigned a fixed coordinate of x=1, y=1, z=1. What I needed the code to do was to detect whether a randomly generated planet on the map, say X=230, y=101, z=113, would occupy the same coordinate of earth, or any other fixed planets I had assigned in the game.

That's all, nothing more complex than that.

DMAN, I blame my lack of the use of AND in the IF..THEN conditions on my lack of creativity. I just didn't think of it and I haven't had my first cup of coffee yet.

Yes.... no... wait!

Login to post a reply

Server time is: 2024-09-19 02:45:04
Your offset time is: 2024-09-19 02:45:04