一个扑克游戏的诞生---扑克牌及相关类代码兼谈异常(下)
发表于:2007-06-30来源:作者:点击数:
标签:
file: CardCollection.cs using System; using System.Diagnostics ; namespace Bigeagle.Games.Cards { /// summary /// 牌集合 /// brAuthor: bigeagle/br /// brDate: 2002/4/19/br /// brHistory: 2002/4/19/br /// /summary public class CardCollection
file: CardCollection.cs
using System;
using System.Diagnostics ;
namespace Bigeagle.Games.Cards
{
/// <summary>
/// 牌集合
/// <br>Author: bigeagle</br>
/// <br>Date: 2002/4/19</br>
/// <br>History: 2002/4/19</br>
/// </summary>
public class CardCollection : System.Collections.ICollection
{
#region 成员变量
/// <summary>
/// 牌数组
/// </summary>
protected Card[] m_arrCards ;
/// <summary>
/// 大小
/// </summary>
protected int m_intSize ;
/// <summary>
/// 最后一个有效元素的索引
/// </summary>
protected int m_intCapacity ;
#endregion
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
public CardCollection()
{
this.m_arrCards = new Card[16] ;
this.m_intCapacity = -1 ;
this.m_intSize = 0 ;
}
#endregion
#region 类方法
/// <summary>
/// 重新设置容量
/// </summary>
/// <param name="capacity">要设置的容量</param>
private void SetCapacity(int capacity)
{
Card[] cards = new Card[capacity];
if (this.m_intSize > 0)
{
if (this.m_intCapacity > 0)
{
Array.Copy(this.m_arrCards , 0 , cards , 0, this.m_intSize) ;
}
// else
// {
// Array.Copy(this.m_arrCards , 0 , cards , 0, this.m_arrCards.Length);
// Array.Copy(this.m_arrCards , 0 , cards , this.m_arrCards.Length, this.m_intCapacity);
// }
}
//this.m_intSize = capacity ;
this.m_arrCards = cards ;
//this.m_intCapacity = this.m_intSize -1 ;
}
/// <summary>
/// 取得元素
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
internal Object GetElement(int i)
{
if(i < 0 || i >= this.m_intSize)
{
throw(new IndexOutOfRangeException()) ;
}
return this.m_arrCards[i] ;
}
/// <summary>
/// 添加一个元素
/// </summary>
/// <param name="a_objCard"></param>
public void Add(Card a_objCard)
{
foreach(Card c in this.m_arrCards)
{
if(c != null && c.Equals(a_objCard))
{
throw(new CardA
lreadyExistsException(c , "牌已经存在")) ;
}
}
if (this.m_intCapacity == this.m_arrCards.Length -1)
{
int newcapacity = this.m_arrCards.Length + 16 ;
SetCapacity(newcapacity);
}
this.m_intCapacity ++ ;
this.m_arrCards[this.m_intCapacity] = a_objCard;
this.m_intSize ++ ;
//this.m_intCapacity ++ ;
//this.m_intSize ++ ;
}
/// <summary>
/// 删除元素
/// </summary>
/// <param name="a_objCard">要删除的Card对象</param>
public void Remove(Card a_objCard)
{
Card[] cards = new Card[this.m_arrCards.Length] ;
int intIndex = -1 ;
for(int i = 0 ; i < this.m_arrCards.Length ; i ++)
{
if(a_objCard == this.m_arrCards[i])
{
intIndex = i ;
break ;
}
}
if(intIndex == -1)
{
throw(new CardNotFoundException(a_objCard , "集合中未找到指定对象")) ;
}
else
{
RemoveAt(intIndex) ;
}
}//end method
/// <summary>
/// 删除元素
/// </summary>
/// <param name="a_intIndex">要删除元素的索引</param>
public void RemoveAt(int a_intIndex)
{
Card[] cards = new Card[this.m_arrCards.Length] ;
if(a_intIndex < 0 || a_intIndex > this.m_intSize - 1)
{
throw(new IndexOutOfRangeException()) ;
}
else
{
if(a_intIndex > 0)
{
Array.Copy(this.m_arrCards , 0 , cards , 0 , a_intIndex) ;
Array.Copy(this.m_arrCards , a_intIndex + 1 , cards
, a_intIndex , this.m_arrCards.Length - a_intIndex -1) ;
}
else
{
Array.Copy(this.m_arrCards , 1 , cards , 0 , this.m_arrCards.Length - 1) ;
}
this.m_arrCards = cards ;
this.m_intCapacity -- ;
this.m_intSize -- ;
}
}
/// <summary>
/// 排序
/// </summary>
/// <param name="a_bIsReverse">排序方向,为真则从大到小排列,否则从小到大排列</param>
// public void Sort(bool a_bIsReverse)
// {
// Card temp = new Card() ;
// for(int i = 0 ; i < this.m_arrCards.Length ; i ++)
// {
//
// }
// }
#endregion//end 类方法
#region 重写父接口方法。
/// <summary>
/// 拷贝数组
/// </summary>
/// <param name="array">数组对象</param>
/// <param name="index">索引</param>
public void CopyTo(System.Array array, int index)
{
this.m_arrCards.CopyTo(array , index) ;
}
/// <summary>
/// 取得有效元素数
/// </summary>
public int Count
{
get
{
return this.m_intSize ;
}
}
public bool IsSynchronized
{
get
{
return false ;
}
}
public object SyncRoot
{
get
{
return this;
}
}
public System.Collections.IEnumerator GetEnumerator()
{
return (new CardEnumerator(this)) ;
}
/// <summary>
/// 重载取得哈希值
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
int result = 0 ;
for(int i = 0 ; i < this.m_intSize ; i ++)
{
result += this.m_arrCards[i].GetHashCode() ;
}
return result;
}
#endregion
#region 静态方法
/// <summary>
/// 取得一副牌
/// </summary>
/// <param name="a_bIncludeJoker"></param>
/// <returns></returns>
public static CardCollection GenerateCards(bool a_bIncludeJoker)
{
//临时数组
CardCollection temp = new CardCollection() ;
//产生一个顺序牌集合
if(a_bIncludeJoker)
{
temp.Add(new Joker()) ;
temp.Add(new ViceJoker()) ;
}
Card card ;
for(int i = 0 ; i < 4 ; i ++)
{
for(int j = 14 ; j > 1 ; j --)
{
card = new Card() ;
card.Colors = (CardColors)i ;
card.Number = (CardNumbers)j ;
temp.Add(card) ;
}
}
return temp ;
}//end method
/// <summary>
/// 生成一副随机顺序的牌
/// </summary>
/// <param name="a_bIncludeJoker">是否包括大小王</param>
/// <returns>一副牌,如果包括大小王返回54张牌,否则返回52张</returns>
public static CardCollection GenerateRandomCards(bool a_bIncludeJoker)
{
//结果
CardCollection result = new CardCollection() ;
//临时数组
CardCollection temp = new CardCollection() ;
//总共产生的牌数
int intCardCount = a_bIncludeJoker ? 54 : 52 ;
temp = CardCollection.GenerateCards(a_bIncludeJoker) ;
//随机排出顺序
//用时间做种子数,用unchecked关键字防止溢出
int seed = unchecked((int)DateTime.Now.Ticks) ;
Random random = new Random(seed) ;
int index = 0 ;
while(temp.Count > 0)
{
index = random.Next(temp.Count) ;
result.Add(temp[index]) ;
temp.RemoveAt(index) ;
}
return result ;
}//end method
#endregion
#region 索引器
/// <summary>
/// 索引器
/// </summary>
public Card this[int Index]
{
get
{
if(Index < 0 || Index > this.Count)
{
throw(new IndexOutOfRangeException()) ;
}
return this.m_arrCards[Index] ;
}
set
{
if(Index < 0 || Index > this.Count)
{
throw(new IndexOutOfRangeException()) ;
}
this.m_arrCards[Index] = value ;
}
}
#endregion
#region 嵌套类
/// <summary>
/// 牌集合迭代
/// <br>Author: bigeagle</br>
/// <br>Date: 2002/4/19</br>
/// <br>History: 2002/4/19</br>
/// </summary>
public class CardEnumerator : System.Collections.IEnumerator
{
private CardCollection m_objCollection;
private int m_intIndex;
private Object m_objCurrentElement;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="a_objCardCollection"></param>
internal CardEnumerator(CardCollection a_objCardCollection)
{
this.m_objCollection = a_objCardCollection ;
this.m_intIndex = 0 ;
this.m_objCurrentElement = this.m_objCollection.m_arrCards ;
if (this.m_objCollection.m_intSize == 0)
this.m_intIndex = -1;
}
/// <summary>
/// 移到下一个元素
/// </summary>
/// <returns>如果索引小于0返回假,否则返回真</returns>
public bool MoveNext()
{
if (this.m_intIndex < 0)
{
this.m_objCurrentElement = this.m_objCollection.m_arrCards ;
return false;
}
this.m_objCurrentElement = this.m_objCollection.GetElement(this.m_intIndex) ;
this.m_intIndex ++ ;
if (this.m_intIndex == this.m_objCollection.m_intSize)
{
this.m_intIndex = -1;
}
return true;
}
/// <summary>
/// 重置
/// </summary>
public void Reset()
{
if (this.m_objCollection.m_intSize == 0)
{
this.m_intIndex = -1;
}
else
{
this.m_intIndex = 0;
}
this.m_objCurrentElement = this.m_objCollection.m_arrCards ;
}
/// <summary>
/// 当前元素
/// </summary>
public object Current
{
get
{
if (this.m_objCurrentElement == this.m_objCollection.m_arrCards)
{
if (this.m_intIndex == 0)
throw new InvalidOperationException("无效操作");
else
throw new InvalidOperationException("无效操作");
}
return this.m_objCurrentElement ;
}
}
}//end class
#endregion
}//endclass
}//end namespace
原文转自:http://www.ltesting.net