根据变量显示对象信息

时间:2017-01-20 08:10:59

标签: c#

我正在尝试制作一个二十一点游戏,我有一个以ASCII格式打印卡片的功能。

public static void PrintCard()
    {

        Console.SetCursorPosition(x + 0, 0);
        Console.Write(" _____ ");
        Console.SetCursorPosition(x + 0, 1);
        Console.Write("|");
        Console.SetCursorPosition(x + 1, 1);
        Console.Write(Ten.CardChar);
        Console.SetCursorPosition(x + 6, 1);
        Console.Write("|");
        Console.SetCursorPosition(x + 0, 2);
        Console.Write("|");
        Console.SetCursorPosition(x + 6, 2);
        Console.Write("|");
        Console.SetCursorPosition(x + 0, 3);
        Console.Write("|");
        Console.SetCursorPosition(x + 6, 3);
        Console.Write("|");
        Console.SetCursorPosition(x + 0, 4);
        Console.Write("|");
        Console.SetCursorPosition(x + 6, 4);
        Console.Write("|");
        Console.SetCursorPosition(x + 0, 5);
        Console.Write("|");
        Console.SetCursorPosition(x + 6, 5);
        Console.Write("|");
        Console.SetCursorPosition(x + 0, 6);
        Console.Write("|____");
        if(NameOfCard == "Ten")
        {
            Console.SetCursorPosition(x + 4, 6);
            Console.Write(Ten.CardChar);
        }
        else
        {
            Console.SetCursorPosition(x + 5, 6);
            Console.Write(Ace.CardChar);
        }

        Console.SetCursorPosition(x + 6, 6);
        Console.Write("|");

由于存在不同的卡,我如何根据字符串变量选择显示哪个CardChar?这些是卡片对象的详细信息,第一个是CardName,第二个是CardValue,第三个是CardChar。

static Card Ace =   new Card("Ace",   0,  "A");
    static Card Two =   new Card("Two",   2,  "2");
    static Card Three = new Card("Three", 3,  "3");
    static Card Four =  new Card("Four",  4,  "4");
    static Card Five =  new Card("Five",  5,  "5");
    static Card Six =   new Card("Six",   6,  "6");
    static Card Seven = new Card("Seven", 7,  "7");
    static Card Eight = new Card("Eight", 8,  "8");
    static Card Nine =  new Card("Nine",  9,  "9");
    static Card Ten =   new Card("Ten",   10, "10");
    static Card Jack =  new Card("Jack",  10, "J");
    static Card Queen = new Card("Queen", 10, "Q");
    static Card King =  new Card("King",  10, "K");

如果说Ten.CardChar,我怎么能用字符串来工作,比如NameOfCard.CardChar?其中Ten存储在NameOfCard中?

1 个答案:

答案 0 :(得分:0)

我不知道我的问题是否正确,但您已经问过如何根据字符串选择卡片了吗?这可以使用LocalizationProvidersModule class:

轻松完成
Dictionary<>

将此结合到您的代码中:

Dictionary<string, Card> _cards = new Dictionary<string, Card>();
//_cards.Add("ace", Ace);
//or
_cards.Add("ace", new Card("Ace",   0,  "A"));