如何在控制台窗口中实际打印下一张卡?

时间:2013-09-19 01:06:37

标签: c#-4.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;
    }
  }
}

2 个答案:

答案 0 :(得分:0)

您需要覆盖卡类下的tostring。

答案 1 :(得分:0)

您需要覆盖ToString()隐式继承的Object类的Card方法的默认实现。

例如:

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

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

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