winform實(shí)現(xiàn)五子棋游戲
本文實(shí)例為大家分享了winform實(shí)現(xiàn)五子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下
利用數(shù)組,根據(jù)新舊數(shù)組值的不同,獲取那個(gè)點(diǎn)是什么棋子;
說(shuō)明:
棋盤:15*15;
定義4個(gè)全局變量:
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=無(wú)子
自我感覺(jué)這種方法好笨,但是實(shí)在想不到什么好方法了。
代碼如下:
#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];//把新數(shù)組賦值給舊數(shù)組
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;//五個(gè)點(diǎn)相連為贏
// int kstart = 0;//五子連開始的點(diǎn)
//四種情況,橫、豎、左斜、右斜
//橫(左上角坐標(biāo)為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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
DevExpress之ChartControl創(chuàng)建Drill-Down樣式的Title實(shí)例
這篇文章主要介紹了DevExpress之ChartControl創(chuàng)建Drill-Down樣式的Title實(shí)現(xiàn)方法,以實(shí)例形式講述了創(chuàng)建Drill-Down樣式的Title原理與實(shí)現(xiàn)過(guò)程,需要的朋友可以參考下2014-10-10
C#中計(jì)數(shù)排序算法的原理及實(shí)現(xiàn)
計(jì)數(shù)排序是一種線性時(shí)間復(fù)雜度的排序方法,主要通過(guò)統(tǒng)計(jì)元素出現(xiàn)的次數(shù)實(shí)現(xiàn)排序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-10-10
C# Dictionary和SortedDictionary的簡(jiǎn)介
今天小編就為大家分享一篇關(guān)于C# Dictionary和SortedDictionary的簡(jiǎn)介,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10

