GPWiki.org
GPWiki.org
It is currently Fri May 24, 2013 6:17 am

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Mon Jul 16, 2012 12:18 am 
Rookie

Joined: Sun Jul 15, 2012 10:15 pm
Posts: 4
I may be thinking into this too much at this point.

I have a class Ruler with a variable name and a class Land with a variable name.

I create an object "John" in my Ruler class with name "John".
I create an object "land3" in my Land class with name "land3". I set the ruler of land3 to John.name.

I now want to make a function that when I select land3 it will get ruler from the land3 object then get the power from the ruler object(John in this case). Then use this power in a formula within the function.

How do I get a value from an object that I get from another object?

_________________
If there any programmers knowledgeable in C# that would be willing to share their skype contact information for general question please message me separately.


Top
 Profile  
 
PostPosted: Mon Jul 16, 2012 3:12 am 
Grand Optimizer
User avatar

Joined: Mon Jan 17, 2005 6:01 pm
Posts: 352
Location: Canada
I'd reccomend an array of "Rulers". Rather than giving your land class a ruler string, give it an index number. As an example:

Rulers[0] is John
Rulers[1] is Larry
Rulers[2] is Kevin

etc.

Then, in your class definition for "land", you could add a class variable called "RulerIndex"... and say something like:

land3.RulerIndex = 0; //which according to above would point to John.

Once you've got that going, if you want to reference the ruler info from your land class, you could say something like:

MyNeedfullStringVariable = Rulers[land3.RulerIndex].Name;

or

land4.CitizenFearLevel = (land3.Power * Rulers[land3.RulerIndex].Ruthlessness);

and so on.

Hope that helps. I'm sure there's better ways of doing it, but this is how I do it.

_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com


Top
 Profile  
 
PostPosted: Mon Jul 16, 2012 8:04 am 
Shake'n'Baker

Joined: Sun May 27, 2012 6:01 pm
Posts: 62
A class is a description of an objects taxonomy. (e.g. its classification)

a 'ruler' is a role, using the relationship 'owner'. Assuming a 1-to-many (1 owner can have many lands, 1 land can only have 1 owner)

class person {...}

class land {
person *owner;
person get_owner() { return owner; }

...
new_ruler(person *nowner) { owner = nowner; }
};

* ive renamed it to owner because land does not have a 'ruler', citizens do.

Ruler is a role, that is required in an algorithm which may (most likely) require lots of other objects to collaborate in order to achieve a goal/result.

e.g. A rulers 'power' maybe related to the owners wealth, each lands economy, each citizens happiness, the size of the army and each allies faithfulness. etc.

If you tell us who requires to know the rulers power (whats the context, when is it used) we can help alittle more.

cxzuk


Top
 Profile  
 
PostPosted: Mon Jul 16, 2012 11:44 pm 
Rookie

Joined: Sun Jul 15, 2012 10:15 pm
Posts: 4
You guys gave me a lot to think about but my actual problem seems to stem from my inability to correctly create objects. I have been reading a crapload of chapters on classes and I cannot seem to find any good information on how to effectively create new objects that are accessible by my functions.

I have not gotten very far since I want this to work correctly before I keep going. Here is the ghetto code I threw together until I can get all the moving parts working together. I know the Generator Function is completely wrong for creating my objects but it is the closest thing I have gotten to 'working'.

Code:
        #region Land Class
        public class Land
        {
            public int location { get; set; }
            public string name { get; set; }
            public Ruler ruler { get; set; }
            public int troops { get; set; }
        }
        #endregion

        #region Ruler Class
        public class Ruler
        {
            public string name { set; get; }
            public int power { set; get; }
        }
        #endregion

static void Generator()
{
            Ruler Hlok = new Ruler();
            Hlok.name = "Hlok";
            Hlok.power = 65;

            Land land1 = new Land();
            land1.location = 0;
            land1.name = "Onett";
            land1.ruler = Hlok;
            land1.troops = 300;
}

#region Enemy Selection
        static void EnemySelection()
        {
            Console.WriteLine("Which land do you wish to attack?");
            Console.WriteLine("1) Land 1");
            Console.WriteLine("2) Land 2");
            Console.WriteLine("3) Land 3");
            Console.WriteLine("4) Land 4");
            int a = Convert.ToInt32(Console.ReadLine());
            switch (a)
            {
                case 1:
                    Console.WriteLine("This is your land");
                    EnemySelection();
                    break;

                case 2:
                    int Ruler = land2.ruler;
                    int ePower = Ruler.power;
                    break;
                   
                case 3:
                    int Ruler = land3.ruler;
                    int ePower = Ruler.power;
                    break;

                case 4:
                    int Ruler = land4.ruler;
                    int ePower = Ruler.power;
                    break;

                default:
                    Console.WriteLine("Invalid Selection");
                    EnemySelection();
                    break;
            }
        }

        #region Battle System
        static void Battle()
        {
            int ran = RandomGenerator.GetRandomNumber(100);
            int ran2 = RandomGenerator.GetRandomNumber(100);
            if ((pPower + ran) >= (ePower + ran2))
            {
                Console.WriteLine("{0}, {1}: {2} has moidered {3}!", ran + pPower, ran2 + ePower, pName, eName);
            }
            if ((pPower + ran) < (ePower + ran2))
            {
                Console.WriteLine("{0}, {1}: {2} has been defeated..", ran + pPower, ran2 + ePower, pName);
            }
            RepeatBattle();
         }
#endregion

_________________
If there any programmers knowledgeable in C# that would be willing to share their skype contact information for general question please message me separately.


Top
 Profile  
 
PostPosted: Tue Jul 17, 2012 12:59 am 
King Code Monkey
User avatar

Joined: Wed Sep 01, 2004 3:05 pm
Posts: 11182
Location: Abingdon, MD
You need to study object scope. Object created inside functions only exist in those functions. You need to create your objects so they can be seen by all the functions that need to see them.

Code:
   class Program
    {
        public static Land SomeLand;

        static void Main(string[] args)
        {
            SomeLand = new Land();

            Ruler ruler = new Ruler();
            ruler.name = "John";
            ruler.power = 10;

            SomeLand.ruler = ruler;

            ShowRulerForLand();

            while(Console.Read() != 32)
            {

            }
        }

        public static void ShowRulerForLand()
        {
            Console.WriteLine(SomeLand.ruler.name);
        }


Note that even though the ruler for SomeLand is created inside the Main function it can still be seen by ShowRulerForLand because SomeLand holds a reference to it.

_________________
Bored? Head on over to my blog and see what I'm up to.

Microsoft XNA MVP


Top
 Profile  
 
PostPosted: Tue Jul 17, 2012 1:52 am 
Rookie

Joined: Sun Jul 15, 2012 10:15 pm
Posts: 4
I am missing something really simple. Everything has made sense but I am still unable to figure out how to define my objects to be usable within my functions. I am going to scan everything I can find about object scope.


Let me see if explaining the first few things I want to accomplish will help. Maybe I will figure out what I am missing along the way.

First I want to define a Land Class which currently I only need a name, the current ruler residing, and the number of troops.

Second I want to define a Ruler Class which currently only needs a name and a power. They will both have quite a few parameters later once I get my current functions working.

Third I would like to randomly generate (4 for testing purposes) around 40 lands. I would like to generate (4 for testing) 10-15 Rulers and randomly assign them to the 40 lands.

Fourth I would like to give the ability for you as the player to select a Ruler. This will place you in control of the lands which currently have your Ruler assigned.

Fifth I would like to be able to select another land to attack. This will run a basic battle function that uses a formula based on troops and power + a random value to dictate the winner. The loser will lose a percentage of troops based on the difference between the values.


I had 1-4 working but poorly coded. When I started rewriting the code to use classes for the land and rulers I butchered it completely. Thanks for all the patience guys.

_________________
If there any programmers knowledgeable in C# that would be willing to share their skype contact information for general question please message me separately.


Top
 Profile  
 
PostPosted: Tue Jul 17, 2012 2:05 pm 
Grand Optimizer
User avatar

Joined: Mon Jan 17, 2005 6:01 pm
Posts: 352
Location: Canada
I put comments a few lines down in here.. not terribly visible because it's all green, but have a look.

Code:
#region Battle System
        static void Battle()
        {
            int ran = RandomGenerator.GetRandomNumber(100);
            int ran2 = RandomGenerator.GetRandomNumber(100);
            if ((pPower + ran) >= (ePower + ran2)) //ePower isn't defined in this method , or at the class level, nor is it passed as a parameter. the Battle() method cannot "see" that variable, and therefore this code wont run. I also don't see any definitions for pPower or pName or eName.
           {
                Console.WriteLine("{0}, {1}: {2} has moidered {3}!", ran + pPower, ran2 + ePower, pName, eName);
            }
            if ((pPower + ran) < (ePower + ran2))
            {
                Console.WriteLine("{0}, {1}: {2} has been defeated..", ran + pPower, ran2 + ePower, pName);
            }
            RepeatBattle();
         }
#endregion



It's something called "scope". Consider this structure:

Code:
class SomeClass
{

int ClassInt = 4;

        static void Method1()
         {
            int X = 1;
             //this method can access X, and ClassInt.. but not Y.
          }

         static void Method2()
          {
            int Y = 2;
            //this method can access Y, and ClassInt.. but not X.
           }
}


X and Y are 'local' variables to their own respective methods, and only exist within them. ClassInt is a class variable, and is accessible to all members of that class, which includes Method1() and Method2(). Try defining your pPower variables higher up in the class structure, or you could try passing them as arguments to your battle method.

Hope this helps.

_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com


Top
 Profile  
 
PostPosted: Wed Jul 18, 2012 5:24 pm 
Shake'n'Baker

Joined: Sun May 27, 2012 6:01 pm
Posts: 62
Some opinion of whats "best" which you may disagree with.

Hlok wrote:
[1]First I want to define a Land Class which currently I only need a name, the current ruler residing, and the number of troops.

[2]Third I would like to randomly generate (4 for testing purposes) around 40 lands.


Ive read [2] to mean some kind of land mass, or grid square?

Names are important. Good names make understanding things easier. is "Land" really a good name for what your trying to describe? It sounds like your class is trying to manage multiple responsibilities.

For my model, id be tempted to name [1] something along the lines of "empire" or "territory".
and [2] "land" - with the meaning of the square or area of ground.

https://en.wikipedia.org/wiki/Single_responsibility_principle

Code:
class RULER {
  EMPIRE * territory; // Its a pointer, because the ruler of an empire may change.
};

// For me, an Empire "owns" its army, not the ruler.
class EMPIRE {
  // Its a list pointing to each troop, as I guess you might want to do stuff with them at some point?
  std::list<PERSON *> troops;
  int army_size() {
    return troops.size();
  }

  std::list<LAND *> empire_coverage;

  void add_land_to_empire(LAND *l) {
    empire_coverage.push_back(l);
  }

  int empire_size() {
    return empire_coverage.size();
  }
};

// A chunk of land doesn't care about "rulers" or "empires"
class LAND {
  // Sounds abstract maybe?
};

class GRASS: inherit LAND { ... };

class SAND: inherit LAND { ... };


Quote:
Third I would like to randomly generate (4 for testing purposes) around 40 lands. I would like to generate (4 for testing) 10-15 Rulers and randomly assign them to the 40 lands.


Its normally good practise (In my opinion anyway) to have a class, which acts as a controller, that represents the system or sub-system.

Code:
class KINGDOM_WARS {
  // Stereotype <<control>>
 
  // Set up my interface objects (How i talk to the user).
  // Have functions which react to signals from the interface objects.

  KINGDOM_WARS() {
    //Constructor will set up your lands and rulers.
  };

  void pick_ruler() {
    // An event fired this function from the interface object.
  };
 
  void attack_this_land() {
    //
  };
};


Quote:
Fourth I would like to give the ability for you as the player to select a Ruler. This will place you in control of the lands which currently have your Ruler assigned.

Fifth I would like to be able to select another land to attack. This will run a basic battle function that uses a formula based on troops and power + a random value to dictate the winner. The loser will lose a percentage of troops based on the difference between the values.


I hope when reading the classes on LAND, EMPIRE and RULER, you've seen that multiple empires can "own" a LAND, and an EMPIRE can be ruled by multiple RULERS.

This is a business policy, These rules depend on your game/application. You could make a reference pointer in LAND and EMPIRE to its owner, but this is considered an OO "forgery" (I believe thats the correct term). Information should be kept in one place and one place only. You will discover forgeries causing you many issues down the road trying to keep them in sync and you will start to loose cohesion.

What you need to do is make a set of functions that will correctly alter the information so that it is correct to your rules. E.g. When some land is "won", the loser removes the land from its empire, and the "winner" adds the land to its empire.

All the best!
Mike


Top
 Profile  
 
PostPosted: Wed Jul 18, 2012 11:39 pm 
Rookie

Joined: Sun Jul 15, 2012 10:15 pm
Posts: 4
You guys have been awesome. I am going to study some more on my end to figure out what I am missing. I am confident I can put this project together in a month or two having the rapid support you guys have been offering.

_________________
If there any programmers knowledgeable in C# that would be willing to share their skype contact information for general question please message me separately.


Top
 Profile  
 
PostPosted: Thu Jul 19, 2012 8:18 am 
Shake'n'Baker

Joined: Sun May 27, 2012 6:01 pm
Posts: 62
Hlok wrote:
You guys have been awesome. I am going to study some more on my end to figure out what I am missing. I am confident I can put this project together in a month or two having the rapid support you guys have been offering.


Ive only been here a short time, and must admit this is the friendliest community i've met. Some lovely and helpful people.

Good luck and let us know how you get on.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group