栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

winform实现五子棋游戏

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

winform实现五子棋游戏

本文实例为大家分享了winform实现五子棋游戏的具体代码,供大家参考,具体内容如下

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

说明:

棋盘: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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123005.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号