日期:2014-05-20 浏览次数:21211 次
using System; using System.Collections.Generic; using System.Text; namespace Huaquan { /// <summary> /// 划拳结果 /// </summary> public enum Result { /// <summary> /// 未知,还没开始判断 /// </summary> Unknown, /// <summary> /// 平局,结果一致 /// </summary> Dogfall, /// <summary> /// 胜,猜中结果 /// </summary> Win, /// <summary> /// 负,对方猜中结果,自己没有猜中 /// </summary> Lost, /// <summary> /// 犯规, /// </summary> Foul, /// <summary> /// 超时,反应时间超出100毫秒 /// </summary> Overtime } /// <summary> /// 酒鬼类 /// </summary> public abstract class Drunkard { /// <summary> /// 出拳 /// </summary> /// <param name="ANumber">出的手指数</param> /// <param name="ASum">猜的合计</param> abstract public void Come(out int AFinger, out int ASum); /// <summary> /// 接收结果 /// </summary> /// <param name="AOtherFinger">对方出的手指数</param> /// <param name="AOtherSum">对方猜的合计</param> /// <param name="AOtherResult">对方划拳的结果</param> /// <param name="ASelfFinger">自己出的手指数</param> /// <param name="ASelfSum">自己猜的合计</param> /// <param name="ASelfResult">自己划拳的结果</param> abstract public void Outcome(int AOtherFinger, int AOtherSum, Result AOtherResult, int ASelfFinger, int ASelfSum, Result ASelfResult); } public class Zswang一号 : Drunkard { public override void Come(out int AFinger, out int ASum) { AFinger = 5; // 每次都出5 ASum = 10; // 每次都猜10 } public override void Outcome(int AOtherFinger, int AOtherSum, Result AOtherResult, int ASelfFinger, int ASelfSum, Result ASelfResult) { /* 这机器人不关心比赛结果 */ } } public class Zswang二号 : Drunkard { private Random random; public Zswang二号() { random = new Random(); } public override void Come(out int AFinger, out int ASum) { ASum = random.Next(10 + 1); //0-10 if (ASum < 5) // 别犯规 AFinger = random.Next(ASum + 1); else AFinger = random.Next(ASum - 5, 5 + 1); } public override void Outcome(int AOtherFinger, int AOtherSum, Result AOtherResult, int ASelfFinger, int ASelfSum, Result ASelfResult) { /* 这机器人也不关心比赛结果 */ } } /// <summary> /// 酒馆类 /// </summary> /// <typeparam name="T1">划拳机器人1</typeparam> /// <typeparam name="T2">划拳机器人2</typeparam> public class Drunkery<T1, T2> where T1 : Drunkard, new() where T2 : Drunkard, new() { /// <summary> /// 东家 /// </summary> private Drunkard eastPlayer; /// <summary> /// 西家 /// </summary> private Drunkard westPlayer; /// <summary> /// 东家积分 /// </summary> private int eastTotal; /// <summary> /// 西家积分 /// </summary> private int westTotal; /// <summary> /// 东家超时次数 /// </summary> private int eastOvertime; /// <summary> /// 西家超时次数 /// </summary> private int westOvertime; /// <summary> /// 划拳次数 /// </summary> public const int comeCount = 1000; /// <summary> /// 超时罚分 /// </summary> public const int overtimePenalty = 1; /// <summary> /// 异常罚分 /// </summary> public const int catchPenalty = 100; /// <summary> /// 开始比赛 /// </summary> public void Play() { #region 初始化 long vEastTick = Environment.TickCount; // 东家初始化的时间 eastPlayer = new T1(); vEastTick = Environment.TickCount - vEastTick; long vWestTick = Environment.TickCount; // 西家初始化的时间 westPlayer = new T2(); vWestTick = Environment.TickCount - vWestTick; eastTotal = 0; westTotal = 0; eastOvertime = 0; westOvertime = 0; #region 超时处理 if (vEastTick > 1000 || vWestTick > 1000) { if (vEastTick > 1000) Console.WriteLine("{0}初始化严重超时", typeof(T1).Name); if (vWestTick > 1000) Console.WriteLine("{0}初始化严重超时", typeof(T2).Name); return; } if (vEastTick > 100) { eastTotal -= overtimePenalty; eastOvertime++; } if (vWestTick > 100) { westTotal -= overtimePenalty; westOvertime++; } #endregion 超时处理 #endregion 初始化 #region 猜拳过程 for (int i = 0; i < comeCount; i++) { for (int j = 0; j < 100; j++) { int vEastFinger = 0, vWestFinger = 0; int vEastSum = 0, vWestSum = 0; Result vEastResult = Result.Unknown; Result vWestResult = Result.Unknown; #region 出拳 bool vEastCatch = false; vEastTick = Environment.TickCount; // 东家出拳的时间 try { eastPlayer.Come(out vEastFinger, out vEastSum); } catch // 出现异常 { vEastCatch = true; } vEastTick = Environment.TickCount - vEastTick; bool vWestCatch = false; vWestTick = Environment.TickCount; // 西家出拳的时间 try { westPlayer.Come(out vWestFinger, out vWestSum); } catch // 出现异常 { vWestCatch = true; } vWestTick = Environment.TickCount - vWestTick; #endregion 出拳 #region 出现异常 if (vEastCatch || vWestCatch) { if (vEastCatch) { eastTotal -= catchPenalty; westTotal++; } if (vWestCatch) { westTotal -= catchPenalty; eastTotal++; } break; } #endregion 出现异常