本文實(shí)例為大家分享了C#十五子游戲的具體代碼,供大家參考,具體內(nèi)容如下
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 WindowsFormsApplication15{ 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();  }  private void button1_Click(object sender, EventArgs e)  {   //點(diǎn)擊“開始”按鈕,打亂順序   Shuffle();  }  //打亂順序函數(shù)  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]);//交換兩個(gè)按鈕位置   }  }  //生成所有按鈕函數(shù)  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();//設(shè)置按鈕顯示的數(shù)字     btn.Top = y0 + r * d;//設(shè)置按鈕的左邊緣與容器的上邊緣之間的距離     btn.Left = x0 + c * d;//設(shè)置按鈕的左邊緣與容器的左邊緣之間的距離     btn.Width = w;//按鈕寬度     btn.Height = w;//按鈕高度     btn.Visible = true;//是否顯示按鈕     btn.Tag = r * N + c;//Tag屬性是給程序員自己用的,做點(diǎn)標(biāo)記,類似于按鈕的ID,此處這個(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è)按鈕函數(shù)  void Swap(Button btna,Button btnb)  {   //兩個(gè)按鈕的值交換   string t = btna.Text;   btna.Text = btnb.Text;   btnb.Text = t;   //兩個(gè)按鈕的可見屬性交換   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");   }  }  //查找要隱藏的按鈕函數(shù)  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;  }  //判斷是否相鄰函數(shù)  bool IsNeighbor(Button btnA,Button btnB)  {   int a = (int)btnA.Tag;//獲取Tag中保存的位置信息(0-15的值)   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 Btn_Click(object sender, EventArgs e)  {   throw new NotImplementedException();  } }}效果:

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