EQEmulator Forums

EQEmulator Forums (https://www.eqemulator.org/forums/index.php)
-   Archive::Development (https://www.eqemulator.org/forums/forumdisplay.php?f=621)
-   -   coding competition (https://www.eqemulator.org/forums/showthread.php?t=3983)

neotokyo 11-22-2002 02:34 AM

coding competition
 
for(uint32 i=0; i!=max_door_type;i++)
{
const Door* door = GetDoorDBID(i);
if(door == 0 || door->db_id == 0 || strcasecmp(door->zone_name, zone_name))
continue;
if(door->door_id == door_id && strcasecmp(door->zone_name, zone_name) == 0)
return door;
}

whats wrong with these few lines of code ...?
winner gets a free ticket to new delhi, india.

(and btw, this is not to diss anybody, but perhaps people will learn to code if we get the most commonly errors communicated)
*edit: feel free to post my errors too, if you find some.*

Lurker_005 11-22-2002 03:06 AM

Ok, not a coder, but I like to play with it anyhow :p

Shouldn't it be checking for true?
if(door->door_id == door_id && strcasecmp(door->zone_name, zone_name) == 1)

and why is it running thru door types and not door ID's?
for(uint32 i=0; i!=max_door_type;i++)

neotokyo 11-22-2002 03:32 AM

close, but no cigar ;)

string-compare functions always return 0 if strings are equal - so that is ok.
max_door_type is a bad name, but other than that its used correctly.

Bardboy 11-22-2002 03:54 AM

Maybe I can answer that next semester, when I take C Programming I class. :D

Trumpcard 11-22-2002 04:11 AM

This might not be the answer youre looking for, but using != max is a bit dangerous.
If max gets set to a value other than an integer, or the comparison fails to catch the exit condition, which might be easy to do, this is an infinite loop.

Safer practice would be <=max_type

neotokyo 11-22-2002 04:26 AM

trump: right on both accounts. it is much safer, and it isnt the answer i am looking for ;-)

kathgar 11-22-2002 04:36 AM

Code:

/*guessing*/Door * GetDoorByDoorID(const *zone_name,uint32 door_id)
{/*god I hate editing code this way*/
for(uint32 i=0; i!=max_door_type;i++)
{
const Door* door = GetDoorDBID(i);
/*if door is undefined you try to dereference this=bad+crash */
//if(door == 0 || door->db_id == 0 || strcasecmp(door->zone_name, zone_name))
if(door){if(door->db_id==door_id && strcasecmp(door->zone_name, zone_name)==0)
return door;//continue;
//if(door->door_id == door_id/*this doesnt' exist*/ && strcasecmp(door->zone_name, zone_name) == 0)
//return door;}
}
return NULL;//if we don't find it
}

clean block
Code:

Door * GetDoorByDoorID(const *zone_name,uint32 door_id)
{/*god I hate editing code this way*/
        for(uint32 i=0; i!=max_door_type;i++)
        {
                  const Door* door = GetDoorDBID(i);
                  if(door)
                  {
                        if(door->db_id==door_id && strcasecmp(door->zone_name, zone_name)==0)
                              return door;
                  }
        }
        return NULL;//if we don't find it
}

Neo: read the post on the dev board about looking for new devs, then you get CVS access <3

neotokyo 11-22-2002 04:46 AM

do i get a cool board title and avatar then? :-))

and the '@' in irc?

and a trailer and a cook and daily massage?

btw. kath: the code is still wrong but much better then before ;-)

Trumpcard 11-22-2002 05:23 AM

Yep.. And you can boot me out of IRC when I post too many crappy code changes!

kathgar 11-22-2002 05:27 AM

anyone can change their avatar and title... and you would get probably %(halfop.. topic kick ban, just not halfop or op others)

kathgar 11-22-2002 05:39 AM

Code:

/*guessing*/Door * GetDoorByDoorID(const *zone_name,uint32 door_id)
{/*god I hate editing code this way*/
for(uint32 i=0; i!=max_door_type;i++)
{
const Door* door = GetDoorDBID(i);
/*if door is undefined you try to dereference this=bad+crash */
//if(door == 0 || door->db_id == 0 || strcasecmp(door->zone_name, zone_name))
if(door){if(door->db_id==door_id && strcasecmp(door->zone_name, zone_name)==0)
return door;//continue;
//if(door->door_id == door_id/*this doesnt' exist*/ && strcasecmp(door->zone_name, zone_name) == 0)
//return door;}
}
return NULL;//if we don't find it
}

clean block
Code:

Door * GetDoorByDoorID(const *zone_name,uint32 door_id)
{/*god I hate editing code this way*/
        Door* door = NULL;
        for(uint32 i=0; i<max_door_type;i++)
        {
                  door = GetDoorDBID(i);
                  if(door)
                  {
                        if(door->db_id==door_id && strcasecmp(door->zone_name, zone_name)==0)
                              return door;
                  }
        }
        return NULL;//if we don't find it
}

Thats all I see at the moment.. now i'll get some breakfast.. er lunch.. wahtever

alkrun 11-22-2002 06:53 AM

for(uint32 i=0; i <= max_door_type; i++)
{
Door* door = GetDoorDBID(i);
if(door == NULL)
continue;

if(door->db_id == 0 || strcasecmp(door->zone_name, zone_name))
continue;

if(door->door_id == door_id && strcasecmp(door->zone_name, zone_name) == 0)
return door;
}



My personal preference... I hate using 0 in the place of NULL. it's one of my biggest complaints about the emu sourcecode. Since you all don't use hungarian, sometimes it's not directly obvious if a variable is an id or a pointer. If you see this:

if(door == 0)

door could be either. If you see this:

if(door = NULL)

then it's pretty obvious that it's a pointer.

Another of my pointer issues is testing for null in the same conditional as you use to test for a member's value like the original code was testing door and then the door's database id. I prefer to see them split up like I have them. Again, it's a style thing, but with so many people working on the same code, it's easy to get someone to add a condition to your statement and make a mistake.

kathgar 11-22-2002 07:18 AM

Not just a style thing, if you test if the pointer is null and dereference it in the same if it tries to dereference if it is null.. which is quite bad

neotokyo 11-22-2002 11:58 PM

Quote:

Originally Posted by kathgar
Code:

Door * GetDoorByDoorID(const *zone_name,uint32 door_id)
{/*god I hate editing code this way*/
        Door* door = NULL;
        for(uint32 i=0; i<max_door_type;i++)
        {
                  door = GetDoorDBID(i);
                  if(door)
                  {
                        if(door->db_id==door_id && strcasecmp(door->zone_name, zone_name)==0)
                              return door;
                  }
        }
        return NULL;//if we don't find it
}


we have a winner :-))

the problem was:
Code:

Door* door = GetDoorDBID(i);

since the for-loop doesnt leave the scope, the variable door is intialized using GetDoorDBID(i) (which in this case i == 0), and then never again touched. so all you do is that you run the loop thru from i=0 to i=max_door_type, but with always the same door.


take care,
neo

Baron Sprite 11-23-2002 08:28 AM

% is sexy :cool:
oh forgot:
Code:

#include <iostream.h>

int main()
{
    std::cout << "Hello, world!\n";
}



All times are GMT -4. The time now is 10:53 AM.

Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.