Best way to represent game card class in C#

后端 未结 7 1859
难免孤独
难免孤独 2021-01-06 01:23

I use class Card which contains 2 enumerated properties (suite - hearts diamonds spades and clubs) and card value from 2 to A. And overrides ToString() method to returns som

7条回答
  •  时光说笑
    2021-01-06 01:49

    Scratch what I wrote before, this is better.

    using System;
    
    enum Suit
    {
        Clubs,
        Hearts,
        Diamonds,
        Spades
    }
    
    class Card
    {
        Suit Suit
        {
            get;
            private set;
        }
    
        int Value
        {
            get;
            private set;
        }
    
        Card(Suit suit, int value)
        {
            Suit = suit;
            Value = value;
        }
    
        private const string[] valsToString = new string[] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
    
        bool IsValid()
        {
            return Value >= 2 && Value <= 14;
        }
    
        override string ToString()
        {
            return string.Format("{0} of {1}", valsToString[Value - 2], Suit);
        }
    }
    

提交回复
热议问题