MineBoard类,将布局和交互的功能都移到另外的类上实现。

Code
using System;
using System.Collections;
using System.Collections.Generic;

namespace xingd.Minesweeper


{
public enum GameStatus

{
NotStarted,
Playing,
Win,
Lose
}

public enum MineCellStatus : short

{
Covered,
MarkDoubt,
MarkMine,
Uncovered,
Exploded
}

public class MineCell

{
public MineCellStatus Status;
public bool IsMine;
public byte NearbyMines;
}

public class MineCellActionArgs

{
private MineCell cell;
private int row;
private int column;

public MineCellActionArgs(MineCell cell, int row, int column)

{
this.cell = cell;
this.row = row;
this.column = column;
}


public MineCell Cell
{ get
{ return cell; } }

public int Row
{ get
{ return row; } }

public int Column
{ get
{ return column; } }
}

public class MineBoard

{
private MineCell[,] cells = new MineCell[0, 0];

private GameStatus status;


public int Columns
{ get
{ return cells.GetLength(1); } }

public int Rows
{ get
{ return cells.GetLength(0); } }

public int TotalMines
{ get
{ return Count(c => c.IsMine); } }

public int MarkedMines
{ get
{ return Count(c => c.Status == MineCellStatus.MarkMine); } }

public int UncoveredMines
{ get
{ return Count(c => c.Status == MineCellStatus.Uncovered); }}


public GameStatus Status
{ get
{ return status; } }

public MineCell this[int row, int col]
{ get
{ return cells[row, col]; } }

public void Init(int rows, int columns, Action<MineBoard> builder)

{
cells = new MineCell[rows, columns];
for (int i = 0; i < rows; i++)

{
for (int j = 0; j < columns; j++)

{
cells[i, j] = new MineCell();
}
}
builder(this);
status = GameStatus.NotStarted;
}

public bool IsValidCell(int row, int column)

{
return row >= 0 && row < Rows && column >= 0 && column < Columns;
}

public void WalkNearbyCells(int row, int column, Action<MineCellActionArgs> callback, Predicate<MineCellActionArgs> match)

{
if (IsValidCell(row, column))

{
MineCellActionArgs args = new MineCellActionArgs(cells[row, column], row, column);
callback(args);

if (match(args))

{
for (int i = -1; i < 2; i++)

{
for (int j = -1; j < 2; j++)

{
if (i != 0 || j != 0)

{
WalkNearbyCells(row + i, column + j, callback, match);
}
}
}
}
}
}

public void DoAction(Action<MineBoard> action)

{
if (status == GameStatus.NotStarted)

{
status = GameStatus.Playing;
}

if (status == GameStatus.Playing)

{
action(this);
}

int count = 0;

// No TrueForAll for two-dimensional arrays, so manual foreach needed
foreach (MineCell cell in cells)

{
if (cell.IsMine && cell.Status == MineCellStatus.Exploded)

{
status = GameStatus.Lose;
return;
}
else if (cell.Status == MineCellStatus.MarkMine || cell.Status == MineCellStatus.Uncovered)

{
++count;
}
}

if (count == cells.Length)

{
status = GameStatus.Win;
}
}

private int Count(Predicate<MineCell> match)

{
int count = 0;

foreach (MineCell cell in cells)

{
if (match(cell)) count++;
}

return count;
}
}
}

为了代码上的灵活性和进一步扩展,将MineCell由原来的struct改为了class。
对于比Minesweeper更复杂的游戏,也可以将坐标(row, column)直接加到MineCell类中,或者将MineBoard的引用也存储在MineCell类内。
MineBoard类提供了三个核心的逻辑实现方法,Init方法接受一个Action<MineBoard>参数,允许外部指定初始化布局。与上一篇随笔中描述的不同,这次调整将MineBoard的大小与初始化布局分开了,因此Init方法也需要传入rows和columns,由MineBoard类创建所有的MineCell实例后再由builder初始布局。
WalkNearbyCells实现对一个MineCell周围递归处理的功能,原型为WalkNearbyCells(int row, int column, Action<MineCellActionArgs> callback, Predicate<MineCellActionArgs> match)。MineCellActionArgs包含MineCell和其坐标,如果按前文描述将坐标加上MineCell类,则不需要单独的一个Args类了。
DoAction方法用来实现逻辑操作,具体操作由传入的
Action<MineBoard> action指定。
在MineActions文件中,实现了一个纯虚基类:
public abstract class BaseMineAction


{
protected int row;
protected int column;

public BaseMineAction(int row, int column)

{
if (row < 0 || column < 0)

{
throw new ArgumentException();
}

this.row = row;
this.column = column;
}

public void Run(MineBoard board)

{
if (board.IsValidCell(row, column))

{
MineCell cell = board[row, column];
RunValidCell(board, cell);
}
}

protected abstract void RunValidCell(MineBoard board, MineCell cell);
}
DistanceMatch类用来限制递归操作中远离初始点的距离,也可以在MineBoard类上加入一个重载的WalkNeabyCells方法,指定depth。不过从通用性的角度,仅保留了传入Match<MineCellActionArgs>的方法。
public class DistanceMatch


{
private int row;
private int column;
private int distance;

public DistanceMatch(int row, int column, int distance)

{
this.row = row;
this.column = column;
this.distance = distance;
}

public bool Match(MineCellActionArgs args)

{
return Math.Abs(args.Row - row) < distance && Math.Abs(args.Column - column) < distance;
}
}
具体实现的四种Action为PlaceOneMineAction,MineUncoverAction,MineMarkAction和MineUncoverNearbyAction,都在MineActions.cs中,暂不作详细描述了,有问题可以在回复中指出。实现脚本引擎后,这四个Action都可以转换成为脚本实现,或者在脚本中实现用户鼠标操作到某个Action的绑定。
MineBuilders.cs中目前仅实现了一种初始化布局逻辑,RandomBoardBuilder。特别说明的是,RandomBoardBuilder类并不是在放置完所有雷后计算每个点的相邻雷数,而且使用PlaceOneMineAction去触发周围Cell的相邻雷数更新。
项目文件下载:20080322.zip
系列索引:Minesweeper: 索引