2010年11月12日 星期五

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
    {
        System.Windows.Forms.Button[] Button1;
        int[] recordbutn = new int[9] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
        int[] butnpressed = new int[9] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
        int[] arr = new int[9];
        int[,] a = new int[4, 4];
        int bno;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Button1 = new System.Windows.Forms.Button[9];
            for (int i = 0; i < 9; ++i)
            {
                Button1[i] = new Button();
                Button1[i].Click += new System.EventHandler(Button1_Click);
                this.Controls.Add(Button1[i]);
                if (i <= 2)
                    Button1[i].Location = new System.Drawing.Point(10 + i * 80, 10 + 100);
                else if (i > 2 && i <= 5)
                    Button1[i].Location = new System.Drawing.Point(10 + (i - 3) * 80, 40 + 100);
                else if (i > 5 && i <= 9)
                    Button1[i].Location = new System.Drawing.Point(10 + (i - 6) * 80, 70 + 100);
                Button1[i].Text = "" + i;

            }
          
        }
        public void Button1_Click(object sender, EventArgs e)
        {
            // System.Windows.Forms.MessageBox.Show("You have clicked button " +
            //   ((System.Windows.Forms.Button)sender).Tag.ToString());
            //MessageBox.Show(Convert.ToString(((Button)sender).Text));
            String tnum = sender.ToString();
            int tlen = tnum.Length;
            String no = tnum.Substring(tlen - 1);
            for (int i = 0; i < 9; i++)
            {
                if (Button1[i].Text == no)
                    // MessageBox.Show("" + no);
                    bno = i;
            }
            // MessageBox.Show(""+bno);
            switch (bno)
            {
                case 0:
                    // MessageBox.Show("0");
                    if (Button1[1].Text == "0")
                    {
                        String temp;
                        temp = Button1[1].Text;
                        Button1[1].Text = Button1[0].Text;
                        Button1[0].Text = temp;
                    }
                    else if (Button1[3].Text == "0")
                    {
                        String temp;
                        temp = Button1[3].Text;
                        Button1[3].Text = Button1[0].Text;
                        Button1[0].Text = temp;
                    }
                    break;
                case 1:
                    // MessageBox.Show("1");
                    if (Button1[4].Text == "0")
                    {
                        String temp;
                        temp = Button1[4].Text;
                        Button1[4].Text = Button1[1].Text;
                        Button1[1].Text = temp;
                    }
                    else if (Button1[0].Text == "0")
                    {
                        String temp;
                        temp = Button1[0].Text;
                        Button1[0].Text = Button1[1].Text;
                        Button1[1].Text = temp;
                    }
                    else if (Button1[2].Text == "0")
                    {
                        String temp;
                        temp = Button1[2].Text;
                        Button1[2].Text = Button1[1].Text;
                        Button1[1].Text = temp;
                    }
                    break;
                case 2:
                    // MessageBox.Show("2");
                    if (Button1[1].Text == "0")
                    {
                        String temp;
                        temp = Button1[1].Text;
                        Button1[1].Text = Button1[2].Text;
                        Button1[2].Text = temp;
                    }
                    else if (Button1[5].Text == "0")
                    {
                        String temp;
                        temp = Button1[5].Text;
                        Button1[5].Text = Button1[2].Text;
                        Button1[2].Text = temp;
                    }
                    break;
                case 3:
                    //MessageBox.Show("3");
                    if (Button1[0].Text == "0")
                    {
                        String temp;
                        temp = Button1[0].Text;
                        Button1[0].Text = Button1[3].Text;
                        Button1[3].Text = temp;
                    }
                    else if (Button1[4].Text == "0")
                    {
                        String temp;
                        temp = Button1[4].Text;
                        Button1[4].Text = Button1[3].Text;
                        Button1[3].Text = temp;
                    }
                    else if (Button1[6].Text == "0")
                    {
                        String temp;
                        temp = Button1[6].Text;
                        Button1[6].Text = Button1[3].Text;
                        Button1[3].Text = temp;
                    }
                    break;
                case 4:
                    //MessageBox.Show("4");
                    if (Button1[1].Text == "0")
                    {
                        String temp;
                        temp = Button1[4].Text;
                        Button1[4].Text = Button1[1].Text;
                        Button1[1].Text = temp;
                    }
                    else if (Button1[3].Text == "0")
                    {
                        String temp;
                        temp = Button1[3].Text;
                        Button1[3].Text = Button1[4].Text;
                        Button1[4].Text = temp;
                    }
                    else if (Button1[5].Text == "0")
                    {
                        String temp;
                        temp = Button1[5].Text;
                        Button1[5].Text = Button1[4].Text;
                        Button1[4].Text = temp;
                    }
                    else if (Button1[7].Text == "0")
                    {
                        String temp;
                        temp = Button1[7].Text;
                        Button1[7].Text = Button1[4].Text;
                        Button1[4].Text = temp;
                    }
                    break;
                case 5:
                    // MessageBox.Show("5");
                    if (Button1[2].Text == "0")
                    {
                        String temp;
                        temp = Button1[2].Text;
                        Button1[2].Text = Button1[5].Text;
                        Button1[5].Text = temp;
                    }
                    else if (Button1[4].Text == "0")
                    {
                        String temp;
                        temp = Button1[4].Text;
                        Button1[4].Text = Button1[5].Text;
                        Button1[5].Text = temp;
                    }
                    else if (Button1[8].Text == "0")
                    {
                        String temp;
                        temp = Button1[8].Text;
                        Button1[8].Text = Button1[5].Text;
                        Button1[5].Text = temp;
                    }
                    break;
                case 6:
                    //  MessageBox.Show("6");
                    if (Button1[3].Text == "0")
                    {
                        String temp;
                        temp = Button1[3].Text;
                        Button1[3].Text = Button1[6].Text;
                        Button1[6].Text = temp;
                    }
                    else if (Button1[7].Text == "0")
                    {
                        String temp;
                        temp = Button1[7].Text;
                        Button1[7].Text = Button1[6].Text;
                        Button1[6].Text = temp;
                    }
                    break;
                case 7:
                    // MessageBox.Show("7");
                    if (Button1[4].Text == "0")
                    {
                        String temp;
                        temp = Button1[4].Text;
                        Button1[4].Text = Button1[7].Text;
                        Button1[7].Text = temp;
                    }
                    else if (Button1[6].Text == "0")
                    {
                        String temp;
                        temp = Button1[6].Text;
                        Button1[6].Text = Button1[7].Text;
                        Button1[7].Text = temp;
                    }
                    else if (Button1[8].Text == "0")
                    {
                        String temp;
                        temp = Button1[8].Text;
                        Button1[8].Text = Button1[7].Text;
                        Button1[7].Text = temp;
                    }
                    break;
                case 8:
                    //  MessageBox.Show("8");
                    if (Button1[5].Text == "0")
                    {
                        String temp;
                        temp = Button1[5].Text;
                        Button1[5].Text = Button1[8].Text;
                        Button1[8].Text = temp;
                    }
                    else if (Button1[7].Text == "0")
                    {
                        String temp;
                        temp = Button1[7].Text;
                        Button1[7].Text = Button1[8].Text;
                        Button1[8].Text = temp;
                    }
                    break;
            }

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            {
                int temp=0;
                arr[0] = 0;
                arr[1] = 1;
                arr[2] = 2;
                arr[3] = 3;
                arr[4] = 4;
                arr[5] = 5;
                arr[6] = 6;
                arr[7] = 7;
                arr[8] = 8;
           
              
            for (int i = 0; i < 9; i++)
            {
                Random rd = new Random();
                int rdno=rd.Next(0,9);
                button2.Text = Convert.ToString(rdno);
                temp = arr[rdno];
                arr[rdno] = arr[(arr.Length - (i + 1))];
                arr[arr.Length - (i+1)] = temp;
            }
                for(int i=0;i<9;i++)
                {
                    Button1[i].Text = Convert.ToString(arr[i]);
          
                }
               
            }

        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否要結束遊戲?", "結束遊戲", MessageBoxButtons.YesNo,
                 MessageBoxIcon.Information, MessageBoxDefaultButton.Button1,
                 MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
            {
                System.Windows.Forms.Application.Exit();
            }
        }
      
   
    }
}
       
   

沒有留言:

張貼留言