中国象棋-棋子

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

  

 class RedGeneral : Chess
    {
        public RedGeneral(string ChessName, Point Location)
            : base(ChessName, Location)
        {
        }

        protected override bool RouteProtocol(Point Location, Point destination, ref List<Chess> 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<Chess> 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<Chess> 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<Chess> 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<Chess> 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<Chess> 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<Chess> 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<Chess> 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<Chess> 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.将军);
                }
            }
        }
    }

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

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

posted @ 2015-12-01 15:28  刘小白2011  阅读(361)  评论(0)    收藏  举报