C#棋牌游戏
- 需求
- 代码块
- 定义枚举
- 定义Card类
- 定义CardLibrary类
- *定义Player类
- Main函数中实现
- 结果展示
需求
- 需要定义Card类,该类包含特征:
每张牌的名字;
花色:红心、方块、梅花、黑桃;
牌号:1- 10、J、Q、K、小王、大王 - 定义牌库CardLibrary类,该类包含类成员:
容器存储所有牌;
所有牌的总数量;
索引器(通过int类型下标取出容器对应的牌);
初始化牌库方法;
发牌方法;
洗牌方法
在Main方法测试以上逻辑
代码块
定义枚举
- 根据需求定义花色和牌号两个枚举
enum CardColors{None,//这里考虑到小王大王所以需要定义一个空花色Heart,Block,PlumBlossom,Spade}enum CardNums{Three,//根据牌的大小顺序定义,默认Three的int值为0Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King,One,Two,SmallJoker,BigJoker}
定义Card类
class Card{//卡牌的名字(字段和属性)private string cardName;public string CardName{get { return cardName; }}//卡牌的花色(字段和属性)private CardColors cardColor;public CardColors CardColor{get { return cardColor; }}//卡牌的牌号(字段和属性)private CardNums cardNum;public CardNums CardNum{get { return cardNum; }}//Card类的构造函数 这里需要定义卡牌的花色和牌号public Card(CardColors cardColor, CardNums cardNum){this.cardColor = cardColor;this.cardNum = cardNum;CardNameInit();//卡牌名通过构造函数进行初始化}//卡牌名初始化函数private void CardNameInit(){cardName = cardColor.ToString() + cardNum.ToString();}}
定义CardLibrary类
class CardLibrary{//单例模块 方便在Main函数中直接调用private static CardLibrary instance;public static CardLibrary Instance{get{if (instance == null)instance = new CardLibrary();return instance;}}private Random random;//私有的随机数//私有的牌库private List<Card> cards;//卡牌数量属性public int CardCount{get { return cards.Count; }}//索引器public Card this[int index]{get { return cards[index]; }}//构造函数 这里对字段属性进行初始化public CardLibrary(){cards = new List<Card>();CardsInit();random = new Random();}//牌库初始化public void CardsInit(){for (int i = 1; i <= 4; i++)//四个花色{for (int j = 0; j <= 12; j++)//十二个牌号(这里不包括大小王){cards.Add(new Card((CardColors)i, (CardNums)j));//这里对i,j进行显示转换}}cards.Add(new Card(CardColors.None, CardNums.SmallJoker));cards.Add(new Card(CardColors.None, CardNums.BigJoker));//单独将大小王加进去}//发牌(单个的)public Card HandOutCard(){Card card = cards[CardCount - 1];cards.Remove(card);return card;}//这里做了一个针对斗地主的发牌,将卡牌均分到每个玩家的手中public void HandOutCard(Player[] players){int cardsNum = CardCount;foreach (Player player in players){for (int i = 0; i < cardsNum / players.Length; i++){player.Cards.Add(HandOutCard());}}}//洗牌方法public void ReShuffleCard(){for (int i = 0; i < CardCount; i++)//每次都和最末尾的牌进行交换{int num = random.Next(0, CardCount - i);//每交换一次,随机数范围减少1Card temp = cards[num];cards[num] = cards[CardCount - i - 1];cards[CardCount - i - 1] = temp;}}//展示牌库中所有牌public void ShowCards(){foreach (Card card in cards){Console.Write(card.CardName + " ");}}}
*定义Player类
class Player{public List<Card> Cards { get; set; }public string PlayerName { get; set; }public Player(string playerName){PlayerName = playerName;Cards = new List<Card>();}public void ShowCards(){Console.WriteLine("{0}卡牌", PlayerName);foreach (Card card in Cards){Console.Write(card.CardName + " ");}}}
Main函数中实现
class Program{static void Main(string[] args){//初始化Player[] players = new Player[] { new Player("玩家1"),new Player("玩家2"),new Player("玩家3"),new Player("玩家4") };Console.WriteLine("洗牌前");CardLibrary.Instance.ShowCards();Console.WriteLine();CardLibrary.Instance.ReShuffleCard();Console.WriteLine("\r\n洗牌后");CardLibrary.Instance.ShowCards();Console.WriteLine();//发牌CardLibrary.Instance.HandOutCard(players);//展示玩家卡牌foreach (Player player in players){Console.WriteLine();player.ShowCards();Console.WriteLine();}}}