最新文章 (全部类别)
VS2022消除编译警告
“SymmetricAlgorithm.Create(string)”已过时:“Cryptographic factory methods accepting an algorithm name are obsolete. Use the parameterless Create factory method on the algorithm type instead
SHA256Managed/SHA512Managed已过时:Derived cryptographic types are obsolete. Use the Create method on the base type instead
MD5CryptoServiceProvider已过时:Derived cryptographic types are obsolete. Use the Create method on the base type instead
C#使用HttpClient获取IP地址位置和网络信息
判断IP是否是外网IP、内网IP
C#使用HttpClient获取公网IP
WebRequest.Create(string)已过时:WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead
C#根据第三方提供的IP查询服务获取公网外网IP地址
html/dom/js/javascript开发记录
调试ASP.NETCore Web站点 - 清理IISExpress缓存数据(js,css)
EFCore+Oracle根据不同的Schema连接数据库
主程序集成CSFramework.EF 数据库框架(.NET7版本)
CSFramework.EF数据库框架简介(.NET8+EFCore)
迁移ECS服务器:导致ORACLE监听服务启动不了解决方案
SQLite数据库
VS2022编译报错:Visual Studio 容器工具需要 Docker Desktop
.NET 9 预览版+C#13新功能
EFCore禁用实体跟踪
WebApi开发框架V3.0 (.NETCore+EFCore) 增加AppSettings全局参数类
C#获取应用程序所有依赖的程序集
LINQ Expression 多条件复合条件组合(And/Or)
CSFrameworkV6客户案例 - MHR - 宁德时代制造人力资源系统
CS软件授权注册系统V3 - 发布证书
C/S软件授权注册系统V3.0(Winform+WebApi+.NET8+EFCore版本)
CS软件授权注册系统V3 - 购买方式
CS软件授权注册系统V3 - 试用版下载
CS软件授权注册系统-客户登记(制作证书)
C/S软件授权注册系统V3.0 - 管理员工具
CSFrameworkV6旗舰版开发框架 - 集成软件授权认证系统
CSFramework.Authentication 软件证书管理系统 - 制作软件客户授权证书
CSFramework.Authentication 软件证书管理系统 - MAC地址管理
CSFramework.Authentication 软件授权证书管理系统
Login/Logout接口调用dalUser的Login/Logout方法
C# Newtonsoft.Json.Linq.JObject 转对象
CSFramework.Authentication 软件授权认证系统 - 软件测试报告
C/S架构软件开发平台 - 旗舰版V6.0 - 底层框架迭代开发
C/S架构软件开发平台 - 旗舰版V6.1新功能 - 增加软件授权认证模块
C/S架构软件开发平台 - 旗舰版CSFrameworkV6 Bug修改记录
CS软件授权注册系统V3 - 开发手册 - 软件集成与用户注册
CS软件授权注册系统-模拟MES/ERP用户注册软件
CS软件授权注册系统-发布/部署WebApi服务器(IIS+.NET8+ASP.NETCore)
CS软件授权注册系统-VS2022调试WebApi接口
.NETCore Console控制台程序使用ILogger日志
CS软件授权注册系统-WebApi服务器介绍
ASP.NETCore集成Swagger添加Authorize按钮Bearer授权
CS软件授权注册系统-WebApi服务器配置
.NETCore WebApi发布到IIS服务器无法打开swagger
.NET8/ .NETCore /ASP.NETCore 部署WebApi到IIS服务器需要安装的运行环境
HTTP Error 500.31 - Failed to load ASP.NET Core runtime,Microsoft.NetCore.App or Microsoft.AspNetCore.App was not found.
.net敏捷开发,创造卓越

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 - 视频介绍
基于.Net C/S结构系统开发框架 - 标准V2.2正式发布!
智能分词搜索引擎Lucent.NET
智能语音播报音箱收款播报机|收钱音箱|收款盒子4GWifi(支持微信、支付宝)
正在整理C/S结构系统框架 - 标准(ADO Direct) Ver:2.2
MES - 比亚迪智能制造执行系统
基础V2.0与标准V2.2有什么区别?
CSFrameworkV2.x标准成功案例 - C# Winform C/S架构MES系统
关于C/S系统开发框架标准V2.x停止软件升级的公告
C/S系统快速开发框架 - 标准V2.3

热门标签
软件著作权登记证书 .NET .NET Reactor .NET5 .NET6 .NET7 .NET8 .NET9 .NETFramework APP AspNetCore AuthV3 Auth-软件授权注册系统 Axios B/S B/S开发框架 B/S框架 BSFramework Bug Bug记录 C#加密解密 C#源码 C/S CHATGPT CMS系统 CodeGenerator CSFramework.DB CSFramework.EF CSFramework.License CSFrameworkV1学习版 CSFrameworkV2标准版 CSFrameworkV3高级版 CSFrameworkV4企业版 CSFrameworkV5旗舰版 CSFrameworkV6.0 CSFrameworkV6.1 CSFrameworkV6旗舰版 DAL数据访问层 Database datalock DbFramework Demo教学 Demo实例 Demo下载 DevExpress教程 Docker Desktop DOM ECS服务器 EFCore EF框架 Element-UI EntityFramework ERP ES6 Excel FastReport GIT HR IDatabase IIS JavaScript LINQ MES MiniFramework MIS MySql NavBarControl NETCore Node.JS NPM OMS Oracle资料 ORM PaaS POS Promise API PSD RedGet Redis RSA SAP Schema SEO SEO文章 SQL SQLConnector SQLite SqlServer Swagger TMS系统 Token令牌 VS2022 VSCode VS升级 VUE WCF WebApi WebApi NETCore WebApi框架 WEB开发框架 Windows服务 Winform 开发框架 Winform 开发平台 WinFramework Workflow工作流 Workflow流程引擎 XtraReport 安装环境 版本区别 报表 备份还原 踩坑日记 操作手册 达梦数据库 代码生成器 迭代开发记录 功能介绍 国际化 基础资料窗体 架构设计 角色权限 开发sce 开发工具 开发技巧 开发教程 开发框架 开发平台 开发指南 客户案例 快速搭站系统 快速开发平台 框架升级 毛衫行业ERP 秘钥 密钥 权限设计 软件报价 软件测试报告 软件加壳 软件简介 软件开发框架 软件开发平台 软件开发文档 软件授权 软件授权注册系统 软件体系架构 软件下载 软件著作权登记证书 软著证书 三层架构 设计模式 生成代码 实用小技巧 视频下载 收钱音箱 数据锁 数据同步 微信小程序 未解决问题 文档下载 喜鹊ERP 喜鹊软件 系统对接 详细设计说明书 新功能 信创 行政区域数据库 需求分析 疑难杂症 蝇量级框架 蝇量框架 用户管理 用户开发手册 用户控件 在线支付 纸箱ERP 智能语音收款机 自定义窗体 自定义组件 自动升级程序
联系我们
联系电话:13923396219(微信同号)
电子邮箱:23404761@qq.com
站长微信二维码
微信二维码