2014年12月4日 星期四

物件矩陣與不重複亂數 方法一

public partial class Form1 : Form
    {
        string c;
        int[] a = new int[10];
        Button[,] b = new Button[5, 5];
        int[,] w=new int[5,5];
        Random r1 = new Random();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
         
            for (int j = 0; j < 5; j++)
            {
                for (int i = 0; i < 5; i++)
                {
                    b[i, j] = new System.Windows.Forms.Button();
                    b[i, j].Location = new Point(50 * i, 50 * j);
                    b[i, j].Click += new EventHandler(b_Click);
                    b[i, j].Width = 50;
                    b[i, j].Height = 50;
                    b[i, j].Text = "" + (i + j * 5);
                    w[i, j] = i + j * 5;//w[i,j]可以用來紀錄b[i,j]有關的數值
                    this.Controls.Add(b[i, j]);
                 
                }
            }
            for (int j = 0; j < 25; j++)
            {
                for (int i = 0; i < j; i++)
                {
                    a[0] = r1.Next(0, j);//a[0]從0到j
                    a[1] = a[0] % 5;//a[1],a[2]從0到4
                    a[2] = (a[0] - a[1]) / 5;
                    c = b[a[1], a[2]].Text;
                    b[a[1], a[2]].Text = b[i % 5, i / 5].Text;
                    b[i % 5, i / 5].Text = c;//被隨機到的按鈕與第i個交換名稱

                }
            }
        }
    }
//對於1,2,..,n的數列,被分為未打亂與已打亂,每次從未打亂的數列中隨機挑一個數到已打亂數列裡面,n次之後就會得到隨機不重複數列

2014年11月20日 星期四

賽馬遊戲與亂數


public partial class Form1 : Form
    {
        int i,j=0,k,r;
        public Form1()
        {
            InitializeComponent();
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random random1 = new Random();
            //這邊Random為一種物件類別,宣告random1為此類別
            if (j == 0)
            {
                j = 1;
                i = random1.Next(1, 7);
                label1.Text = i.ToString();
                k = (Form1.ActiveForm.Size.Width - pictureBox1.Left) / 32;
                for (r = 1; r <= i; r++)
                {
                    pictureBox1.Left = pictureBox1.Left + k;
                    Application.DoEvents();
                    Thread.Sleep(100);
                    Application.DoEvents();

                    // Thread.Sleep(100)可讓程式等待0.1秒
                    //Application.DoEvents();可在等待過程中執行其他行程式

                    if (pictureBox1.Left+ pictureBox1.Width >= Form1.ActiveForm.Size.Width) {
                        r = i + 1;
                        j = 2;
                        MessageBox.Show("play1 win");
                    }
                }
             
            }
            else if (j == 1)
            {
                j = 0;
                i = random1.Next(1, 7);
                label2.Text = i.ToString();
                k = (Form1.ActiveForm.Size.Width - pictureBox1.Left) / 32;
                for (r = 1; r <= i; r++)
                {
                    pictureBox2.Left = pictureBox2.Left + k;
                    Application.DoEvents();
                    Thread.Sleep(100);
                    Application.DoEvents();
                    if (pictureBox2.Left +pictureBox2.Width >= Form1.ActiveForm.Size.Width)
                    {
                        r = i + 1;
                        j = 2;
                        MessageBox.Show("play2 win");
                    }
                }
             
            }
        }
-------------------------------------------------------------------------------------
執行畫面

小算盤與副程式

public partial class Form1 : Form
    {
        int a = 0, b = 0,i=4;
        float a1 = 0, b1 = 0, c1 = 1;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text ="+";
            if (a == 0) {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text ="-";
            if (a == 0)
            {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 1;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text ="*";
            if (a == 0)
            {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 2;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox2.Text ="/";
            if (a == 0)
            {
                a = 1;
                textBox2.Visible = true;
                textBox3.Visible = true;
                b = 0;
                button17.Enabled = true;
                c1 = 1;
            }
            i = 3;
        }
        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            a = 0;
            b = 0;
            a1 = 0;
            b1 = 0;
            c1 = 1;
            i = 4;
            textBox2.Visible = false;
            textBox3.Visible = false;
            textBox4.Visible = false;
            button17.Enabled = true;
        }
        private void button6_Click(object sender, EventArgs e)
        {
            add(0);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox4.Visible = true;
            if (i == 0)
            {
                c1 = a1 + b1;
                textBox4.Text = "" + c1;
            }
            if (i == 1)
            {
                c1 = a1 - b1;
                textBox4.Text = "" + c1;
            }
            if (i == 2)
            {
                c1 = a1 * b1;
                textBox4.Text = "" + c1;
            }
            if (i == 3)
            {
                if (b1 != 0)
                {
                    c1 = a1 / b1;
                    textBox4.Text = "" + c1;
                }
                else if (b1 == 0) { textBox4.Text = "error!"; }
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            add(1);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            add(2);
        }

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

        private void button11_Click(object sender, EventArgs e)
        {
            add(4);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            add(5);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            add(6);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            add(7);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            add(8);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            add(9);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            b = 1;
            button17.Enabled = false;
            if (a == 0) { textBox1.Text = textBox1.Text + "."; }
            if (a == 1)
            {
                textBox3.Text = textBox3.Text + ".";
            }
        }
        private void add(int i)//這邊宣告了副程式add(int i)
        {
            if (a == 0)
            {
                textBox1.Text = textBox1.Text +i;
                if (b == 0)
                {
                    a1 = a1 * 10 + i;
                }
                if (b == 1)
                {
                    c1 = c1 / 10;
                    a1 = a1 + i * c1;
                }
            }
            if (a == 1)
            {
                textBox3.Text = textBox3.Text+i ;
                if (b == 0)
                {
                    b1 = b1 * 10 + i;
                }
                if (b == 1)
                {
                    c1 = c1 / 10;
                    b1 = b1 + i * c1;
                }
            }
        }
    }
--------------------------------------------------------------------------------------------

井字遊戲、副程式與物件陣列

public partial class Form1 : Form
    {
        int n = 0, m = 0;
        int i=1, j=1;
        Button[,] buttons=new Button[3,3];//3,3為(0~2,0~2)
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
                for (int j = 0; j < 3; j++)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        buttons[i, j] = new System.Windows.Forms.Button();
                        buttons[i, j].Location = new Point(50 * i, 50 * j);
                        buttons[i, j].Click += new EventHandler(buttons_Click);//宣告點擊事件
                        buttons[i, j].Width = 50;
                        buttons[i, j].Height = 50;
                        this.Controls.Add(buttons[i, j]);//將物件顯示
                    }
                }
        }

        private void buttons_Click(object sender, EventArgs e)//當事件被處發時,處發者為sander
        {

            Button but = sender as Button;//宣告but為跟sander一樣的按鈕
            if (n == 0) { n = 1; but.Text = "O"; }
            else if (n == 1) { n = 0; but.Text = "X"; }
            but.Enabled = false;
            for (m = 0; m <= 2; m++)
            {
                    if (buttons[m, 0].Text == buttons[m, 1].Text && buttons[m, 0].Text == buttons[m, 2].Text && buttons[m, 0].Text != "")
                    {
                        if (n == 1) { MessageBox.Show("O勝利"); } else if (n == 0) { MessageBox.Show("X勝利"); }
                    }
                    if (buttons[0, m].Text == buttons[1, m].Text && buttons[0, m].Text == buttons[2, m].Text && buttons[0, m].Text != "")
                    {
                        if (n == 1) { MessageBox.Show("O勝利"); } else if (n == 0) { MessageBox.Show("X勝利"); }
                    }
            }
            if (buttons[0, 0].Text == buttons[1, 1].Text && buttons[0, 0].Text == buttons[2, 2].Text && buttons[0, 0].Text != "")
            {
                if (n == 1) { MessageBox.Show("O勝利"); } else if (n == 0) { MessageBox.Show("X勝利"); }
            }
            if (buttons[0, 2].Text == buttons[1, 1].Text && buttons[0, 2].Text == buttons[2, 0].Text && buttons[0, 2].Text != "")
            {
                if (n == 1) { MessageBox.Show("O勝利"); } else if (n == 0) { MessageBox.Show("X勝利"); }
            }
        }
    }
--------------------------------------------------------------------