Vladan899
Vladan899

Reputation: 29

Creating array?

I have this class but i wanna make it 2D dimensional Array. And i do not know how to do it...Everyone tells me to create 2D array so i can easy call it and read array...but i found this way of making items. Now array or gird that will store this times would be helpful. Any code to transfer this class to an array would be helpful.

Weapon base is Inheriting 2 additional Classes.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace RPG
    {
    class WeaponList
    {

        private WeaponBase WoodenSword;
        private WeaponBase RustySword;
        private WeaponBase IronSword;
        private WeaponBase SteelSword;
        private WeaponBase BoardSword;
        private WeaponBase MythrilSword;
        private WeaponBase BloodSword;
        private WeaponBase CoralSword;
        private WeaponBase AncientSword;


        public void CreateWeapon()
        {
            WoodenSword = new WeaponBase();
            WoodenSword.ItemName = "Wooden Sword";
            WoodenSword.ItemDescription = "A basic traning sword...can do some damage";
            WoodenSword.IsStackable = true;    
            WoodenSword.Attack = 4;
            WoodenSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            WoodenSword.ItemID = 1;
            WoodenSword.Price = 10;

            RustySword = new WeaponBase();
            RustySword.ItemName = "Rysty Sword";
            RustySword.ItemDescription = "Old rusty sword....still better than wooden one";
            RustySword.Attack = 5;
            RustySword.IsStackable = true;
            RustySword.WeaponType = WeaponBase.WeaponTypes.Sword;
            RustySword.ItemID = 2;
            RustySword.Price = 50;

            IronSword = new WeaponBase();
            IronSword.ItemName = "Iron Sword";
            IronSword.ItemDescription = "This sword has a broad and sturdy blade, but its iron construction makes it very heavy.";
            IronSword.IsStackable = true;
            IronSword.Attack = 6;
            IronSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            IronSword.ItemID = 3;
            IronSword.Price = 100;

            SteelSword = new WeaponBase();
            SteelSword.ItemName = "Steel Sword";
            SteelSword.ItemDescription = "Hardend version of iron sword";
            SteelSword.IsStackable = true;
            SteelSword.Attack = 7;
            SteelSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            SteelSword.ItemID = 4;
            SteelSword.Price = 500;

            BoardSword = new WeaponBase();
            BoardSword.ItemName = "Borad Sword";
            BoardSword.ItemDescription = "This broad-bladed sword is suited for large slashing strokes. It is inexpensive, but not particularly powerful. ";
            BoardSword.IsStackable = true;
            BoardSword.Attack = 8;
            BoardSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            BoardSword.ItemID = 5;
            BoardSword.Price = 800;

            MythrilSword = new WeaponBase();
            MythrilSword.ItemName = "Mythril Sword";
            MythrilSword.ItemDescription = "A sword forged from the metal known as mythril. Its brilliantly shining blade is incredibly lightweight.";
            MythrilSword.IsStackable = true;
            MythrilSword.Attack = 10;
            MythrilSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            MythrilSword.ItemID = 6;
            MythrilSword.Price = 1600;

            BloodSword = new WeaponBase();
            BloodSword.ItemName = "Blood Sword";
            BloodSword.ItemDescription = "The blade of this sword is a deep crimson, as if it were drenched in blood. It is cruelly sharp.";
            BloodSword.IsStackable = true;
            BloodSword.Attack = 8;
            BloodSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            BloodSword.ItemID = 7;
            BloodSword.Price = 1400;
            BloodSword.specialAttack = "Drains Foe's HP";

            CoralSword = new WeaponBase();
            CoralSword.ItemName = "Coral Sword";
            CoralSword.ItemDescription = "The handle of this single-edged sword has been decorated with intricate coral piecework.";
            CoralSword.IsStackable = true;
            CoralSword.Attack = 11;
            CoralSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            CoralSword.ItemID = 8;
            CoralSword.Price = 2200;
            CoralSword.specialAttack = "Element: Thunder";

            AncientSword = new WeaponBase();
            AncientSword.ItemName = "Ancient Sword";
            AncientSword.ItemDescription = "A sword constructed using ancient techniques that have long since perished from the world.";
            AncientSword.IsStackable = true;
            AncientSword.Attack = 11;
            AncientSword.WeaponType = WeaponBase.WeaponTypes.Sword;
            AncientSword.ItemID = 9;
            AncientSword.Price = 3300;
        }
    }
}

Upvotes: 0

Views: 103

Answers (2)

Monacraft
Monacraft

Reputation: 6620

Well I'm not sure what you're exactly doing, but making a 2D Array is quite simple:

int[,] MyArray = new int[3,4];
// Access like this:
MyArray[0,0] = 1;
MyArray[0,1] = 2;
MyArray[0,2] = 3;
// Etc...

// To Populate using a For Loop:
int i = 0;
for(int x=0; x < 3; x++)
    for(int y=0; y < 4; y++)
    {
        i++;
        MyArray[x, y] = i;
    }

And that's how you make a 2D Array.

Edit:

Making it for a class is a similar process:

MyClass[,] MyArray = new MyClass[3,4];
// Access like this:
MyClass[0,0] = new MyClass();
MyClass[0,1] = new MyClass();
MyClass[0,2] = new MyClass();
// Etc...

// To Populate using a For Loop:
for(int x=0; x < 3; x++)
    for(int y=0; y < 4; y++)
    {
        MyClass[x, y] = new MyClass();
    }

As you can see making a 2D Array for a class is pretty much the same.

Upvotes: 1

Davor Mlinaric
Davor Mlinaric

Reputation: 2027

I suggest this way (vb-pseudo code).

Class WeaponBase
    ItemName
    ItemDescription
    IsStackable
    ...
End Class

Class Weapons
    inherits list (of WeaponBass)
    Sub CreateWeapon
        ' here i will read data from XML or JSON instead of this below

         w = new WeaponBase();
         w.ItemName = "Wooden Sword";
         w.ItemDescription = "A basic traning sword...can do some damage";
         w.IsStackable = true; 
         add(w)   

         w = new WeaponBase();
         w.ItemName = "Metal Sword";
         w.ItemDescription = "A basic traning sword...can do some damage";
         w.IsStackable = true; 
         add(w)

    End Sub

    Sub CreateNewItem(_name, _desc, _stackable)
         w = new WeaponBase();
         w.ItemName = _name;
         w.ItemDescription = _desc;
         w.IsStackable = _stackable; 
         add(w)
    End Sub

End Class

and now you have a list of weapons that you can easily access and do whatevery you wish.

Upvotes: 0

Related Questions