C#版智能五子棋游戏(2)


C#版智能五子棋游戏(2)IChessEngine 五子棋实现

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace QiuQiu.ChessEngine
{
   /// <summary>
   /// 游戏引擎
   /// </summary>
   public class ChessEngine : IChessEngine
   {
      #region private fields
      private IPlayer _plyer1;//玩家1
      private IPlayer _plyer2;//玩家2
      private IPlayer _currentPlayer;//当前玩家
      private IChessData _chessData;//棋子数据
      private Thread _doChessThread;//下棋线程
      private IChessLogic _chessLogic;//下棋逻辑
      private ChessTimer _timer;//计时器
      private TimeSpan _timeLimit = TimeSpan.FromMinutes(10);
      
      private bool _disposed = false;
      
      /// <summary>
      /// 数据变化时触发该事件
      /// </summary>
      public event EventHandler DataChange;
      /// <summary>
      /// 当前玩家改变时触发该事件
      /// </summary>
      public event EventHandler PlayerChange;
      /// <summary>
      /// 其他事件发生时触发该事件
      /// </summary>
      public event EngineEventHandle EventFire;
      
      public TimeSpan TimeLimit
      {
         get { return _timeLimit; }
         set { _timeLimit = value; }
      }
      
      /// <summary>
      /// 计时器
      /// </summary>
      public ChessTimer Timer
      {
         get { return _timer; }
      }
      
      /// <summary>
      /// 获取棋子数据的副本
      /// </summary>
      public IChessData ChessData
      {
         get { return _chessData.Copy(); }
      }
      
      /// <summary>
      /// 玩家1
      /// </summary>
      public IPlayer Plyer1
      {
         get { return _plyer1; }
         set { _plyer1 = value; }
      }
      
      /// <summary>
      /// 列家2
      /// </summary>
      public IPlayer Plyer2
      {
         get { return _plyer2; }
         set { _plyer2 = value; }
      }
      
      /// <summary>
      /// 当前玩家
      /// </summary>
      public IPlayer CurrentPlayer
      {
         get { return _currentPlayer; }
      }
      #endregion
      
      /// <summary>
      /// 构造方法
      /// </summary>
      public ChessEngine()
      {
         _chessData = new ChessData();
         _timer = new ChessTimer();
         _timer.Elapsed += new ChessTimerElapsed(_timer_Elapsed);
      }
      
      /// <summary>
      /// 开始
      /// </summary>
      public void Start()
      {
         if (_plyer1 == null || _plyer2 == null)
         {
            return;
         }
         _chessData = new ChessData();
         _chessLogic = new ChessLogic();
         _plyer1.Init(ChessType.Black);
         _plyer2.Init(ChessType.White);
         _currentPlayer = _plyer1;
         _plyer1.PutChess += new PutChessEventHandle(plyerPutChess);
         _plyer2.PutChess += new PutChessEventHandle(plyerPutChess);
         
         _timer.Start(ChessType.Black);
         _doChessThread = new Thread(new ThreadStart(StartDoChess));
         _doChessThread.Start();
         
         //DoChess(_plyer1);
      }
      
      /// <summary>
      /// 停止
      /// </summary>
      public void Stop()
      {
         StopTimer();
         StopThread();
      }
      
      /// <summary>
      /// 停止计时器
      /// </summary>
      private void StopTimer()
      {
         if(_timer != null)
         _timer.Stop();
      }
      
      /// <summary>
      /// 停止线程
      /// </summary>
      private void StopThread()
      {
         if (_doChessThread != null)
         {
            if (_doChessThread.ThreadState != ThreadState.Stopped)
            {
               try
               {
                  _doChessThread.IsBackground = true;
                  _doChessThread.Abort();
               }
               catch (Exception ep)
               {
                  string t = ep.Message;
               }
               finally
               {
                  _doChessThread.Join(5000);
               }
            }
         }
      }
      
/// <summary>
/// 当一家放下棋子事件处理程序
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void plyerPutChess(IPlayer sender, PutChessEventArgs args)
{
   if (_currentPlayer != sender)
   {
      //没轮他该玩家,犯规
      StopTimer();
      FireEvents(EventType.Illegality);
      StopThread();
      return;
   }
   if (args.TheChessPosition.ColIndex < 0 || args.TheChessPosition.ColIndex >= 15 || args.TheChessPosition.RowIndex < 0 || args.TheChessPosition.RowIndex >= 15)
   {
      //不在范围内
      StopTimer();
      FireEvents(EventType.Illegality);
      StopThread();
      return;
   }
   if (_chessData.HasChess(args.TheChessPosition))
   {
      //这里已经有棋子了,犯规
      StopTimer();
      FireEvents(EventType.Illegality);
      StopThread();
      return;
   }
   //落子
   _chessLogic.PutChess(ref _chessData, new ChessInfo(args.TheChessPosition, sender.ChessType));
   if (DataChange != null)
   {
      DataChange(this, EventArgs.Empty);
   }
   //检查是否赢
   ChessType whoWin = _chessLogic.TestWin(_chessData);
   if (whoWin == ChessType.Black)
   {
      StopTimer();
      FireEvents(EventType.BlackWin);
      StopThread();
      return;
   }
   if (whoWin == ChessType.White)
   {
      StopTimer();
      FireEvents(EventType.WhiteWin);
      StopThread();
      return;
   }
   //检查是否是平局
   if (_chessLogic.TestDogfall(_chessData))
   {
      StopTimer();
      FireEvents(EventType.DataFull);
      StopThread();
      return;
   }
   
   //对方下棋
   try
   {
      DoChess(sender == _plyer1 ? _plyer2 : _plyer1);
   }
   catch (ThreadAbortException ep)
   {
   }
   catch (Exception ep)
   {
      string t = ep.Message;
      StopTimer();
      StopThread();
      FireEvents(EventType.Exceptions, ep.Message);
   }
}

/// <summary>
/// 下子
/// </summary>
/// <param name="player"></param>
private void DoChess(IPlayer player)
{
   _currentPlayer = player;
   if (PlayerChange != null)
   PlayerChange(this, EventArgs.Empty);
   _timer.Tick(_currentPlayer.ChessType);
   _currentPlayer.DoChess(ChessData);
}

/// <summary>
/// 开始下子
/// </summary>
private void StartDoChess()
{
   DoChess(_plyer1);
}

/// <summary>
/// 计时器事件处理程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void _timer_Elapsed(object sender, ChessTimerEventArgs e)
{
   //判断是否超时
   if (_timeLimit.Ticks > 0)
   {
      if (e.BlackPlayer > _timeLimit)
      {
         Stop();
         FireEvents(EventType.WhiteWin);
      }
      if (e.WhitePlayer > _timeLimit)
      {
         Stop();
         FireEvents(EventType.BlackWin);
      }
   }
}

/// <summary>
/// 触发一个其他事件
/// </summary>
/// <param name="eventType"></param>
private void FireEvents(EventType eventType)
{
   if (EventFire != null)
   EventFire(this, new EngineEventArgs(this, eventType));
}

/// <summary>
/// 触发一个其他事件
/// </summary>
/// <param name="eventType"></param>
/// <param name="message"></param>
private void FireEvents(EventType eventType, string message)
{
   if (EventFire != null)
   EventFire(this, new EngineEventArgs(this, eventType, message));
}

protected virtual void Dispose(bool disposing)
{
   if (_disposed)
   return;
   if (disposing)
   {
      // TODO: 此处释放受控资源
      if (_timer != null)
      {
         _timer.Stop();
         _timer.Dispose();
         _timer = null;
         StopThread();
      }
   }
   // TODO: 此处释放所有受控资源
   
   _disposed = true;
}

~ChessEngine()
{
   Dispose(false);
}

public void Dispose()
{
   Dispose(true);
   GC.SuppressFinalize(this);
}
}
}



IChessLogic 接口,封装部分逻辑

using System;
using System.Collections.Generic;
using System.Text;

namespace QiuQiu.ChessEngine
{
   /// <summary>
   /// 棋类游戏逻辑接口
   /// </summary>
   public interface IChessLogic
   {
      /// <summary>
      /// 测试是否某一方赢棋
      /// </summary>
      /// <param name="chessData"></param>
      /// <returns></returns>
      ChessType TestWin(IChessData chessData);
      
      /// <summary>
      /// 测试是否是平局
      /// </summary>
      /// <param name="chessData"></param>
      /// <returns></returns>
      bool TestDogfall(IChessData chessData);
      
      /// <summary>
      /// 落子逻辑,在某一点落子后的逻辑
      /// </summary>
      /// <param name="chessData"></param>
      /// <param name="chessInfo"></param>
      void PutChess(ref IChessData chessData, ChessInfo chessInfo);
   }
}




IChessLogic 五子棋实现 ChessLogic

using System;
using System.Collections.Generic;
using System.Text;

namespace QiuQiu.ChessEngine
{
   /// <summary>
   /// 棋类游戏逻辑
   /// </summary>
   public class ChessLogic : IChessLogic
   {
      /// <summary>
      /// 判断是否是某一方赢棋
      /// </summary>
      /// <param name="chessData"></param>
      /// <returns></returns>
      public ChessType TestWin(IChessData chessData)
      {
         if (chessData.ChessCount < 9)
         return ChessType.None;
         int seriesNum = 0;//某个子连续出现的个数
         ChessType nowChess = ChessType.None;
         //遍历列
         for (int rowIndex = 0; rowIndex < chessData.RowCount; rowIndex++)
         {
            for (int colIndex = 0; colIndex < chessData.ColCount; colIndex++)
            {
               ChessType currentChess = chessData.GetChess(new ChessPosition(rowIndex, colIndex));
               if (currentChess == ChessType.None)
               {
                  nowChess = currentChess;
                  seriesNum = 0;
               }
               else if (currentChess == nowChess)
               {
                  seriesNum++;
                  if (seriesNum >= 5)
                  {
                     return currentChess;
                  }
               }
               else
               {
                  nowChess = currentChess;
                  seriesNum = 1;
               }
            }
            nowChess = ChessType.None;
            seriesNum = 0;
         }
         //遍历行
         for (int colIndex = 0; colIndex < chessData.ColCount; colIndex++)
         {
            for (int rowIndex = 0; rowIndex < chessData.RowCount; rowIndex++)
            {
               ChessType currentChess = chessData.GetChess(new ChessPosition(rowIndex, colIndex));
               if (currentChess == ChessType.None)
               {
                  nowChess = currentChess;
                  seriesNum = 0;
               }
               else if (currentChess == nowChess)
               {
                  seriesNum++;
                  if (seriesNum >= 5)
                  {
                     return currentChess;
                  }
               }
               else
               {
                  nowChess = currentChess;
                  seriesNum = 1;
               }
            }
            nowChess = ChessType.None;
            seriesNum = 0;
         }
         //遍历左上到右下
         for (int scolIndex = 0; scolIndex < chessData.ColCount; scolIndex++)
         {
            int rowIndex = 0;
            int colIndex = scolIndex;
            while (rowIndex < chessData.RowCount && colIndex < chessData.ColCount)
            {
               ChessType currentChess = chessData.GetChess(new ChessPosition(rowIndex, colIndex));
               if (currentChess == ChessType.None)
               {
                  nowChess = currentChess;
                  seriesNum = 0;
               }
               else if (currentChess == nowChess)
               {
                  seriesNum++;
                  if (seriesNum >= 5)
                  {
                     return currentChess;
                  }
               }
               else
               {
                  nowChess = currentChess;
                  seriesNum = 1;
               }
               rowIndex++;
               colIndex++;
            }
            nowChess = ChessType.None;
            seriesNum = 0;
         }
         for (int srowIndex = 0; srowIndex < chessData.ColCount; srowIndex++)
         {
            int rowIndex = srowIndex;
            int colIndex = 0;
            while (rowIndex < chessData.RowCount && colIndex < chessData.ColCount)
            {
               ChessType currentChess = chessData.GetChess(new ChessPosition(rowIndex, colIndex));
               if (currentChess == ChessType.None)
               {
                  nowChess = currentChess;
                  seriesNum = 0;
               }
               else if (currentChess == nowChess)
               {
                  seriesNum++;
                  if (seriesNum >= 5)
                  {
                     return currentChess;
                  }
               }
               else
               {
                  nowChess = currentChess;
                  seriesNum = 1;
               }
               rowIndex++;
               colIndex++;
            }
            nowChess = ChessType.None;
            seriesNum = 0;
         }
         //遍历左下到右上
         for (int scolIndex = 0; scolIndex < chessData.ColCount; scolIndex++)
         {
            int rowIndex = 0;
            int colIndex = scolIndex;
            while (rowIndex < chessData.RowCount && colIndex >= 0)
            {
               ChessType currentChess = chessData.GetChess(new ChessPosition(rowIndex, colIndex));
               if (currentChess == ChessType.None)
               {
                  nowChess = currentChess;
                  seriesNum = 0;
               }
               else if (currentChess == nowChess)
               {
                  seriesNum++;
                  if (seriesNum >= 5)
                  {
                     return currentChess;
                  }
               }
               else
               {
                  nowChess = currentChess;
                  seriesNum = 1;
               }
               rowIndex++;
               colIndex--;
            }
            nowChess = ChessType.None;
            seriesNum = 0;
         }
         for (int srowIndex = 0; srowIndex < chessData.RowCount; srowIndex++)
         {
            int rowIndex = srowIndex;
            int colIndex = chessData.ColCount - 1;
            while (rowIndex < chessData.RowCount && colIndex >= 0)
            {
               ChessType currentChess = chessData.GetChess(new ChessPosition(rowIndex, colIndex));
               if (currentChess == ChessType.None)
               {
                  nowChess = currentChess;
                  seriesNum = 0;
               }
               else if (currentChess == nowChess)
               {
                  seriesNum++;
                  if (seriesNum >= 5)
                  {
                     return currentChess;
                  }
               }
               else
               {
                  nowChess = currentChess;
                  seriesNum = 1;
               }
               rowIndex++;
               colIndex--;
            }
            nowChess = ChessType.None;
            seriesNum = 0;
         }
         
         return ChessType.None;
      }
      
      /// <summary>
      /// 测试是否是平局
      /// </summary>
      /// <param name="chessData"></param>
      /// <returns></returns>
      public bool TestDogfall(IChessData chessData)
      {
         return chessData.IsFull;
      }
      
      /// <summary>
      /// 落子逻辑,在某一点落子后的逻辑
      /// </summary>
      /// <param name="chessData"></param>
      /// <param name="chessInfo"></param>
      public void PutChess(ref IChessData chessData,ChessInfo chessInfo)
      {
         chessData.SetChess(chessInfo.ChessPosition, chessInfo.TheChessType);
      }
   }
}



 

版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
C/S框架网
上一篇:C#版智能五子棋游戏(1)
下一篇:C#版智能五子棋游戏(3)
评论列表

发表评论

评论内容
昵称:
关联文章

C#智能五子棋游戏(2)
C#智能五子棋游戏(1)
C#智能五子棋游戏(3)
C#智能五子棋游戏(4)-主窗体
C#积木游戏(改编自DevExpress GridTetris)
C#贪吃蛇小游戏的源代码
.Net Core SignalR简介-用SignalR撸个游戏
C#网络版中国象棋游戏源代码(VS2005)
SQL智能提示插件 SQL Prompt 破解
WMS - 北京某公司智能仓储管理系统 - CSFrameworkV5旗舰成功案例
C# C/S结构快速开发框架标准V2.2 - 视频介绍
智能语音播报音箱收款播报机|收钱音箱|收款盒子4GWifi(支持微信、支付宝)
基于.Net C/S结构系统开发框架 - 标准V2.2正式发布!
正在整理C/S结构系统框架 - 标准(ADO Direct) Ver:2.2
智能分词搜索引擎Lucent.NET
MES - 比亚迪智能制造执行系统
基础V2.0与标准V2.2有什么区别?
CSFrameworkV2.x标准成功案例 - C# Winform C/S架构MES系统
关于C/S系统开发框架标准V2.x停止软件升级的公告
C/S系统快速开发框架 - 标准V2.3

热门标签
.NET5 .NET6 .NET7 APP Auth-软件授权注册系统 Axios B/S B/S开发框架 Bug Bug记录 C#加密解密 C#源码 C/S CHATGPT CMS系统 CodeGenerator CSFramework.DB CSFramework.EF CSFrameworkV1学习版 CSFrameworkV2标准版 CSFrameworkV3高级版 CSFrameworkV4企业版 CSFrameworkV5旗舰版 CSFrameworkV6.0 DAL数据访问层 Database datalock DbFramework Demo教学 Demo下载 DevExpress教程 DOM EF框架 Element-UI EntityFramework ERP ES6 Excel FastReport GIT HR IDatabase IIS JavaScript LINQ MES MiniFramework MIS NavBarControl Node.JS NPM OMS ORM PaaS POS Promise API Redis SAP SEO SQL SQLConnector TMS系统 Token令牌 VS2022 VSCode VUE WCF WebApi WebApi NETCore WebApi框架 WEB开发框架 Windows服务 Winform 开发框架 Winform 开发平台 WinFramework Workflow工作流 Workflow流程引擎 版本区别 报表 踩坑日记 操作手册 代码生成器 迭代开发记录 基础资料窗体 架构设计 角色权限 开发sce 开发技巧 开发教程 开发框架 开发平台 开发指南 客户案例 快速搭站系统 快速开发平台 秘钥 密钥 权限设计 软件报价 软件测试报告 软件简介 软件开发框架 软件开发平台 软件开发文档 软件体系架构 软件下载 软著证书 三层架构 设计模式 生成代码 实用小技巧 收钱音箱 数据锁 数据同步 微信小程序 未解决问题 文档下载 喜鹊ERP 喜鹊软件 系统对接 详细设计说明书 行政区域数据库 需求分析 疑难杂症 蝇量级框架 蝇量框架 用户管理 用户开发手册 用户控件 在线支付 纸箱ERP 智能语音收款机 自定义窗体 自定义组件 自动升级程序