winform 五子棋 判断输赢

来源:互联网 发布:ant design pro 知乎 编辑:程序博客网 时间:2024/05/01 01:44

新手上路,高手勿进!

利用数组,根据新旧数组值的不同,获取那个点是什么棋子;

说明:

   棋盘:15*15;

     定义4个全局变量:

       string[,] stroldlist = new string[15, 15];//初始的List
        public string[,] strlist = new string[15, 15]; //0 :未下,1:黑子 2:白子

         int icount = 0;//五子连线算赢
          string abc = "";//获取是白子还是黑子,1=黑子  2=白子  0=无子

      自我感觉这种方法好笨,但是实在想不到什么好方法了。

代码如下:

#region 判断输赢

        public void PanDuan() {
            //赋值
            for (int i = 0; i < 225; i++) {
                strlist[i / 15, i % 15] = sandwich[i].btn.Text;
                if (stroldlist[i / 15, i % 15] != strlist[i / 15, i % 15]) {
                    stroldlist[i / 15, i % 15] = strlist[i / 15, i % 15];//把新数组赋值给旧数组
                    icount = i;
                    abc = strlist[i / 15, i % 15];              
                }
            }


            //检查输赢,共有四中情况,横、竖、左斜、右斜
            int ix = icount / 15;//X轴
            int iy = icount % 15;//y轴
          //  PublicClass.ShowMessage(ix+"--------"+iy);
            int count = 0;//五个点相连为赢
            // int kstart = 0;//五子连开始的点
            //四种情况,横、竖、左斜、右斜


            //横(左上角坐标为0,0)       "|"
            int k_shu_min = ix - 4 < 0 ? 0 : ix - 4;
            int k_shu_max = ix + 4 < 15 ? ix + 4 : 14;
            for (int k = k_shu_min; k <= k_shu_max; k++) {
                if (strlist[k, iy] == abc) {
                    count++;
                    if (count >= 5) {
                        if (abc == "1") {
                            PublicClass.ShowMessage("黑子胜!");
                        }
                        if (abc == "2") {
                            PublicClass.ShowMessage("白子胜!");
                        }
                        return;
                    }

                }else {
                    count = 0;
                }

            }
            count = 0;//count值清0


            //竖            "一"
            int k_heng_min = iy - 4 < 0 ? 0 : iy - 4;
            int k_heng_max = iy + 4 < 15 ? iy + 4 : 14;
            for (int k = k_heng_min; k <= k_heng_max; k++) {
                if (strlist[ix, k] == abc) {
                    count++;
                    if (count >= 5) {
                        if (abc == "1") {
                            PublicClass.ShowMessage("黑子胜!");
                        }
                        if (abc == "2") {
                            PublicClass.ShowMessage("白子胜!");
                        }
                        return;
                    }
                }else {
                    count = 0;
                }
            }         
           count = 0;


           //左斜          "/"
           int k_left_min = ix - 4 < 0 ? 0 : ix - 4;
           int k_left_max = ix + 4 < 15 ? ix + 4 : 14;
           for (int k = k_left_min; k <= k_left_max; k++) {
               int ky = 0;
               if (ix + iy > 14) {
                   ky = ix + iy - k >= 14 ? 14 : ix + iy - k;
               } else {
                   ky = ix + iy - k <= 0 ? 0 : ix + iy - k;
               }
               if (strlist[k, ky] == abc) {
                   count++;
                   if (count >= 5) {
                       if (abc == "1") {
                           PublicClass.ShowMessage("黑子胜!");
                       }
                       if (abc == "2") {
                           PublicClass.ShowMessage("白子胜!");
                       }
                       return;
                   }
               }else {
                    count = 0;
                }
           }
           count = 0;


           //右斜           "\"
           int k_right_min = iy - 4 < 0 ? 0 : iy - 4;
           int k_right_max = iy + 4 < 15 ? iy + 4 : 14;
           for (int k = k_right_min; k <= k_right_max; k++) {
               int kx = 0;
               if (ix < iy) {                  
                   kx = ix - iy + k <= 0 ? 0 : ix - iy + k;    
               } else {
                   kx = ix - iy + k >= 14 ? 14 : ix - iy + k;           
               }
               // PublicClass.ShowMessage(kx+"---"+k);
               if (strlist[kx, k] == abc) {
                   count++;
                   if (count >= 5) {
                       if (abc == "1") {
                           PublicClass.ShowMessage("黑子胜!");
                       }
                       if (abc == "2") {
                           PublicClass.ShowMessage("白子胜!");
                       }
                       return;
                   }
               }else {
                    count = 0;
                }
           }
           count = 0;
        }
        #endregion
0 0
原创粉丝点击