Archived

This topic is now archived and is closed to further replies.

Andrew Nguyen

Strangest Piece of Code

Recommended Posts

Post the strangest piece of code you or someone else has written! Mine:
  
//before

int a
int b
int e
//SNIPPET


void SlowComputerDown(e)
{
  for(a=0; a<=(computer_speed()*e); a++;)//computer speed

  {                                     //was found in another

    b+=a                                //function

  }
}

  
But still, I''d have to say, the code was still slow without it (it was a 16bit program, that ran slowly...)

Share this post


Link to post
Share on other sites
You know, no matter how many times I look at this code, I still can''t recall how I arrived at it. Some parts make perfect sense, but others...

  
bool CheckMove(int X, int Y, direction Move)
{
if(Move==LEFT)
{
if(X==1)//Is the left wall in the way?

return false;//Move is illegal

if((Y!=1) && (Y!=((2*level_size)+1)))
{
if((((X-1)%2)==0) && ((Y%2)==0))//Is there a block in the way?

return false;//Move is illegal

}
}
else if(Move==DOWN)
{
if(Y==1)//Is the bottom wall in the way?

return false;//Move is illegal

if((X!=1) && (X!=((2*level_size)+1)))
{
if((((Y-1)%2)==0) && ((X%2)==0))//Is there a block in the way?

return false;//Move is illegal

}
}
else if(Move==RIGHT)
{
if(X==((2*level_size)+1))//Is right wall in the way?

return false;//Move is illegal

if((Y!=1) && (Y!=((2*level_size)+1)))
{
if((((X+1)%2)==0) && ((Y%2)==0))//Is there a block in the way?

return false;//Move is illegal

}
}
else if(Move==UP)
{
if(Y==((2*level_size)+1))//Is top wall in the way?

return false;//Move is illegal

if((X!=1) && (X!=((2*level_size)+1)))
{
if((((Y+1)%2)==0) && ((X%2)==0))//Is there a block in the way?

return false;//Move is illegal

}
}
return true;//Passed all tests, the move is legal

}


Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"

Share this post


Link to post
Share on other sites
This thing is called "Duff''s device". Apparently, it''s some awesome optimization from back in the day

This was taken from the C FAQ: http://www.faqs.org/faqs/C-faq/faq/

  
register n = (count + 7) / 8;
switch (count % 8)
{
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}

Share this post


Link to post
Share on other sites
Oh dear! I am rather embarrased by this ...
    
for(i=0;i<NumObjects;i++)
{
// if the object can move

if(!obj[i].isstatic)
{

// if moving, make previous location empty

if(obj[i].ismoving)
mapObj[obj[i].srcX][obj[i].srcY]=-1;

// get current location

obj[i].srcX=obj[i].destX;
obj[i].srcY=obj[i].destY;

// decide whether or not to move

obj[i].ismoving=rand()%2;

// if we are moving

if(obj[i].ismoving)
{
// get a direction...

obj[i].direction=(rand()%4+1)*2;

// and a destination based on the direction

switch(obj[i].direction)
{
case 2:
obj[i].destX=obj[i].srcX;
obj[i].destY=obj[i].srcY+1;
break;
case 4:
obj[i].destX=obj[i].srcX-1;
obj[i].destY=obj[i].srcY;
break;
case 6:
obj[i].destX=obj[i].srcX+1;
obj[i].destY=obj[i].srcY;
break;
case 8:
obj[i].destX=obj[i].srcX;
obj[i].destY=obj[i].srcY-1;
break;
}

// if there is an object in our path, don't move

if(mapObj[obj[i].destX][obj[i].destY] != -1)
obj[i].ismoving=0;

t=map[obj[i].destX][obj[i].destY];

// if there is an impassable tile in our path, don't move

if(t!= 1 && t != 15 && t != 19 && t!=21)
obj[i].ismoving=0;

// if the player is in our path, dont' move

if(obj[i].destX==pmx && obj[i].destY==pmy)
obj[i].ismoving=0;
}
// if we aren't moving...don't move

if(!obj[i].ismoving)
{
obj[i].destX=obj[i].srcX;
obj[i].destY=obj[i].srcY;
mapObj[obj[i].srcX][obj[i].srcY]=i;
}
else // make our destination occupied

{
mapObj[obj[i].srcX][obj[i].srcY]=i;
mapObj[obj[i].destX][obj[i].destY]=i;
}

}
}


Edited by - Martee on January 16, 2002 2:06:30 AM

Share this post


Link to post
Share on other sites