2010年11月12日 星期五

計算機

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int cal, temp, logic;
        Boolean a = false;
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (a == false)
            {
                textBox1.Text += 1;
            }
            else
            {
                textBox1.Text = "1";
                a = false;
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (a == false)
            {
                textBox1.Text += 2;
            }
            else
            {
                textBox1.Text = "2";
                a = false;
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (a == false)
            {
                textBox1.Text += 3;
            }
            else
            {
                textBox1.Text = "3";
                a = false;
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (a == false)
            {
                textBox1.Text += 4;
            }
            else
            {
                textBox1.Text = "4";
                a = false;
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (a == false)
            {
                textBox1.Text += 5;
            }
            else
            {
                textBox1.Text = "5";
                a = false;
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            if (a == false)
            {
                textBox1.Text += 6;
            }
            else
            {
                textBox1.Text = "6";
                a = false;
            }
        }
        private void button7_Click(object sender, EventArgs e)
        {
            if (a == false)
            {
                textBox1.Text += 7;
            }
            else
            {
                textBox1.Text = "7";
                a = false;
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            if (a == false)
            {
                textBox1.Text += 8;
            }
            else
            {
                textBox1.Text = "8";
                a = false;
            }
        }
        private void button9_Click(object sender, EventArgs e)
        {
            if (a == false)
            {
                textBox1.Text += 9;
            }
            else
            {
                textBox1.Text = "9";
                a = false;
            }
        }
        private void button10_Click(object sender, EventArgs e)
        {
            if (a == false)
            {
                textBox1.Text += 0;
            }
            else
            {
                textBox1.Text = "0";
                a = false;
            }
        }
        private void button11_Click(object sender, EventArgs e)
        {
            temp = int.Parse(textBox1.Text);
            logic = 1;
            a = true;
        }
        private void button12_Click(object sender, EventArgs e)
        {
            temp = int.Parse(textBox1.Text);
            logic = 2;
            a = true;
        }
        private void button13_Click(object sender, EventArgs e)
        {
            temp = int.Parse(textBox1.Text);
            logic = 3;
            a = true;
        }
        private void button14_Click(object sender, EventArgs e)
        {
            temp = int.Parse(textBox1.Text);
            logic = 4;
            a = true;
        }
        private void button15_Click(object sender, EventArgs e)
        {
            switch (logic)
            {
                case 1:
                    cal = temp + int.Parse(textBox1.Text);
                    textBox1.Text = cal.ToString();
                    break;
                case 2:
                    cal = temp - int.Parse(textBox1.Text);
                    textBox1.Text = cal.ToString();
                    break;
                case 3:
                    cal = temp * int.Parse(textBox1.Text);
                    textBox1.Text = cal.ToString();
                    break;
                case 4:
                    if (int.Parse(textBox1.Text) != 0)
                    {
                        cal = temp / int.Parse(textBox1.Text);
                        textBox1.Text = cal.ToString();
                    }
                    else
                    {
                        MessageBox.Show("除數不得為零!");
                    }
                    break;
            }
            a = true;
        }
    }
}

益智遊戲+隨機亂數1

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        System.Windows.Forms.Button[] Buttons;
        System.Windows.Forms.TextBox[] TextBoxs;
        int[,] a = new int[4, 4];
        int[] arr = new int[9];
        int[] score = new int[9];
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Buttons = new System.Windows.Forms.Button[9];
            TextBoxs = new System.Windows.Forms.TextBox[9];
            for (int i = 0; i < 9; ++i)
            {
                Buttons[i] = new Button();
                Buttons[i].Click += new System.EventHandler(Buttons_Click);
                this.Controls.Add(Buttons[i]);
                if (i <= 2)
                    Buttons[i].Location = new System.Drawing.Point(10 + i * 80, 10 + 100);
                else if (i > 2 && i <= 5)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 3) * 80, 40 + 100);
                else if (i > 5 && i <= 9)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 6) * 80, 70 + 100);
            }
            for (int i = 0; i < 9; ++i)
            {
                TextBoxs[i] = new TextBox();
                this.Controls.Add(TextBoxs[i]);
                if (i <= 2)
                    TextBoxs[i].Location = new System.Drawing.Point(10 + i * 100, 10);
                else if (i > 2 && i <= 5)
                    TextBoxs[i].Location = new System.Drawing.Point(10 + (i - 3) * 100, 40);
                else if (i > 5 && i <= 9)
                    TextBoxs[i].Location = new System.Drawing.Point(10 + (i - 6) * 100, 70);
            }
            TextBoxs[0].Text = "7";
            TextBoxs[1].Text = "2";
            TextBoxs[2].Text = "4";
            TextBoxs[3].Text = "5";
            TextBoxs[4].Text = "0";
            TextBoxs[5].Text = "6";
            TextBoxs[6].Text = "8";
            TextBoxs[7].Text = "3";
            TextBoxs[8].Text = "1";
        }
        public void Buttons_Click(object sender, EventArgs e)
        {
            // System.Windows.Forms.MessageBox.Show("You have clicked button " +
            //   ((System.Windows.Forms.Button)sender).Tag.ToString());
            String tnum = sender.ToString();
            int tlen = tnum.Length;
            String no = tnum.Substring(tlen - 1);
            int n = int.Parse(no);
            for (int i = 0; i < 9; i++)
            {
                if (Buttons[i].Text == no)
                    n = i;
            }
            switch (n)
            {
                case 0:
                    if (Buttons[1].Text == "0")
                    {
                        string temp;
                        temp = Buttons[1].Text;
                        Buttons[1].Text = Buttons[0].Text;
                        Buttons[0].Text = temp;
                    }
                    if (Buttons[3].Text == "0")
                    {
                        string temp;
                        temp = Buttons[3].Text;
                        Buttons[3].Text = Buttons[0].Text;
                        Buttons[0].Text = temp;
                    }

                    break;
                case 1:
                    if (Buttons[0].Text == "0")
                    {
                        string temp;
                        temp = Buttons[0].Text;
                        Buttons[0].Text = Buttons[1].Text;
                        Buttons[1].Text = temp;
                    }
                    if (Buttons[4].Text == "0")
                    {
                        string temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[1].Text;
                        Buttons[1].Text = temp;
                    }
                    if (Buttons[2].Text == "0")
                    {
                        string temp;
                        temp = Buttons[2].Text;
                        Buttons[2].Text = Buttons[1].Text;
                        Buttons[1].Text = temp;
                    }
                    break;
                case 2:
                    if (Buttons[1].Text == "0")
                    {
                        string temp;
                        temp = Buttons[1].Text;
                        Buttons[1].Text = Buttons[2].Text;
                        Buttons[2].Text = temp;
                    }
                    if (Buttons[5].Text == "0")
                    {
                        string temp;
                        temp = Buttons[5].Text;
                        Buttons[5].Text = Buttons[2].Text;
                        Buttons[2].Text = temp;
                    }

                    break;
                case 3:
                    if (Buttons[0].Text == "0")
                    {
                        string temp;
                        temp = Buttons[0].Text;
                        Buttons[0].Text = Buttons[3].Text;
                        Buttons[3].Text = temp;
                    }
                    if (Buttons[4].Text == "0")
                    {
                        string temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[3].Text;
                        Buttons[3].Text = temp;
                    }
                    if (Buttons[6].Text == "0")
                    {
                        string temp;
                        temp = Buttons[6].Text;
                        Buttons[6].Text = Buttons[3].Text;
                        Buttons[3].Text = temp;
                    }
                    break;
                case 4:
                    if (Buttons[1].Text == "0")
                    {
                        string temp;
                        temp = Buttons[1].Text;
                        Buttons[1].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    if (Buttons[3].Text == "0")
                    {
                        string temp;
                        temp = Buttons[3].Text;
                        Buttons[3].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    if (Buttons[5].Text == "0")
                    {
                        string temp;
                        temp = Buttons[5].Text;
                        Buttons[5].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    if (Buttons[7].Text == "0")
                    {
                        string temp;
                        temp = Buttons[7].Text;
                        Buttons[7].Text = Buttons[4].Text;
                        Buttons[4].Text = temp;
                    }
                    break;
                case 5:
                    if (Buttons[2].Text == "0")
                    {
                        string temp;
                        temp = Buttons[2].Text;
                        Buttons[2].Text = Buttons[5].Text;
                        Buttons[5].Text = temp;
                    }
                    if (Buttons[4].Text == "0")
                    {
                        string temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[5].Text;
                        Buttons[5].Text = temp;
                    }
                    if (Buttons[8].Text == "0")
                    {
                        string temp;
                        temp = Buttons[8].Text;
                        Buttons[8].Text = Buttons[5].Text;
                        Buttons[5].Text = temp;
                    }
                    break;
                case 6:
                    if (Buttons[3].Text == "0")
                    {
                        string temp;
                        temp = Buttons[3].Text;
                        Buttons[3].Text = Buttons[6].Text;
                        Buttons[6].Text = temp;
                    }
                    if (Buttons[7].Text == "0")
                    {
                        string temp;
                        temp = Buttons[7].Text;
                        Buttons[7].Text = Buttons[6].Text;
                        Buttons[6].Text = temp;
                    }
                    break;
                case 7:
                    if (Buttons[4].Text == "0")
                    {
                        string temp;
                        temp = Buttons[4].Text;
                        Buttons[4].Text = Buttons[7].Text;
                        Buttons[7].Text = temp;
                    }
                    if (Buttons[6].Text == "0")
                    {
                        string temp;
                        temp = Buttons[6].Text;
                        Buttons[6].Text = Buttons[7].Text;
                        Buttons[7].Text = temp;
                    }
                    if (Buttons[8].Text == "0")
                    {
                        string temp;
                        temp = Buttons[8].Text;
                        Buttons[8].Text = Buttons[7].Text;
                        Buttons[7].Text = temp;
                    }
                    break;
                case 8:
                    if (Buttons[5].Text == "0")
                    {
                        string temp;
                        temp = Buttons[5].Text;
                        Buttons[5].Text = Buttons[8].Text;
                        Buttons[8].Text = temp;
                    }
                    if (Buttons[7].Text == "0")
                    {
                        string temp;
                        temp = Buttons[7].Text;
                        Buttons[7].Text = Buttons[8].Text;
                        Buttons[8].Text = temp;
                    }
                    break;
            }
        }
      
        private void button1_Click_1(object sender, EventArgs e)
        {
            //讀入  input
            a[1, 1] = Convert.ToInt16(TextBoxs[0].Text);
            a[1, 2] = Convert.ToInt16(TextBoxs[1].Text);
            a[1, 3] = Convert.ToInt16(TextBoxs[2].Text);
            a[2, 1] = Convert.ToInt16(TextBoxs[3].Text);
            a[2, 2] = Convert.ToInt16(TextBoxs[4].Text);
            a[2, 3] = Convert.ToInt16(TextBoxs[5].Text);
            a[3, 1] = Convert.ToInt16(TextBoxs[6].Text);
            a[3, 2] = Convert.ToInt16(TextBoxs[7].Text);
            a[3, 3] = Convert.ToInt16(TextBoxs[8].Text);
            //輸出  output
            for (int i = 0; i < 9; i++)
            {
                if (i < 3)
                {
                    Buttons[i].Text = Convert.ToString(a[1, i + 1]);
                }
                else if (i >= 3 && i < 6)
                {
                    Buttons[i].Text = Convert.ToString(a[2, (i - 3) + 1]);
                }
                else
                {
                    Buttons[i].Text = Convert.ToString(a[3, (i - 3 * 2) + 1]);
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //讀入  input
            Random rd = new Random();
            int temp ;
            int rdno ;
          

            for (int i = 8; i >= 0; --i)
            {
                rdno = rd.Next(0, i);
                temp = score[rdno];
                score[rdno] = score[i];
                score[i] = temp;
            }
            a[1, 1] = Convert.ToInt16(Buttons[0].Text);
            a[1, 2] = Convert.ToInt16(Buttons[1].Text);
            a[1, 3] = Convert.ToInt16(Buttons[2].Text);
            a[2, 1] = Convert.ToInt16(Buttons[3].Text);
            a[2, 2] = Convert.ToInt16(Buttons[4].Text);
            a[2, 3] = Convert.ToInt16(Buttons[5].Text);
            a[3, 1] = Convert.ToInt16(Buttons[6].Text);
            a[3, 2] = Convert.ToInt16(Buttons[7].Text);
            a[3, 3] = Convert.ToInt16(Buttons[8].Text);
            //輸出  output
            for (int i = 0; i < 9; ++i)
            {
                Buttons[i].Text = Convert.ToString(score[i]);
               score[i] = i;
            }  
        }
    }
}

2010年11月5日 星期五

井字遊戲 未完成



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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        System.Windows.Forms.Button[] Buttons;
        int temp;
       
        int nobtpressed=0;
       int [] l=new int[9]{0,1,2,3,4,5,6,7,8};
      
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Buttons = new System.Windows.Forms.Button[9];
            for (int i = 0; i < 9; ++i)
            {
                Buttons[i] = new Button();
                Buttons[i].Size = new Size(50, 50);
                Buttons[i].Click += new System.EventHandler(Buttons_Click);
                this.Controls.Add(Buttons[i]);
                if (i <= 2)
                    Buttons[i].Location = new System.Drawing.Point(10 + i * 80, 0 + 10);
                else if (i > 2 && i <= 5)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 3) * 80, 50 + 10);
                else if (i > 5 && i <= 9)
                    Buttons[i].Location = new System.Drawing.Point(10 + (i - 6) * 80, 100 + 10);
            }
            Buttons[0].Text = "0";
            Buttons[1].Text = "1";
            Buttons[2].Text = "2";
            Buttons[3].Text = "3";
            Buttons[4].Text = "4";
            Buttons[5].Text = "5";
            Buttons[6].Text = "6";
            Buttons[7].Text = "7";
            Buttons[8].Text = "8";
            }
       
        public void Buttons_Click(object sender, EventArgs e)
        {
            // System.Windows.Forms.MessageBox.Show("You have clicked button " +
            //   ((System.Windows.Forms.Button)sender).Tag.ToString());
            String tnum = sender.ToString();
            int tlen = tnum.Length;
            String no = tnum.Substring(tlen - 1);
            int n = int.Parse(no);
            for (int i = 0; i < 9; i++)
            {
                if (Buttons[i].Text == no)
                    n = i;
            }
            switch (n)
            {
                case 0:
                    Buttons[0].Image = pictureBox1.Image;
                    nobtpressed=nobtpressed+1;
                    label2.Text=""+nobtpressed;
                    temp=l[0];
                    l[0]=l[9-nobtpressed];
                    l[9-nobtpressed]=temp;
                    button1_Click_1(sender, e);

                    break;
                case 1:
                    Buttons[1].Image = pictureBox1.Image;
                    nobtpressed=nobtpressed+1;
                    label2.Text=""+nobtpressed;
                    temp=l[1];
                    l[1]=l[9-nobtpressed];
                    l[9-nobtpressed]=temp;
                    button1_Click_1(sender, e);
                    break;
                case 2:
                    Buttons[2].Image = pictureBox1.Image;
                    nobtpressed=nobtpressed+1;
                    label2.Text=""+nobtpressed;
                    temp=l[2];
                    l[2]=l[9-nobtpressed];
                    l[9-nobtpressed]=temp;
                    button1_Click_1(sender, e);
                    break;
                case 3:
                    Buttons[3].Image = pictureBox1.Image;
                    nobtpressed=nobtpressed+1;
                    label2.Text=""+nobtpressed;
                    temp=l[3];
                    l[3]=l[9-nobtpressed];
                    l[9-nobtpressed]=temp;
                    button1_Click_1(sender, e);
                    break;
                case 4:
                    Buttons[4].Image = pictureBox1.Image;
                    nobtpressed=nobtpressed+1;
                    label2.Text=""+nobtpressed;
                    temp=l[4];
                    l[4]=l[9-nobtpressed];
                    l[9-nobtpressed]=temp;
                    button1_Click_1(sender, e);
                    break;
                case 5:
                    Buttons[5].Image = pictureBox1.Image;
                    nobtpressed=nobtpressed+1;
                    label2.Text=""+nobtpressed;
                    temp=l[5];
                    l[5]=l[9-nobtpressed];
                    l[9-nobtpressed]=temp;
                    button1_Click_1(sender, e);
                    break;
                case 6:
                    Buttons[6].Image = pictureBox1.Image;
                    nobtpressed=nobtpressed+1;
                    label2.Text=""+nobtpressed;
                    temp=l[6];
                    l[6]=l[9-nobtpressed];
                    l[9-nobtpressed]=temp;
                    button1_Click_1(sender, e);
                    break;
                case 7:
                    Buttons[7].Image = pictureBox1.Image;
                    nobtpressed=nobtpressed+1;
                    label2.Text=""+nobtpressed;
                    temp=l[7];
                    l[7]=l[9-nobtpressed];
                    l[9-nobtpressed]=temp;
                    button1_Click_1(sender, e);
                    break;
                case 8:
                    Buttons[8].Image = pictureBox1.Image;
                    nobtpressed=nobtpressed+1;
                    label2.Text=""+nobtpressed;
                    temp=l[8];
                    l[8]=l[9-nobtpressed];
                    l[9-nobtpressed]=temp;
                    button1_Click_1(sender, e);
                    break;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            nobtpressed = nobtpressed + 1;
            label2.Text = "" + nobtpressed;
            int temp = l[8];
            l[8] = l[9 - nobtpressed];
            l[9 - nobtpressed] = temp;
            Random rd = new Random();
            int rdno = rd.Next(9 - nobtpressed);
            label1.Text = "" + l[rdno];
            Buttons[l[rdno]].Image = pictureBox2.Image;
        }
    }
}