博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
中国象棋-棋子
阅读量:6088 次
发布时间:2019-06-20

本文共 9708 字,大约阅读时间需要 32 分钟。

  今天说棋子的实现,先说“帅”吧。

  

class RedGeneral : Chess    {        public RedGeneral(string ChessName, Point Location)            : base(ChessName, Location)        {        }        protected override bool RouteProtocol(Point Location, Point destination, ref List
chessList, ref bool rollbackNeed, ref Chess deadChess) { if ((Location.X - destination.X) * (Location.Y - destination.Y) == 0 && (Math.Abs(Location.X - destination.X) == 1 || Math.Abs(Location.Y - destination.Y) == 1) && destination.X > 2 && destination.X < 6 && destination.Y < 3) { if (!IsAlly(destination, chessList, ChessCamp.红方)) { rollbackNeed = CheckRollBack(Location, destination, ref chessList, ref deadChess); return true; } } else if (Location.X == destination.X) { foreach (Chess c in chessList) if (c.ChessName == "将" && c.Location == destination) { var query = from x in chessList where x.Location.X == destination.X && x.Location.Y > Location.Y && x.Location.Y < destination.Y select x; if (query.Count() == 0) { rollbackNeed = CheckRollBack(Location, destination, ref chessList, ref deadChess); return true; } } } return false; } }

这里重写了RouteProtocol方法,这个方法是判别每个子的行走路径是否合法的,帅分为两种走法,一种是在九宫格里面走,另一种,是直接冲到对方老窝,即杀将。所以在此做了判定,如果是一格一格的走,看看是否出了九宫格是否是横平竖直的走,如果是直接冲,看看帅和将之间还有没有别的子。另外走的时候,还要看看落子位置是不是自己的棋,不是才能走。这样,帅就完成了。士和他差不多,都是在九宫格里,就不说了,说说马吧。

class RedHorse : Chess    {        public RedHorse(string ChessName, Point Location)            : base(ChessName, Location)        {        }        protected override bool RouteProtocol(Point Location, Point destination, ref List
chessList, ref bool rollbackNeed, ref Chess deadChess) { if (Math.Abs((Location.X - destination.X) * (Location.Y - destination.Y)) == 2) { if (!IsAlly(destination, chessList, ChessCamp.红方)) { IEnumerable
query; if (Math.Abs(Location.X - destination.X) == 1) { Point HorseLeg = new Point(Location.X, Location.Y - (Location.Y - destination.Y) / 2); query = from x in chessList where x.Location == HorseLeg select x; } else { Point HorseLeg = new Point(Location.X - (Location.X - destination.X) / 2, Location.Y); query = from x in chessList where x.Location == HorseLeg select x; } if (query.Count() == 0) { rollbackNeed = CheckRollBack(Location, destination, ref chessList, ref deadChess); IsMurderGeneral(destination, chessList); return true; } } } return false; } protected override void IsMurderGeneral(Point destination, List
chessList) { Point BlackGeneralP = new Point(); foreach (Chess c in chessList) if (c.ChessName == "将") BlackGeneralP = c.Location; if (Math.Abs((destination.X - BlackGeneralP.X) * (destination.Y - BlackGeneralP.Y)) == 2) { IEnumerable
query; if (Math.Abs(destination.X - BlackGeneralP.X) == 1) { Point HorseLeg = new Point(destination.X, destination.Y - (destination.Y - BlackGeneralP.Y) / 2); query = from x in chessList where x.Location == HorseLeg select x; } else { Point HorseLeg = new Point(Location.X - (Location.X - destination.X) / 2, Location.Y); query = from x in chessList where x.Location == HorseLeg select x; } if (query.Count() == 0) { soundPlayer.PlaySound(SoundType.将军); } } } }

这里以红马举例,马走日,他的走步里面包含了一个检测绊马腿如果没伴着就能走,另外还要检测每走一步是不是能将军,方法就是将走的一步作为起点,看看能不能到达对方的帅或将的位置。象和马有些类似,也都有被其他子憋住自己不能走的时候,但象不能过河。

接着说炮,车和炮很像,但车的判断比炮还简单一些,因为炮还有一个隔子打子的走法,这里展示下炮的代码

[Serializable]    class RedCannon : Chess    {        public RedCannon(string ChessName, Point Location)            : base(ChessName, Location)        {        }        protected override bool RouteProtocol(Point Location, Point destination, ref List
chessList, ref bool rollbackNeed, ref Chess deadChess) { if ((Location.X - destination.X) * (Location.Y - destination.Y) == 0) { if (!IsAlly(destination, chessList, ChessCamp.红方)) { var chessquery = from x in chessList where x.Location == destination select x; IEnumerable
query; if (Location.X == destination.X) { if (Location.Y > destination.Y) { query = from x in chessList where x.Location.Y < Location.Y && x.Location.Y > destination.Y && x.Location.X == Location.X select x; } else { query = from x in chessList where x.Location.Y > Location.Y && x.Location.Y < destination.Y && x.Location.X == Location.X select x; } } else { if (Location.X > destination.X) { query = from x in chessList where x.Location.X < Location.X && x.Location.X > destination.X && x.Location.Y == Location.Y select x; } else { query = from x in chessList where x.Location.X > Location.X && x.Location.X < destination.X && x.Location.Y == Location.Y select x; } } if (query.Count() == 1 && chessquery.Count() == 1 || query.Count() == 0 && chessquery.Count() == 0) { rollbackNeed = CheckRollBack(Location, destination, ref chessList, ref deadChess); IsMurderGeneral(destination, chessList); return true; } } } return false; } protected override void IsMurderGeneral(Point destination, List
chessList) { Point BlackGeneralP = new Point(); foreach (Chess c in chessList) if (c.ChessName == "将") { BlackGeneralP = c.Location; break; } if ((destination.X - BlackGeneralP.X) * (destination.Y - BlackGeneralP.Y) == 0) { IEnumerable
query; if (destination.X == BlackGeneralP.X) { if (destination.Y > BlackGeneralP.Y) { query = from x in chessList where x.Location.Y < destination.Y && x.Location.Y > BlackGeneralP.Y && x.Location.X == BlackGeneralP.X && x != this select x; } else { query = from x in chessList where x.Location.Y > destination.Y && x.Location.Y < BlackGeneralP.Y && x.Location.X == BlackGeneralP.X && x != this select x; } } else { if (destination.X > BlackGeneralP.X) { query = from x in chessList where x.Location.X < destination.X && x.Location.X > BlackGeneralP.X && x.Location.Y == BlackGeneralP.Y && x != this select x; } else { query = from x in chessList where x.Location.X > destination.X && x.Location.X < BlackGeneralP.X && x.Location.Y == BlackGeneralP.Y && x != this select x; } } if (query.Count() == 1) { soundPlayer.PlaySound(SoundType.将军); } } } }

黑子和红子是一样的,只不过阵营不同而已。

本来想挂上附件的,但不知道怎么挂啊!等晚上回去了传到百度网盘上吧

转载于:https://www.cnblogs.com/MAC-114/p/5010249.html

你可能感兴趣的文章
我的友情链接
查看>>
想说一点东西。。。。
查看>>
css知多少(8)——float上篇
查看>>
NLB网路负载均衡管理器详解
查看>>
水平添加滚动条
查看>>
PHP中”单例模式“实例讲解
查看>>
VS2008查看dll导出函数
查看>>
VM EBS R12迁移,启动APTier . AutoConfig错误
查看>>
atitit.细节决定成败的适合情形与缺点
查看>>
Mysql利用binlog恢复数据
查看>>
我的友情链接
查看>>
用yum安装mariadb
查看>>
一点IT"边缘化"的人的思考
查看>>
WPF 降低.net framework到4.0
查看>>
搭建一个通用的脚手架
查看>>
开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
查看>>
开源磁盘加密软件VeraCrypt教程
查看>>
本地vs云:大数据厮杀的最终幸存者会是谁?
查看>>
阿里云公共镜像、自定义镜像、共享镜像和镜像市场的区别 ...
查看>>
shadowtunnel v1.7 发布:新增上级负载均衡支持独立密码
查看>>