RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

The Operator that could!

Talow

Sorceror
First what is an operator?
Well an operators are symbols that specify which operations to perform in an expression.
For more on operators in C# visit: http://msdn.microsoft.com/en-us/library/6a71f45d.aspx

So a + or a - or even a = are operators, so which operator is this tutorial for?
The modulus(mod) operator, whose symbol is %. I'm sure you know that being percent, but in our environment it means modulus.

So here is where I tell you what it do to save you a google trip. When you divide 2 values the modulus is the remainder. So if you where to do 5/2 you would get 2, but where did that extra one go? well it's not part of this division, but if you do the same thing with a %, or 5%2 you get 1, and that's the remainder.

So what are some uses for this guy?

They can help out with looping in large scales, though division.

lets say you have a list, you don't know how long the list could be, but you know that there are 6 options for this list, and that's it...

so we can assign the list count and mod it by 6, so now, no mater what our answer is going to come out with a reference of 0-5.

60%6 = 0 there is no remainder
61%6 = 1
62%6 = 2
...
65%6 = 5
66%6 = 0

and this is how it will continue. So now you can figure out which option of that list was referenced without having to drop a for loop and search it out, so you can then us this in a switch...

Code:
switch(listref.Count%6)
{
  count:0
  {
    //here the remainder = 0
  }
  count:1
  {
    //I'm so not writeing this for each one.
  }
  count:2
  {
    //I do think you get the point right?
  }
}
So now you can see that no matter how long the list, it will return into your case, because your case has the 6 options and by finding the remainder of the count divided by 6 you can only get 6 answers (yes we count 0).

FYI: 0 % x where x = any integer = 0
 

Vorspire

Knight
Another great example for the modulus operator would be in a level system that gives rewards after every 10th level;

Rich (BB code):
//In the context of a Mobile class scope, where "this" refers to the current Mobile object...

public virtual void OnLevelUp( int level )
{
        if( this.Level % 10 == 0 ) //If the level is divisible by 10 with a 0 remainder
        { this.AddItem( new SuperSpecialRewardItem( ) ); }
}

You can also use it as a sorting algorithm for generating a checker-board;
Rich (BB code):
public static void FindCheckerBoardTiles( Rectangle3D rect, out Point3D[] blackTiles, out Point3D[] whiteTiles )
{
        int
             count = (rect.Width * rect.Height) / 2,
             blackIndex = 0,
             whiteIndex = 0,
             x, y, z;

        blackTiles = new Point3D[ count ];
        whiteTiles = new Point3D[ count ];

        for( x = rect.Start.X; x <= rect.End.X; x++ )
        {
              for( y = rect.Start.Y; y <= rect.End.Y; y++ )
              {
                        z = Map.GetAverageZ( x, y ); //Are these arguments correct? //Supposed to detect the Z axis for the current (x, y)

                        if( x % 2 == 0 && y % 2 == 0 && blackIndex < count )
                        {
                                blackTiles[ blackIndex ] = new Point3D( x, y, z );
                                blackIndex++;
                        }
                        else if( whiteIndex < count  )
                        {
                                whiteTiles[ whiteIndex ] = new Point3D( x, y, z );
                                whiteIndex++;
                        }
                        else
                        { die = true; }

                        if( die )
                        { break; }
              }

              if( die )
              { break; }
        }
}
After using the FindCheckerBoardTiles method, you can easily access the Point3D elements in the Collections by using an iteration (foreach/for) and placing the correctly hued tiles at those locations.
 
Top