本文實(shí)例為大家分享了C#15子游戲的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
所需控件:一個(gè)Button,拖入Form1中即可。
源碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;  namespace ShiWuZhiGame {   public partial class Form1 : Form   {     public Form1()     {       InitializeComponent();     }      const int N = 4; //按鈕的行、列數(shù)     Button[,] buttons = new Button[N, N]; //按鈕的數(shù)組      private void Form1_Load(object sender, EventArgs e)     {       //產(chǎn)生所有按鈕       GenerateAllButtons();     }     //打亂順序     void Shuffle()     {       //多次隨機(jī)交換兩個(gè)按鈕       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 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 = r * N + c; //這個(gè)數(shù)據(jù)用來表示它所在行列位置            //注冊(cè)事件           btn.Click += new EventHandler(btn_Click);            buttons[r, c] = btn; //放到數(shù)組中           this.Controls.Add(btn); //加到界面上         }       buttons[N - 1, N - 1].Visible = false; //最后一個(gè)不可見     }      //交換兩個(gè)按鈕     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;     }      //按鈕點(diǎn)擊事件處理     void btn_Click(object sender, EventArgs e)     {       Button btn = sender as Button; //當(dāng)前點(diǎn)中的按鈕       Button blank = FindHiddenButton(); //空白按鈕        //判斷是否與空白塊相鄰,如果是,則交換       if (IsNeighbor(btn, blank))       {         Swap(btn, blank);         blank.Focus();       }        //判斷是否完成了       if (ResultIsOk())       {         MessageBox.Show("ok");       }     }      //查找要隱藏的按鈕     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; //Tag中記錄是行列位置       int b = (int)btnB.Tag;       int r1 = a / N, c1 = a % N;       int r2 = b / N, c2 = b % N;        if (r1 == r2 && (c1 == c2 - 1 || c1 == c2 + 1) //左右相鄰         || c1 == c2 && (r1 == r2 - 1 || r1 == r2 + 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;     }      private void button1_Click_1(object sender, EventArgs e)     {       //打亂順序       Shuffle();     }   } } 運(yùn)行效果如下:
程序啟動(dòng)時(shí):

點(diǎn)擊開始后:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選