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

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - xingd

Minesweeper: 新版代码 Minesweeper: GDI+ 初步实现 Minesweeper: GDI+ 综述 SQL Server 2008: Change Data Capture和Change Tracking SQL Server 2008: 常见缩写汇总 SQL Server 2008: Feb CTP 企业数据平台增强 SQL Server 2008: Feb CTP 关系数据库引擎增强 SQL Server 2008: Installation Center SQL Server 2008: Feb CTP 开放下载 Minesweeper: GDI+ Line Scratch Minesweeper: GDI+ 概述 Minesweeper: 索引 推荐.NET新书 忽略大小写的.NET脏字过滤算法 2007年度总结 再度提升!.NET脏字过滤算法 单独谈谈C#3.0 (再发).NET脏字过滤算法 (重发).NET脏字过滤算法
Minesweeper: 代码结构改进
xingd · 2008-03-19 · via 博客园 - xingd

接着前一篇文章,Minesweeper: GDI+ 初步实现,本文在代码结构上做一些改进,不涉及新的功能。

首先我们来看MineBoard类的Init方法:

public void Init(int rows, int columns, int mines)
{
    
if (rows <= 0 || columns <= 0 || mines >= columns * rows)
    {
        
throw new ArgumentException();
    }

    cells 

= new MineCell[rows, columns];
    totalMines 
= mines;
    makedMines 
= 0;
    status 
= GameStatus.NotStarted;

    PlaceRandomMines();
    UpdateNearbyMinesCount();
}


实际的游戏开发中,随机布局整个游戏区域的情况并不多,更为常见的还是从地图中读取。同时,为了实现游戏过程中保存当前的游戏进展,然后读取后继续,或者支持从网络上下载布局,以及同其他人分享等,需要将布局的功能移到MineBoard在之外。

调整后的代码如下:

public interface IMineBoardBuilder
{
    
int TotalMines { get; }
    
int MarkedMines { get; }

    MineCell[,] BuildMineCells();
}

public class RandomBoardBuilder : IMineBoardBuilder
{
    
private int rows;
    
private int columns;
    
private int totalMines;public RandomBoardBuilder(int rows, int columns, int mines)
    {
        
if (rows <= 0 || columns <= 0 || mines >= columns * rows)
        {
            
throw new ArgumentException();
        }
        
        
this.rows = rows;
        
this.columns = columns;
        
this.totalMines = mines;
    }
public int TotalMines { get { return totalMines; } }
    
public int MarkedMines { get { return 0; } }
    
    
public MineCell[,] BuildMineCells()
    {
        MineCell[,] cells 
= new MineCell[rows, columns];

        PlaceRandomMines(cells);
        UpdateNearbyMinesCount(cells);

return cells;
    }
}


PlaceRandomMines和UpdateNearbyMinesCount实现跟上一篇文章中一样,就不给出了。

MineBoard的Init方法则重构为:

public void Init(IMineBoardBuilder builder)
{
    cells 
= builder.BuildMineCells();
    totalMines 
= builder.TotalMines;
    makedMines 
= builder.MarkedMines;
    status 
= GameStatus.NotStarted;
}


下面的这个类可以很容易的实现二进制地图文件的读取。文本/XML格式的地图读取,也可以很容易的实现。

[Serializable]
public class BoardFileLoader : IMineBoardBuilder
{
    
private MineCell[,] cells;
    
private int totalMines;
    
private int markedMines;public int TotalMines { get { return totalMines; } }
    
public int MarkedMines { get { return markedMines; } }public MineCell[,] BuildMineCells()
    {
        
return cells;
    }
}

相应的,可以为MineBoard类添加保存游戏进展的功能。

在交互方面,也可作如下改进:

public void WalkNearByCells(int row, int col, MineWalkCallback callback, Predicate<MineCell> match)
{
    
if (IsValidCell(row, col))
    {
        callback(
ref cells[row, col]);if (match(cells[row, col]))
        {
            
for (int i = -1; i < 2; i++)
            {
                
for (int j = -1; j < 2; j++)
                {
                    
if (i != 0 || j != 0)
                    {
                        WalkNearByCells(row 
+ i, j + i, callback, match);
                    }
                }
            }
        }
    }
}
public void DoAction(IMineBoardAction action)
{
    action.Run(
this);
}