C#十五子游戲編寫代碼
更新時間:2017年05月09日 09:36:46 作者:愛笛生計劃
這篇文章主要為大家詳細(xì)介紹了C#十五子游戲的編寫代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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)
{
//點擊“開始”按鈕,打亂順序
Shuffle();
}
//打亂順序函數(shù)
void Shuffle()
{
//多次隨機(jī)交換兩個按鈕
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]);//交換兩個按鈕位置
}
}
//生成所有按鈕函數(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屬性是給程序員自己用的,做點標(biāo)記,類似于按鈕的ID,此處這個數(shù)據(jù)用來表示它所在的行列位置
//注冊事件
btn.Click += new EventHandler(btn_click);
buttons[r, c] = btn;//放到數(shù)組中
this.Controls.Add(btn);//加到界面上
}
}
buttons[N - 1, N - 1].Visible = false;//定義最后一個按鈕不可見
}
//交換兩個按鈕函數(shù)
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;
}
//按鈕點擊事件處理
void btn_click(object sender,EventArgs e)
{
Button btn = sender as Button;//當(dāng)前點中的按鈕
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();
}
}
}
效果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# Record構(gòu)造函數(shù)的行為更改詳解
C# 9 中的record類型是僅具有只讀屬性的輕量級、不可變數(shù)據(jù)類型(或輕量級類),下面這篇文章主要給大家介紹了關(guān)于C# Record構(gòu)造函數(shù)的行為更改的相關(guān)資料,需要的朋友可以參考下2021-08-08
WinFrom中l(wèi)abel背景透明的實現(xiàn)方法
這篇文章主要介紹了WinFrom中l(wèi)abel背景透明的實現(xiàn)方法,方法簡單實用,是C#程序設(shè)計中非常實用的技巧,需要的朋友可以參考下2014-09-09
C#調(diào)用C++使用C++/CLI的實現(xiàn)
在C#開發(fā)過程中,我們可能會遇到需要調(diào)用Windows API 或是第三方庫的場景,本文主要介紹了C#調(diào)用C++使用C++/CLI的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-03-03

