Re: [問題] 關於button的使用
可以將9個如下命名
[bn1] [bn2] [bn3]
[bn4] [bn5] [bn6]
[bn7] [bn8] [bn9]
然後要判斷成功條件時, 請用以下方式判斷
判斷一
成功條件:如果直排連成一線則勝利
判斷方法:(虛擬CODE)
令bn.id=為Click的Button Id
// 找出直行上一格的 button Id
int bn_1st_Id = Convert.ToInt(bn.id) - 3;
if(bn_1st_Id<1) { // 如果本身沒有上一格, 則找下下一格
bn_1st_Id+=9;
}
// 找出直行的下一格 button Id
int bn_2nd_Id = Convert.ToInt(bn.id) + 3;
if(bn_2nd_Id>9) { // 如果本身沒有下一格,則找上上一格
bn_2nd_Id-=9;
}
string bnSelfFlag=bn.Text;
string bn1stFlag=(Button)FindContorl("bn"+bn_1st_Id.toString()).Text;
string bn2ndFlag=(Button)FindContorl("bn"+bn_2nd_Id.toString()).Text;
if(bnSelfFlag==bn1stFlag==bn2ndFlag)
{
Show(bn.Text + " Win!!");
}
判斷二:
成功條件:橫排連成一線則勝利
判斷方法:
// 找出橫排左一格的 button Id
int bn_Left_Id = Convert.ToInt(bn.id);
if((bn_Left_Id % 3)==1) { // 如果本身沒有前一格, 則找後後一格
bn_Left_Id+=2;
}
else {
bn_Left_Id -= 1;
}
// 找出橫排右一格的 button Id
int bn_Right_Id = Convert.ToInt(bn.id);
if((bn_Right_Id%3)==0) { // 如果本身沒有後一格,則找前前一格
bn_Right_Id-=2;
}
else {
bn_Right_Id += 1;
}
string bnSelfFlag=bn.Text;
string bnLeftFlag=(Button)FindContorl("bn"+bn_Left_Id.toString()).Text;
string bnRightFlag=(Button)FindContorl("bn"+bn_Right_Id.toString()).Text;
if(bnSelfFlag==bnLeftFlag==bnRightFlag)
{
Show(bn.Text + " Win!!");
}
判斷三:
成功條件:如果斜角有連成線則勝利
判斷方法:
// 判斷按鈕是否在對角線上(數字為單數即是)
if((Convert.ToInt(bn.id) % 2)==1) {
if(Convert.ToInt(bn.id)==5) { // 如果下在正中
if(bn1.Text==bn5.Text==bn9.Text) {
Show(bn.Text + " Win!!");
}
else if(bn3.Text==bn5.Text==bn7.Text) {
Show(bn.Text + " Win!!");
}
}
if((Convert.ToInt(bn.id) % 4)==1) { //左上右下
if(bn1.Text==bn5.Text==bn9.Text) {
Show(bn.Text + " Win!!");
}
}
else { //右上左下
if(bn3.Text==bn5.Text==bn7.Text) {
Show(bn.Text + " Win!!");
}
}
}
大致上邏輯這樣寫囉..
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.75.244.133
※ 文章網址: http://www.ptt.cc/bbs/C_Sharp/M.1412155464.A.5A3.html
推
10/01 21:16, , 1F
10/01 21:16, 1F
→
10/01 21:16, , 2F
10/01 21:16, 2F
→
10/01 21:16, , 3F
10/01 21:16, 3F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
3
17
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章