使用数组示例

namespace Cards
{
 class Game
 {
  public Hand North()
  {
   return north;
  }

  public Hand South()
  {
   return south;
  }

  public Hand West()
  {
   return west;
  }

  public Hand East()
  {
   return east;
  }

  public void Clear()
  {
   north.Clear();
   south.Clear();
   west.Clear();
   east.Clear();
  }

  public void ReturnHandsTo(Pack pack)
  {
   north.ReturnCardsTo(pack);
   south.ReturnCardsTo(pack);
   west.ReturnCardsTo(pack);
   east.ReturnCardsTo(pack);
  }

  private Hand north = new Hand(),
            south = new Hand(),
            west = new Hand(),
            east = new Hand();
 }
}

用户 的hand 中的牌类

namespace Cards
{
 using System;
 using System.Collections;
/*用户在任何时候 一张牌要么在手上 要么在pack 里*/
 class Hand
 {
  public void Accept(PlayingCard dealt)
  {
   cards.Add(dealt);
  }

  public void Clear()
  {
   cards.Clear();
  }

  public override string ToString()
  {
   string result = "";
   foreach (PlayingCard card in cards)
   {
    result += card.ToString();
    result += "/r/n";
   }

   return result;
  }

  public void ReturnCardsTo(Pack pack)
            /* 创立一个循环来遍历一个用户手上的牌 并将它们传回pack*/
            //
  {
            foreach (PlayingCard card in cards)
            {
                pack.Accept(card );
            }
   // to do
  }

  private ArrayList cards = new ArrayList();
 }
}

//CARD  牌///

namespace Cards
{
 using System;
 using System.Collections;

 class Pack
 {
  public Pack()// 构造函数来创建52 扑克
  {
   for (int pips = PlayingCard.Ace; pips <= PlayingCard.King; ++pips)
   {
    Accept(new PlayingCard(Suit.Clubs, pips));
    Accept(new PlayingCard(Suit.Diamonds, pips));
    Accept(new PlayingCard(Suit.Hearts, pips));
    Accept(new PlayingCard(Suit.Spades, pips));
   }
  }

  public bool IsEmpty()
  {
   return cards.Count == 0;
  }

  public PlayingCard Deal()//发牌
  {
   if (cards.Count == 0)
   {
    throw new InvalidOperationException("can't deal from empty pack");
   }
   PlayingCard dealt = (PlayingCard)cards[0];
   cards.RemoveAt(0);
   return dealt;
  }

  public void Accept(PlayingCard card)//添加到arraylist
  {
   cards.Add(card);
  }

  public void Clear()
  {
   cards.Clear();
  }

  public void Shuffle()// 洗牌
  {
   // to do
            Random random = new Random();
            for (int i = 0; i != cards.Count; i++)
                //foreach 只是提供可读访问权限
            {
                int cardToSwap = random.Next() % cards.Count;
                object temp = cards[i];
                cards[i] = cards[cardToSwap];
                cards[cardToSwap]=temp;
            }
  }

        private ArrayList cards = new ArrayList();//cards类型的 private arraylist
 }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值