这篇文章主要为大家详细介绍了C#实现数字华容道游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了C#实现数字华容道游戏的具体代码,供大家参考,具体内容如下
代码如下:
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
//产生所有按钮
GenerateAllButtons();
}
private void button1_Click(object sender, EventArgs e)
{undefined
Shuffle();
}
//打乱顺序
void Shuffle()
{undefined
//多次随机交换两个按钮
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;//这个数据用来表示它所在的行列位置
//注册事件
btn.Click += new EventHandler(btn_Click);
buttons[r, c] = btn;
this.Controls.Add(btn);
}
buttons[N - 1, N - 1].Visible = false;//最后一个不可见
}
//交换两个按钮
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;
}
//按钮点击事件处理
void btn_Click(object sender, EventArgs e)
{undefined
Button btn = sender as Button;//当前点中按钮
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;
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:C#实现数字华容道游戏
基础教程推荐
猜你喜欢
- C# 调用WebService的方法 2023-03-09
- C# windows语音识别与朗读实例 2023-04-27
- winform把Office转成PDF文件 2023-06-14
- ZooKeeper的安装及部署教程 2023-01-22
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- C#类和结构详解 2023-05-30
- C# List实现行转列的通用方案 2022-11-02
- unity实现动态排行榜 2023-04-27
- 一个读写csv文件的C#类 2022-11-06
- C#控制台实现飞行棋小游戏 2023-04-22