C#WinFrom寫的拼圖遊戲

來源:互聯網
上載者:User

標籤:box   產生   const   產生   and   class   generate   事件   檢查   

1.視窗載入時自動產生拼圖按鈕

  const int N = 4;//按鈕的行、列數        Button[,] buttons = new Button[N, N];//按鈕的數組        int step = 0;//記錄步數        private void Form3_Load(object sender, EventArgs e)        {            //產生所有按鈕            GenerateAllButtons();        }        private void button1_Click(object sender, EventArgs e)        {            //打亂順序            this.button1.Text = "重新開始";            Shuffle();        }        void GenerateAllButtons()        {            int x0 = 100, y0 = 10, w = 45, d = 50;            for (int r = 0; r < N; r++)            {                for (int c = 0; c < N; c++)                {                    int num = r * N + c;                    Button btn = new Button();                    btn.Text = (num + 1).ToString();                    btn.Top = y0 + r * d;                    btn.Left = x0 + c * d;                    btn.Width = w;                    btn.Height = w;                    btn.Visible = true;                    btn.Tag = num;                    //註冊事件                    btn.Click += new EventHandler(Btn_Click);                    buttons[r, c] = btn; //放到數組中                    this.Controls.Add(btn); //加到介面上                }            }            buttons[N - 1, N - 1].Visible = false;//最後一個btn不可見面        }               private void Btn_Click(object sender, EventArgs e)        {            step++;            Button btn = sender as Button;//當前選中的按鈕            Button blank = FindHiddenButton();//空白的按鈕            //判斷是否與空白塊相鄰,如果是,則交換            if (IsNeighbor(btn, blank))            {                Swap(btn, blank);                blank.Focus();            }            //判斷是否完成了            if (ResultIsOk())            {                string result = "你真棒!"+"\n一共用了"+step.ToString()+"步";                MessageBox.Show(result);            }        }        Button FindHiddenButton()        {            for (int r = 0; r < N; r++)            {                for (int c = 0; c < N; c++)                {                    if (!buttons[r, c].Visible)                    {                        return buttons[r, c];                    }                }            }            return null;        }        bool IsNeighbor(Button btnA, Button btnB)        {            int a = (int)btnA.Tag;//獲得按鈕記錄的值            int b = (int)btnB.Tag;            int r1 = a / N;int c1 = a % N;//得到按鈕的行號和列號            int r2 = b / N; int c2 = b % N;            if ((r1 == r2 && (c1 - c2 == 1 || c2 - c1 == 1))//左右相鄰                || (c1 == c2 && (r1 - r2 == 1 || r2 - r1 == 1))//上下相鄰                )                {                return true;            }            return false;        }        //檢查是否完成        bool ResultIsOk()        {            for (int r = 0; r < N; r++)                for (int c = 0; c < N; c++)                {                    if (buttons[r, c].Text != (r * N + c + 1).ToString())                    {                        return false;                    }                }            return true;        }        //打亂順序        void Shuffle()        {            //多次隨機交換兩個按鈕            Random rnd = new Random();            for (int i = 0; i < 100; i++)            {                int a = rnd.Next(N);                int b = rnd.Next(N);                int c = rnd.Next(N);                int d = rnd.Next(N);                Swap(buttons[a, b], buttons[c, d]);            }        }        //交換兩個按鈕        void Swap(Button btna, Button btnb)        {            string t = btna.Text;            btna.Text = btnb.Text;            btnb.Text = t;            bool v = btna.Visible;            btna.Visible = btnb.Visible;            btnb.Visible = v;        }    }

 

C#WinFrom寫的拼圖遊戲

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.