本文实例为大家分享了winform实现五子棋游戏的具体代码,供大家参考,具体内容如下
利用数组,根据新旧数组值的不同,获取那个点是什么棋子;
说明:
棋盘:15*15;
定义4个全局变量:
1
2
3
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=无子
|
自我感觉这种方法好笨,但是实在想不到什么好方法了。
代码如下:
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
|
#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
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/u013816709/article/details/38424905