争怎路由网/网站教程/内容

一个扑克游戏的诞生---扑克牌及相关类代码兼谈异常(中)

网站教程2024-07-15 阅读
 关于扑克牌及相关的几个类其他就没有什么可说的了,大家看代码就可以了。

file : card.cs

using System;
using System.Diagnostics ;
namespace Bigeagle.Games.Cards
{

#region 枚举
/// <summary>
/// 花色的枚举
/// </summary>
public enum CardColors
{

/// <summary>
/// 草花
/// </summary>
Club = 0 ,

/// <summary>
/// 方块
/// </summary>
Diamond,

/// <summary>
/// 红心
/// </summary>
Heart ,

/// <summary>
/// 黑桃
/// </summary>
Spade ,

/// <summary>
/// 没有花色,对应joker
/// </summary>
None
}

/// <summary>
/// 牌大小的枚举
/// </summary>
public enum CardNumbers
{
//Zero = 0 ,
Two = 2 ,
Three ,
Four ,
Five ,
Six ,
Seven ,
Eight ,
Nine ,
Ten ,
Jack ,
Queen ,
King ,
Ace ,
ViceJoker ,
Joker
}
#endregion

/// <summary>
/// 扑克牌类
/// <br>author: bigeagle</br>
/// <br>date: 2002/4/19</br>
/// <br>History: 2002/4/19</br>
///
/// </summary>
/// <remarks>继承iComparable接口,用于比较</remarks>
public class Card : System.IComparable
{

#region 成员变量

/// <summary>
/// 花色
/// </summary>
protected CardColors m_enumColors ;

/// <summary>
/// 数字
/// </summary>
protected CardNumbers m_enumNumber ;

#endregion

#region 属性

/// <summary>
/// 存取花色的属性
/// </summary>
public CardColors Colors
{
get
{
return this.m_enumColors ;
}
set
{
if(value == CardColors.None)
{
throw(new InvalidOperationException
("CardColors.None不能用于Card对象")) ;
}
//if(value != CardColors.None)
//{
//this.m_enumColors = value ;
//}
this.m_enumColors = value ;
}
}

/// <summary>
/// 存取大小的属性
/// </summary>
public CardNumbers Number
{
get
{
return this.m_enumNumber ;
}
set
{
if(value == CardNumbers.Joker
(专业提供视频软件下载)

(专业提供视频软件下载)

value == CardNumbers.ViceJoker)
{
throw(new InvalidOperationException
("Zero,Joker和ViceJoker不能用于Card对象")) ;
}
//if(value != CardNumbers.Joker
//&& value != CardNumbers.ViceJoker )
//this.m_enumNumber = value ;
this.m_enumNumber = value ;
}
}

#endregion

#region 构造函数

/// <summary>
/// 构造函数
/// </summary>
public Card()
{
this.m_enumColors = CardColors.Club ;
this.m_enumNumber = CardNumbers.Ace ;
}//end method


/// <summary>
/// 重载构造函数
/// </summary>
/// <param name="a_enumColors">花色</param>
/// <param name="a_enumNumber">大小</param>
public Card(CardColors a_enumColors , CardNumbers a_enumNumber)
{
if(a_enumColors == CardColors.None
(专业提供视频软件下载)

(专业提供视频软件下载)

a_enumNumber == CardNumbers.Joker
(专业提供视频软件下载)

(专业提供视频软件下载)

a_enumNumber == CardNumbers.ViceJoker)
{
throw(new InvalidOperationException
("CardColors.None、CardNumbers.Joker和ViceJoker不能用于Card对象")) ;

}
else
{
this.m_enumColors = a_enumColors ;
this.m_enumNumber = a_enumNumber ;
}
}

#endregion

#region 改写父类方法

/// <summary>
/// 改写ToString方法
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.m_enumColors.ToString() + " " +this.m_enumNumber.ToString() ;
}

/// <summary>
/// 重写取得哈希值
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return (int)(System.Math.Pow(100 , (double)this.m_enumColors + 1)
+ (double)this.m_enumNumber) ;
}

/// <summary>
/// 重写equals方法
/// </summary>
/// <param name="obj">要判断的对象</param>
/// <returns></returns>
public override bool Equals(object obj)
{
//断言
Debug.Assert(obj != null , "要比较的对象不能为空") ;

return obj.GetHashCode() == this.GetHashCode() ;
}

/// <summary>
/// 改写父接口方法
/// </summary>
/// <param name="obj">要比较的对象</param>
/// <returns>
/// 小于零 此实例小于 obj。
/// 零 此实例等于 obj。
/// 大于零 此实例大于 obj。
/// </returns>
public int CompareTo(object obj)
{
if(this.GetHashCode() > obj.GetHashCode())
{
return 1 ;
}
else if(this.GetHashCode() < obj.GetHashCode())
{
return -1 ;
}
else
{
return 0 ;
}
}

#endregion


}//end class
}//end namespace


file: Joker.cs

using System;

namespace Bigeagle.Games.Cards
{
/// <summary>
/// Joker
/// <br>author: bigeagle</br>
/// <br>date: 2002/4/20</br>
/// <br>History: 2002/4/20</br>
/// </summary>
/// <remarks>继承card类</remarks>
public sealed class Joker : Bigeagle.Games.Cards.Card
{
public Joker()
{
this.m_enumColors = CardColors.None ;
this.m_enumNumber = CardNumbers.Joker ;
}

/// <summary>
/// 改写ToString方法
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "Joker" ;
}
}//end method
}//end namespace


file: ViceJoker.cs

using System;

namespace Bigeagle.Games.Cards
{
/// <summary>
/// vice joker
/// <br>author: bigeagle</br>
/// <br>date: 2002/4/19</br>
/// <br>History: 2002/4/19</br>
/// </summary>
public sealed class ViceJoker : Bigeagle.Games.Cards.Card
{
public ViceJoker()
{
this.m_enumColors = CardColors.None ;
this.m_enumNumber = CardNumbers.ViceJoker ;
}

/// <summary>
/// 改写ToString方法
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "ViceJoker" ;
}

}//end class
}//end namespace




……

相关阅读