2011年12月29日 星期四

C# 圈圈叉叉 演算法升級

演算法升級

玩到天荒地老

 

 

 

 



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._1229 {
    public partial class FrmGameV3 : Form {
        int allIndex;
        int btnSize;
        int btnSpacing;
        public FrmGameV3() {
            InitializeComponent();

            btnSize = int.Parse(textBox1.Text);
            btnSpacing = int.Parse(textBox2.Text);
            allIndex = int.Parse(textBox3.Text) - 1;

            for (int i = 0; i <= allIndex; i++) {
                for (int j = 0; j <= allIndex; j++) {
                    Button btn = new Button();
                    btn.Click += new EventHandler(btn_Click);
                    btn.Font = new System.Drawing.Font("微軟正黑體", 16.2F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(136)));
                    btn.Location = new System.Drawing.Point(20 + (j * (btnSize + btnSpacing)), 80 + (i * (btnSize + btnSpacing)));
                    btn.Name = "btn" + i + j;
                    btn.Size = new System.Drawing.Size(btnSize, btnSize);
                    btn.TabIndex = int.Parse((i + "" + j));
                    btn.UseVisualStyleBackColor = true;
                    this.Controls.Add(btn);
                }                
            }
        }

        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;

                int y = int.Parse(((Button)sender).Name.Substring(3, 1));
                int x = int.Parse(((Button)sender).Name.Substring(4, 1));
                //MessageBox.Show(x + "" + y);
                win = false;                
                for (int part = 1; part <= 4; part++) {
                    int count = 0;
                    for (int i = 0; i <= allIndex; i++) {
                        Button btn;
                        switch (part) {
                            case 1:
                                btn = (Button)Controls.Find("btn" + y + i, true)[0];
                                break;
                            case 2:
                                btn = (Button)Controls.Find("btn" + i + x, true)[0];
                                break;
                            case 3:
                                btn = (Button)Controls.Find("btn" + i + i, true)[0];
                                break;
                            case 4:
                                btn = (Button)Controls.Find("btn" + i + (allIndex - i), true)[0];
                                break;
                            default:
                                btn = null;
                                break;
                        }
                        if (btn.Text == who && btn != null) count++;
                    }
                    if (count == (allIndex + 1)) {
                        win = true;
                        break;
                    }
                }
                if (win) {
                    MessageBox.Show(who + "贏");
                    doClear();
                }
                if (doCheckTie()) {
                    MessageBox.Show("平手");
                    doClear();
                }
                b = !b;
            }            
        }

        //檢查平平手
        private bool doCheckTie() {
            bool ckSpace = false;
            for (int i = 0; i <= allIndex; i++) {
                for (int j = 0; j <= allIndex; j++) {
                    Button btn = (Button)Controls.Find("btn" + i + j, true)[0];
                    if (btn.Text == "") ckSpace = true;
                }
            }
            if (ckSpace) {
                return false; //有空字串沒平手
            } else {
                return true; //沒空字串平手
            }
        }
        //美送勝阿
        private void buttonclear_Click(object sender, EventArgs e) {
            doClear();
        }
        //清空
        private void doClear() {

            for (int i = 0; i <= allIndex; i++) {
                for (int j = 0; j <= allIndex; j++) {
                    Button btn = (Button)Controls.Find("btn" + i + j, true)[0];
                    btn.Text = "";
                    btn.BackColor = SystemColors.Control;
                    btn.UseVisualStyleBackColor = true;
                }
            }
        }        
        private bool b; //OX旗標
        private bool win;//是否贏

        private void button1_Click(object sender, EventArgs e) {
            for (int i = 0; i <= allIndex; i++) {
                for (int j = 0; j <= allIndex; j++) {
                    Button btn = (Button)Controls.Find("btn" + i + j, true)[0];
                    this.Controls.Remove(btn);
                }
            }

            btnSize = int.Parse(textBox1.Text);
            btnSpacing = int.Parse(textBox2.Text);
            allIndex = int.Parse(textBox3.Text) - 1;

            for (int i = 0; i <= allIndex; i++) {
                for (int j = 0; j <= allIndex; j++) {
                    Button btn = new Button();
                    btn.Click += new EventHandler(btn_Click);
                    btn.Font = new System.Drawing.Font("微軟正黑體", 16.2F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(136)));
                    btn.Location = new System.Drawing.Point(20 + (j * (btnSize + btnSpacing)), 80 + (i * (btnSize + btnSpacing)));
                    btn.Name = "btn" + i + j;
                    btn.Size = new System.Drawing.Size(btnSize, btnSize);
                    btn.TabIndex = int.Parse((i + "" + j));
                    btn.UseVisualStyleBackColor = true;
                    this.Controls.Add(btn);
                }
            }
        }
    }
}






2013/1/8 彩色版



using System;
using System.Drawing;
using System.Windows.Forms;

namespace TicTacChess
{
    public partial class Form1 : Form
    {
        #region 變數

        private enum LineWay { , , 正斜, 反斜 }

        private int line;
        private int btnSize;
        private int btnSpacing;
        private int allCells;

        private int steps = 0;
        private bool changeSideFlag;

        #endregion 變數

        #region 事件

        public Form1()
        {
            InitializeComponent();
            setUp();
        }

        private void SetUp_btn_Click(object sender, EventArgs e)
        {
            Area_pan.Controls.Clear();
            setUp();
        }

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

        private void btn_Click(object sender, EventArgs e)
        {
            Button thisBtn = (Button)sender;
            if (!string.IsNullOrEmpty(thisBtn.Text)) return;

            string who = changeSideFlag ? "X" : "O";
            thisBtn.Text = who;
            thisBtn.ForeColor = changeSideFlag ? Color.Blue : Color.Green;

            int x = int.Parse(thisBtn.Name.Substring(3, 1));
            int y = int.Parse(thisBtn.Name.Substring(4, 1));

            for (int part = 0; part < 4; part++)
            {
                int count = 0;
                for (int i = 0; i < line; i++)
                {
                    string name = string.Empty;
                    switch (part)
                    {
                        case (int)LineWay.:
                            name = "btn" + x + i;//X固定
                            break;

                        case (int)LineWay.:
                            name = "btn" + i + y;//Y固定
                            break;

                        case (int)LineWay.反斜:
                            name = "btn" + i + i;//+X+Y
                            break;

                        case (int)LineWay.正斜:
                            name = "btn" + i + (line - 1 - i);//+X-Y
                            break;

                        default:
                            break;
                    }
                    if (!string.IsNullOrEmpty(name))
                    {
                        Button btn = (Button)Controls.Find(name, true)[0];
                        if (btn.Text == who) count++;
                    }
                }
                if (count == line)
                {
                    MessageBox.Show(who + "");
                    doClear();
                    break;
                }
            }
            if (steps == allCells)
            {
                MessageBox.Show("平手");
                doClear();
            }
            changeSideFlag = !changeSideFlag;
            steps++;
        }

        #endregion 事件

        #region Sub

        private void setUp()
        {
            btnSize = int.Parse(Size_tb.Text);
            btnSpacing = int.Parse(Spacing_tb.Text);
            line = int.Parse(Line_tb.Text);
            allCells = line * line;

            for (int i = 0; i < line; i++)
            {
                for (int j = 0; j < line; j++)
                {
                    Button btn = new Button();
                    btn.Click += new EventHandler(btn_Click);
                    btn.Font = new System.Drawing.Font("微軟正黑體", 16.2F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(136)));
                    btn.Location = new System.Drawing.Point(20 + (j * (btnSize + btnSpacing)), 20 + (i * (btnSize + btnSpacing)));
                    btn.Name = "btn" + i + j;
                    btn.Size = new System.Drawing.Size(btnSize, btnSize);
                    btn.TabIndex = int.Parse((i + "" + j));
                    btn.UseVisualStyleBackColor = true;
                    Area_pan.Controls.Add(btn);
                }
            }
        }

        private void doClear()
        {
            foreach (Button btn in Area_pan.Controls)
            {
                btn.Text = string.Empty;
                btn.BackColor = SystemColors.Control;
                btn.UseVisualStyleBackColor = true;
            }
            steps = 0;
        }

        #endregion Sub
    }
}

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; //是否贏
    }
}