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
    }
}

沒有留言:

張貼留言