Steffon Scott

Steffon Scott

  • NA
  • 20
  • 12.1k

C# Structures and Data Retrieval

Feb 21 2013 12:30 PM
Sorry if the topic is off, was not sure what to name it.

I'm re-writing a game from C to C# as a learning project. And hope to eventually use C#'s extensive web utility to port the game from it's original IRC method to a more modern web version. The problem I am having is that I feel like I am just piling layers of confusion on myself and going totally round-about ways to solve problems that (i think) have better more succinct solutions. 

Every player object contains army object, which contains a List of unit objects. The unit objects are derived from a struct declaration. 

public struct unit
        {
            public string name;
            public static int attack;
            public static int defend;
            public static int food;
            public static int copper;
            public static int range;
            public static int numattacks;
            public static int swarm;
            public static int train;
            public static int ambush;
        }

^ This also contains a constructor that also sets the initial values but I didn't want to paste too much code in here for readability reasons.

Now, when a person makes a new player they are allocated 200 unit objects ala the addUnit method in the army object. 100 Soldiers and 100 Archers, placed in the List contained in army object.

At this point I can add, remove units using the addUnit removeUnit functions. So I go into the menu.cs file which simply contains a menu object which I interact with as I reconstruct the game. Just to shed a little light on it here is the commands I have thus far.

           Console.WriteLine("1) Add a new character");
            Console.WriteLine("2) Remove an existing character");
            Console.WriteLine("3) Display character stats");
            Console.WriteLine("4) List characters");
            Console.WriteLine("5) Housekeeping");
            Console.WriteLine("6) View character log");
            Console.WriteLine("7) Show character's items");
            Console.WriteLine("8) Quest item");
            Console.WriteLine("9) Review army"); 

The PROBLEM is this under the Review army command I have gotten it to Show the total number of units as well as total "Archers" "Soldiers" etc. but I want to display some of the units stats as well, namely attack and defense. For example here is some console output for Review army.

Please enter your new characters name: Kaelozim
Total Troops: 200
Soldiers: 100 4/4
Archers: 100 4/4

As you can see I do have some sort of numbers in the spot where attack/defense should be, but these numbers in no way correlate to the real values for these units as defined here from the addUnits(int, string) army method:

case "archer":
                    while (count >= 1)
                    {
                        units.Add(new unit("Archer", 0, 6, 0, 1, 1, 1, 1, 0, 0));
                        count--;
                    }

This is the code for where I am actually writing the review output to the screen:

            Console.WriteLine("Soldiers: {0} {1}/{2}", dplayer.unitCount("Soldier"), unit.attack, unit.defend);
            Console.WriteLine("Archers: {0} {1}/{2}", dplayer.unitCount("Archer"), unit.attack, unit.defend);

I actually have no idea what unit. is referring to, since the class is not static and I don't believe I have an object reference, although apparently I have something. My problem is I have this nagging feeling that I am going about this the wrong way and since this is a learning project I really don't want to be practicing unorthodox or irresponsible coding techniques. I have pored over the first 3 pages in Google talking about structs and enums thinking this may hold the answer but alas I am just as confused as ever. I just need someone who has a little insight into my particular problem to make a suggestion. Any help would be appreciated!


Answers (1)