0

我在将卡实际打印到控制台窗口时遇到问题有什么想法吗?另外,欢迎对我的代码提出任何建议。

namespace Drinking_Game_
{
    class Program
    {

这是我遇到一些麻烦的地方。我无法将调用的卡片打印到 ConsoleWindow。

       public static void Main()
       {
           Deck deck = new Deck();
           Card card = new Card();
           deck.Shuffle();
           var value = deck.TakeCard();
           Console.WriteLine(value);
           Console.ReadLine();
       }
}




public enum Suit
{
    Spades = 0,
    Hearts = 1,
    Diamonds = 2,
    Clubs = 3,
}

public enum CardNumber
{
    Ace = 1,
    Two = 2,
    Three = 3,
    Four = 4,
    Five = 5,
    Six = 6,
    Seven = 7,
    Eight = 8,
    Nine = 9,
    Ten = 10,
    Jack = 11,
    Queen = 12,
    King = 13,
}

public class Card
{
    public Suit Suit { get; set; }
    public CardNumber CardNumber { get; set; }
}

public class Deck
{
    public Deck()
    {
        Reset();
    }

    public List<Card> Cards { get; set; }

    public void Reset()
    {
        Cards = Enumerable.Range(1, 4)
            .SelectMany(s => Enumerable.Range(1, 13)
                                .Select(c => new Card()
                                {
                                    Suit = (Suit)s,
                                    CardNumber = (CardNumber)c
                                }
                                        )
                        ).ToList();
    }

    public void Shuffle()
    {
        Cards = Cards.OrderBy(c => Guid.NewGuid()).ToList();
    }

    public Card TakeCard()
    {
        var card = Cards.FirstOrDefault();
        Cards.Remove(card);

        return card;
    }

    public IEnumerable<Card> TakeCards(int numberOfCards)
    {
        var cards = Cards.Take(numberOfCards);

        var takeCards = cards as Card[] ?? cards.ToArray();
        Cards.RemoveAll(takeCards.Contains);

        return takeCards;
    }
  }
}
4

2 回答 2

0

您需要覆盖卡类下的 tostring 。

于 2013-09-19T01:14:21.157 回答
0

您需要从隐式继承的类中覆盖该ToString()方法的默认实现。ObjectCard

例如:

public class Card
{
    public Suit Suit { get; set; }
    public CardNumber CardNumber { get; set; }

    public override string ToString()
    {
        return CardNumber + " of " + Suit;
    }
}

此外,您应该将Suit枚举修改为 1 到 4。

于 2013-09-19T01:53:07.607 回答