惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - magicdlf

[HIMCM暑期班]第4课: 扑克牌问题 [HIMCM暑期班]第3课:一个博弈问题 [HIMCM暑期班]第2课:建模 [HIMCM暑期班]第1课:概述 [C#]ASP.NET MVC 3 在线学习资料 [HIMCM]Consortium可以免费下载了! [C#]在Windows Service中使用ThreadPool [HIMCM]MathType小练习 [C#]DataGridView中使用数据绑定Enum类型 SRM 518 解题报告 SRM 522 解题报告 SRM 521 解题报告 [原创]简易版Socket聊天室 附源码 再谈C#扫雷 如何通过P/Invoke返回Struct和String Array 计算文件的散列值 zz .NET抽象工厂模式 from cnblogs 清空VSTFS cache 如何在VSTFS中设置email notification
C#实现扫雷出炉
magicdlf · 2009-04-28 · via 博客园 - magicdlf

前阵子想用C#写个扫雷,检验一下学习一年多C#的功力如何。扫雷的基本实现很简单,动态生成控件,然后用Controls.Add添加到窗口中,用随机数布雷,处理Mouse事件...最麻烦的事情不过是在点到空白地带的时候需要实现一个FloodFill算法。

不过要实现一个十分接近Windows扫雷的版本,还是需要费一些周折的。首先需要找到资源文件,我是在百度知道上搜到的,或者从扫雷的资源文件里读取也可以。

其次,控件上的图像,要用gdi+画上去,如果使用picture_box去load image的话会很慢,点到大片空白的时候会有闪烁。

设计的时候也有一些技巧,把MineControl控件和Form的逻辑隔开,每个MineControl记录自己的状态,分别为: Initial(初始), Pressed(鼠标左键按住不放), Flag(插上小旗), QuestionMark(标记为问号), Unseal(翻开后) 。同时提供Press(), UnPress(), PutFlag(), Unseal() 这些操作。这样做的好处是,使原本复杂的判断变得简单,在Form中,只需要根据当前的鼠标事件调用这些操作即可,而这些操作本身会完成MineControl的状态转移,并显示出来。

鼠标的左右键处理也有些小技巧,好像以前在VB里,可以直接获得鼠标左右键当前的状态,用Mouse.Left|Mouse.Right来判断鼠标左右键同时按下的状况。但是C#的MouseDown事件中,只能知道当前鼠标按下的键是什么,因为每个键按下总有先后,在C#里,左右键同时按下会触发两次MouseDown事件。于是,需要设一个全局变量,记录鼠标左右键当前的状态。

最后,重写MineControl的Paint方法,用gdi+把icon画到窗口上。

 

附上源码

 SweepMine.rar

写本程序的时候参考了一下icebird的程序,附上链接:

http://www.cnblogs.com/Icebird/archive/2006/06/21/CSharpMine.html

这个实现比较完善,我的程序只实现了最基本的功能。不过自认为结构更清晰一些,欢迎大家拍砖~