一个连珠五子棋的算法

   
为了实现连珠六子及六子以上自杀的功能,中间没有使用break及return 语句,执行效率有所降低。

注:1、使用的是C#语法。
  2、如果“五(子)、六(子)连”按自杀处理。
  3、三三禁军功能还没有实现,以后会进一步完善。

晚辈学习C#的时间不长,还望前辈们多多指教。

   /// <summary>
        /// 是否赢
        /// </summary>
        /// <param name="arrCol">棋盘中的所有棋子</param>
        /// <param name="p">所下棋子的最后一个位置</param>
        /// <returns>是否赢</returns>
        public virtual bool Win(Color[,] arrCol,Point p,out int maxNum)
        {
            maxNum = 0;//标识连续白子或者黑子的最大个数,连续五子取胜,连续六子及六子以上视为自杀
            bool bo = false;//标识是否取胜
            int lenX = arrCol.GetLength(0) - 1;//棋盘行边界索引
            int lenY = arrCol.GetLength(1) - 1;//棋盘列边界索引
            int num = 0;//临时标识变量
            Color col = arrCol[p.X, p.Y];//所下棋子的最后一个棋子的颜色

            for (int i = 0; i < 9; i++)//从左上角 到 右下角 取临界值在九子范围内进行搜索、提高效率
            {
                if ((p.X - 4 + i) < 0 || (p.X - 4 + i) > lenX)//判断是否出界
                    continue;
                if ((p.Y - 4 + i) < 0 || (p.Y - 4 + i) > lenY)//判断是否出界
                    continue;
                if (arrCol[p.X - 4 + i, p.Y - 4 + i] == col)//如果是同一种颜色的棋子,则连续子的个数加一;反之,归零(*****)
                {
                    num++;
                    if (num == 5)//标识五子胜
                    {
                        if (num > maxNum)//(*****)
                        {
                            maxNum = num;
                        }
                        bo = true;
                    }
                    else if(num > 5)//标识六子及六子以上自杀
                    {
                        if (num > maxNum)//(*****)
                        {
                            maxNum = num;
                        }
                        bo = false;
                    }
                }
                else
                    num = 0;//(*****)
            }
            num = 0;//归零(*****)

            /*几乎所有注释下同*/
            for (int i = 0; i < 9; i++)//从 左下角 到 右上角
            {
                if ((p.X + 4 - i) < 0 || (p.X + 4 - i) > lenX)
                    continue;
                if ((p.Y - 4 + i) < 0 || (p.Y - 4 + i) > lenY)
                    continue;
                if (arrCol[p.X + 4 - i, p.Y - 4 + i] == col)
                {
                    num++;
                    if (num == 5)
                    {
                        if (num > maxNum)
                        {
                            maxNum = num;
                        }
                        bo = true;
                    }
                    else if (num > 5)
                    {
                        if (num > maxNum)
                        {
                            maxNum = num;
                        }
                        bo = false;
                    }
                }
                else
                    num = 0;
            }
            num = 0;
            for (int i = 0; i < 9; i++)//从 左 到 右
            {
                if ((p.X - 4 + i) < 0 || (p.X - 4 + i) > lenX)
                    continue;
                if (arrCol[p.X - 4 + i, p.Y] == col)
                {
                    num++;
                    if (num == 5)
                    {
                        if (num > maxNum)
                        {
                            maxNum = num;
                        }
                        bo = true;
                    }
                    else if (num > 5)
                    {
                        if (num > maxNum)
                        {
                            maxNum = num;
                        }
                        bo = false;
                    }
                }
                else
                    num = 0;
            }
            num = 0;
            for (int i = 0; i < 10; i++)//从 上 到 下
            {
                if ((p.Y - 4 + i) < 0 || (p.Y - 4 + i) > lenY)
                    continue;
                if (arrCol[p.X, p.Y - 4 + i] == col)
                {
                    num++;
                    if (num == 5)
                    {
                        if (num > maxNum)
                        {
                            maxNum = num;
                        }
                        bo = true;
                    }
                    else if (num > 5)
                    {
                        if (num > maxNum)
                        {
                            maxNum = num;
                        }
                        bo = false;
                    }
                }
                else
                    num = 0;
            }

            return bo;
        }

今后会实现“三三自杀” 的算法!

posted @ 2009-12-20 02:49  把爱延续  阅读(804)  评论(0编辑  收藏  举报