内蒙古 其他
IT茶座,IT公司查询    
[搜索] [Google 站内搜索] 添加新公司    
一个扑克游戏的诞生---扑克牌及相关类代码兼谈异常(下)
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 CardAlreadyExistsException(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 ++)
     
关于我们 | 联系我们 | 服务条款 | 帮助中心 | 公司评论 | 会员注册
沪ICP证041070号 Copyright©2007 ITCups All rights Reserved