2011年12月28日 星期三

C# 圈圈叉叉 二維陣列解法

僅供參考,瞻仰遺容

後面教陣列再回頭體悟

exe檔下載給你家小朋友玩

http://www.megaupload.com/?d=K50EI693

 

 

 

 



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyHomework._1228 {
    public partial class FrmGameV2 : Form {
        public FrmGameV2() {
            InitializeComponent();
            for (int i = 1; i <= 9; i++) {
                Button btn = (Button)Controls.Find("button" + i, true)[0];
                btn.Click += new EventHandler(btn_Click);
                btn.Font = new System.Drawing.Font("微軟正黑體", 16.2F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(136)));
            }
        }

        void btn_Click(object sender, EventArgs e) {
            if (((Button)sender).Text == "") {
                string who = b ? "X" : "O";
                ((Button)sender).Text = who;
                ((Button)sender).ForeColor = b ? Color.Blue : Color.Green;
                if (doCheck(who)) {
                    MessageBox.Show(who + "贏");
                    doClear();
                }
                if (doCheckTie()) {
                    MessageBox.Show("平手");
                    doClear();
                }                
                b = !b;
            }            
        }
        //檢查平平手
        private bool doCheckTie() {
            bool ckSpace = false;
            for (int i = 1; i <= 9; i++) {
                Button btn = (Button)Controls.Find("button" + i, true)[0];
                if (btn.Text == "") ckSpace = true;
            }
            if (ckSpace) {
                return false; //有空字串沒平手
            } else {
                return true; //沒空字串平手
            }
        }

        private bool doCheck(string who) {
            win = false;
            bool[] now = new bool[9];
            for (int i = 1; i <= 9; i++) {
                Button btn = (Button)Controls.Find("button" + i, true)[0];
                if (btn.Text == who) {
                    now[i - 1] = true;
                }
            }
            /*string ttt = "";
            foreach (bool b in now) {
                ttt += b;
            }
            MessageBox.Show(ttt);//偵錯用*/
            for (int i = 0; i <= 7; i++) {
                int count = 0;
                for (int j = 0; j <= 8; j++) {
                    if (now[j] == arrWin[i, j] && now[j]) {
                        count++;
                    }                    
                }
                if (count == 3) {
                    for (int j = 0; j <= 8; j++) {
                        if (arrWin[i, j]) {
                            Button btn = (Button)Controls.Find("button" + (j + 1), true)[0];
                            btn.ForeColor = Color.Wheat;
                            btn.Font = new System.Drawing.Font("微軟正黑體", 20.2F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(136)));
                            btn.BackColor = Color.Red;
                        }
                    }
                    win = true;
                    break;
                }
            }
            if (win) {
                return true;
            } else {
                return false;
            }
        }

        //清空
        private void doClear() {
            for (int i = 1; i <= 9; i++) {
                Button btn = (Button)Controls.Find("button" + i, true)[0];
                btn.Text = "";
                btn.BackColor = SystemColors.Control;
                btn.UseVisualStyleBackColor = true;
            }
        }

        private void button10_Click(object sender, EventArgs e) {
            doClear();
        }

        private bool[,] arrWin = new bool[,] { { true, true, true,false,false,false,false,false,false},
                                               {false,false,false, true, true, true,false,false,false},
                                               {false,false,false,false,false,false, true, true, true},
                                               { true,false,false, true,false,false, true,false,false},
                                               {false, true,false,false, true,false,false, true,false},
                                               {false,false, true,false,false, true,false,false, true},
                                               { true,false,false,false, true,false,false,false, true},
                                               {false,false, true,false, true,false, true,false,false} };
        private bool b; //OX旗標
        private bool win; //是否贏
    }
}


沒有留言:

張貼留言