C # Basic OOP - creating a class dictionary with constructors

The code I tried and what went wrong: http://ideone.com/cvLRLg

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    public class Minion
    {
        public static int manaCost;
        public static int attack;
        public static int health;
        public static string cardText;
        public Minion(int mana, int atk, int h, string txt)
        {
            manaCost = mana;
            attack = atk;
            health = h;
            cardText = txt;
        }
        public void displayStats(string name)
        {
            Console.WriteLine(name + "\nMana Cost: " + manaCost + "\nAttack: " + attack + "\nHealth: " + health + "\n" + cardText + "\n");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<string> indexList = new List<string>();
            Dictionary<string, Minion> minionList = new Dictionary<string, Minion>();

            //buffer so I start at 1 and not 0
            indexList.Add("MissingNo");

            //make a Wolfrider card
            indexList.Add("Wolfrider");
            Minion Wolfrider = new Minion(3, 3, 1, "Charge");
            minionList.Add(indexList[1], Wolfrider);

            //make a Goldshire Footman card
            indexList.Add("Goldshire Footman");
            Minion GoldshireFootman = new Minion(1, 1, 2, "Taunt");
            minionList.Add(indexList[2], GoldshireFootman);

            //look through all my cards
            for (int i = 1; i < indexList.Count(); i++)
                minionList[indexList[i]].displayStats(indexList[i]);
            Console.ReadLine();
        }
    }
}

I tried to teach myself C #, but it pounded me. I want to make a dictionary that takes a string, then returns Minion (new class).

A Minion takes four arguments when it was done, so I had to allocate a line of code to create a new Minion before adding this to the dictionary.

However, when I look through all the Minions that I have, for some reason the first returns to me the properties of ANOTHER MINION.

Wolfrider
Mana Cost: 1
Attack: 1
Health: 2
Taunt

Goldshire Footman
Mana Cost: 1
Attack: 1
Health: 2
Taunt

The list works correctly because the names are correct ... but Wolfrider has Goldshire lackey properties.

Is there a more efficient / optimized way to do this? If not, what am I doing wrong?

+4
3

, static:

public static int manaCost

, , , , . :

public int ManaCost { get; set; }

indexList Minion .

+6

static . , ?

name:

  public class Minion
    {
        public readonly string name;
        public int manaCost;
        public int attack;
        public int health;
        public string cardText;

        public Minion(string name, int mana, int atk, int h, string txt)
        {
            this.name = name;
            this.manaCost = mana;
            this.attack = atk;
            this.health = h;
            this.cardText = txt;
        }
        public void displayStats()
        {
            Console.WriteLine(name + "\nMana Cost: " + manaCost + "\nAttack: " + attack + "\nHealth: " + health + "\n" + cardText + "\n");
        }
    }

Main List<string> . :

        Dictionary<string, Minion> minionList = new Dictionary<string, Minion>();

        Minion Wolfrider = new Minion("Wolfrider", 3, 3, 1, "Charge");
        minionList.Add(Wolfrider.name , Wolfrider);

        //make a Goldshire Footman card
        Minion GoldshireFootman = new Minion("Goldshire", 1, 1, 2, "Taunt");
        minionList.Add(GoldshireFootman.name, GoldshireFootman);

        foreach(Minion minion in minionList.Values)
          minion.DisplayStats();

        Console.ReadLine();
+1

You should not have static members for your class. Remove the stats in below.

public static int manaCost;
public static int attack;
public static int health;
public static string cardText;

Here is a slightly cleaner version of what you can strive for:

using System;
using System.Collections.Generic;

    namespace ConsoleApplication3
    {
        public class Minion
        {
            public string name { get; set; }
            public int manaCost { get; set; }
            public int attack { get; set; }
            public int health { get; set; }
            public string cardText { get; set; }

            public void displayStats()
            {
                Console.WriteLine(name + "\nMana Cost: " + manaCost + "\nAttack: " + attack + "\nHealth: " + health + "\n" + cardText + "\n");
            }

            class Program
            {
                static void Main(string[] args)
                {
                    var minionList = new List<Minion>();

                    minionList.Add(new Minion() { name = "Wolfrider", attack = 3, cardText = "Charge", health = 3, manaCost = 3 });
                    minionList.Add(new Minion() { name = "GoldShire Footman", attack = 1, cardText = "Taunt", health = 1, manaCost = 2 });

                    //look through all my cards
                    foreach (var minion in minionList)
                        minion.displayStats();
                    Console.ReadLine();
                }
            }
        }
    }
0
source

Source: https://habr.com/ru/post/1621606/


All Articles