C#十五子游戏编写代码

时间:2022-04-09 05:22:17

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;
 
namespace windowsformsapplication15
{
 public partial class form1 : form
 {
  public form1()
  {
   initializecomponent();
  }
  const int n = 4;//按钮的行、列数
  button[,] buttons = new button[n, n];//按钮的数组
 
  private void form1_load(object sender, eventargs e)
  {
   //产生所有按钮
   generateallbuttons();
  }
 
  private void button1_click(object sender, eventargs e)
  {
   //点击“开始”按钮,打乱顺序
   shuffle();
  }
 
  //打乱顺序函数
  void shuffle()
  {
   //多次随机交换两个按钮
   random rnd = new random();
   for(int i = 0; i < 100; i++)
   {
    int a = rnd.next(n);
    int b = rnd.next(n);
    int c = rnd.next(n);
    int d = rnd.next(n);
    swap(buttons[a, b], buttons[c, d]);//交换两个按钮位置
   }
  }
 
  //生成所有按钮函数
  void generateallbuttons()
  {
   int x0 = 100, y0 = 10, w = 45, d = 50;
   for (int r = 0; r < n; r++)
   {
    for (int c = 0; c < n; c++)
    {
     int num = r * n + c;
     button btn = new button();
     btn.text = (num + 1).tostring();//设置按钮显示的数字
     btn.top = y0 + r * d;//设置按钮的左边缘与容器的上边缘之间的距离
     btn.left = x0 + c * d;//设置按钮的左边缘与容器的左边缘之间的距离
     btn.width = w;//按钮宽度
     btn.height = w;//按钮高度
     btn.visible = true;//是否显示按钮
     btn.tag = r * n + c;//tag属性是给程序员自己用的,做点标记,类似于按钮的id,此处这个数据用来表示它所在的行列位置
 
     //注册事件
     btn.click += new eventhandler(btn_click);
 
     buttons[r, c] = btn;//放到数组中
     this.controls.add(btn);//加到界面上
    }
   }
   buttons[n - 1, n - 1].visible = false;//定义最后一个按钮不可见
  }
 
  //交换两个按钮函数
  void swap(button btna,button btnb)
  {
   //两个按钮的值交换
   string t = btna.text;
   btna.text = btnb.text;
   btnb.text = t;
 
   //两个按钮的可见属性交换
   bool v = btna.visible;
   btna.visible = btnb.visible;
   btnb.visible = v;
  }
 
  //按钮点击事件处理
  void btn_click(object sender,eventargs e)
  {
   button btn = sender as button;//当前点中的按钮
   button blank = findhiddenbutton();//空白按钮
 
   //判断是否与空白按钮相邻,如果是,则交换
   if (isneighbor(btn,blank))
   {
    swap(btn, blank);
    blank.focus();
   }
 
   //判断是否完成了游戏
   if (resultisok())
   {
    messagebox.show("ok");
   }
  }
 
  //查找要隐藏的按钮函数
  button findhiddenbutton()
  {
   for (int r = 0; r < n; r++)
   {
    for (int c = 0; c < n; c++)
    {
     if (!buttons[r,c].visible)
     {
      return buttons[r, c];
     }
    }
   }
   return null;
  }
 
  //判断是否相邻函数
  bool isneighbor(button btna,button btnb)
  {
   int a = (int)btna.tag;//获取tag中保存的位置信息(0-15的值)
   int b = (int)btnb.tag;
   int r1 = a / n, c1 = a % n;//算出第几行第几列
   int r2 = b / n, c2 = b % n;
   
   //判断左右相邻或者上下相邻
   if ( (r1 == r2 && (c1 == c2 - 1 || c1 == c2 + 1)) || (c1 == c2 && (r1 == r2 - 1 || r1 == r2 + 1)) )
   {
    return true;
   }
   return false;   
  }
 
  //检查是否完成
  bool resultisok()
  {
   for (int r = 0; r < n; r++)
   {
    for (int c = 0; c < n; c++)
    {
     if(buttons[r,c].text != (r * n + c + 1).tostring())
     {
      return false;
     }
    }
   }
   return true;
  }
 
  private void btn_click(object sender, eventargs e)
  {
   throw new notimplementedexception();
  }
 }
}

效果:

C#十五子游戏编写代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。