C#實(shí)現(xiàn)數(shù)字華容道游戲
本文實(shí)例為大家分享了C#實(shí)現(xiàn)數(shù)字華容道游戲的具體代碼,供大家參考,具體內(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 WindowsFormsApp6 {undefined ? ? public partial class Form1 : Form ? ? {undefined ? ? ? ? public Form1() ? ? ? ? {undefined ? ? ? ? ? ? InitializeComponent(); ? ? ? ? } ? ? ? ? const int N = 4; ? ? ? ? Button[,] buttons = new Button[N, N]; ? ? ? ? private void Form1_Load(object sender, EventArgs e) ? ? ? ? {undefined ? ? ? ? ? ? //產(chǎn)生所有按鈕 ? ? ? ? ? ? GenerateAllButtons(); ? ? ? ? } ? ? ? ? private void button1_Click(object sender, EventArgs e) ? ? ? ? {undefined ? ? ? ? ? ? Shuffle(); ? ? ? ? } ? ? ? ? //打亂順序 ? ? ? ? void Shuffle() ? ? ? ? {undefined ? ? ? ? ? ? //多次隨機(jī)交換兩個(gè)按鈕 ? ? ? ? ? ? Random rnd = new Random(); ? ? ? ? ? ? for (int i=0;i<100;i++) ? ? ? ? ? ? {undefined ? ? ? ? ? ? ? ? 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() ? ? ? ? {undefined ? ? ? ? ? ? int x0 = 100, y0 = 10, w = 45, d = 50; ? ? ? ? ? ? for(int r=0;r<N;r++) ? ? ? ? ? ? ? ? for(int c = 0; c < N; c++) ? ? ? ? ? ? ? ? {undefined ? ? ? ? ? ? ? ? ? ? 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; ? ? ? ? ? ? ? ? ? ? this.Controls.Add(btn); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? buttons[N - 1, N - 1].Visible = false;//最后一個(gè)不可見 ? ? ? ? } ? ? ? ? //交換兩個(gè)按鈕 ? ? ? ? void Swap(Button btna,Button btnb) ? ? ? ? {undefined ? ? ? ? ? ? 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) ? ? ? ? {undefined ? ? ? ? ? ? Button btn = sender as Button;//當(dāng)前點(diǎn)中按鈕 ? ? ? ? ? ? Button blank= FindHiddenButton();//空白按鈕 ? ? ? ? ? ? //判斷與空白按鈕是否相鄰,如果是,交換 ? ? ? ? ? ? if (IsNeighbor(btn, blank)) ? ? ? ? ? ? {undefined ? ? ? ? ? ? ? ? Swap(btn, blank); ? ? ? ? ? ? ? ? blank.Focus(); ? ? ? ? ? ? } ? ? ? ? ? ? //判斷是否完成了 ? ? ? ? ? ? if (ResultIsOk()) ? ? ? ? ? ? {undefined ? ? ? ? ? ? ? ? MessageBox.Show("ok"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? //查找要隱藏的按鈕 ? ? ? ? Button FindHiddenButton() ? ? ? ? {undefined ? ? ? ? ? ? for (int r = 0; r < N; r++) ? ? ? ? ? ? ? ? for (int c = 0; c < N; c++) ? ? ? ? ? ? ? ? {undefined ? ? ? ? ? ? ? ? ? ? if (!buttons[r, c].Visible) ? ? ? ? ? ? ? ? ? ? {undefined ? ? ? ? ? ? ? ? ? ? ? ? return buttons[r, c]; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? return null; ? ? ? ? } ? ? ? ? //判斷是否相鄰 ? ? ? ? bool IsNeighbor(Button btnA, Button btnB) ? ? ? ? {undefined ? ? ? ? ? ? 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() ? ? ? ? {undefined ? ? ? ? ? ? for (int r = 0; r < N; r++) ? ? ? ? ? ? ? ? for (int c = 0; c < N; c++) ? ? ? ? ? ? ? ? {undefined ? ? ? ? ? ? ? ? ? ? if (buttons[r, c].Text != (r * N + c + 1).ToString()) ? ? ? ? ? ? ? ? ? ? {undefined ? ? ? ? ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? return true; ? ? ? ? } ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#編程實(shí)現(xiàn)獲取文件夾中所有文件的文件名
這篇文章主要介紹了C#編程實(shí)現(xiàn)獲取文件夾中所有文件的文件名,可實(shí)現(xiàn)獲取特定目錄下制定類型文件名稱的功能,涉及C#針對(duì)文件與目錄的遍歷、查詢等操作相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11DevExpress GridView自動(dòng)滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了DevExpress GridView自動(dòng)滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06分享一個(gè)C#編寫簡(jiǎn)單的聊天程序(詳細(xì)介紹)
這是一篇基于Socket進(jìn)行網(wǎng)絡(luò)編程的入門文章,我對(duì)于網(wǎng)絡(luò)編程的學(xué)習(xí)并不夠深入,這篇文章是對(duì)于自己知識(shí)的一個(gè)鞏固,同時(shí)希望能為初學(xué)的朋友提供一點(diǎn)參考。文章大體分為四個(gè)部分:程序的分析與設(shè)計(jì)、C#網(wǎng)絡(luò)編程基礎(chǔ)(篇外篇)、聊天程序的實(shí)現(xiàn)模式、程序?qū)崿F(xiàn)2015-12-12使用C#實(shí)現(xiàn)阿拉伯?dāng)?shù)字到大寫中文的轉(zhuǎn)換
這篇文章主要介紹了C#實(shí)現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)為大寫中文的實(shí)現(xiàn)代碼,需要的朋友可以參考下2007-03-03C#實(shí)現(xiàn)讀取匿名對(duì)象屬性值的方法示例總結(jié)
這篇文章主要介紹了C#實(shí)現(xiàn)讀取匿名對(duì)象屬性值的方法,結(jié)合實(shí)例形式總結(jié)分析了C#通過反射、轉(zhuǎn)換等方法讀取匿名對(duì)象屬性值的相關(guān)操作技巧,需要的朋友可以參考下2020-03-03