enum GameState
{
Ready,
Started,
Victory,
Failed
};
public partial class GameFrame : UserControl
{
private Color _defBackColor = Color.FromArgb(0x80, 0x80, 0x80);
//private const int SPACE_WIDTH = ZERO;
private const int ZERO = 0;
private const int ONE_SEC = 1000;
public EventHandler OnGameInitialized = null;
public EventHandler OnGemeStart = null;
public EventHandler OnGameEnd = null;
public EventHandler OnGameVictory = null;
public EventHandler OnGamingClick = null;
public EventHandler OnGameTick = null;
private Block[,] _blocks = null;
private int _width = ZERO;
private int _height = ZERO;
private GameState _gameState = GameState.Ready;
internal GameState GameState
{
get { return _gameState; }
set
{
_gameState = value;
switch (_gameState)
{
case GameState.Ready:
_timer.Enabled = false;
break;
case GameState.Started:
_timer.Enabled = true;
break;
case GameState.Victory:
_timer.Enabled = false;
break;
case GameState.Failed:
_timer.Enabled = false;
break;
}
}
}
private int _bombNum = ZERO;
public int BombNum
{
get { return _bombNum; }
set { _bombNum = value; }
}
private int _rightFlagNum = ZERO;
public int RightFlagNum
{
get { return _rightFlagNum; }
set
{
_rightFlagNum = value;
if (_rightFlagNum == BombNum)
{
GameVictory();
}
}
}
private int _openedBlockNum = ZERO;
public int OpenedBlockNum
{
get { return _openedBlockNum; }
set
{
_openedBlockNum = value;
if (_openedBlockNum == _width * _height - _bombNum)
{
GameVictory();
}
}
}
private int _flagNum = ZERO;
public int FlagNum
{
get { return _flagNum; }
set { _flagNum = value; }
}
private Timer _timer = new Timer();
private int _sec = ZERO;
public int Sec
{
get { return _sec; }
set { _sec = value; }
}
private void timer_Tick(object sender, EventArgs e)
{
_sec++;
if (OnGameTick != null)
{
OnGameTick(this, new EventArgs());
}
}
public GameFrame()
{
InitializeComponent();
this.BackColor = _defBackColor;
this.BorderStyle = BorderStyle.FixedSingle;
Block.Owner = this;
Block.BtImg = Image.FromFile("blockClosed.gif");
Block.FlagImg = Image.FromFile("flag.gif");
Block.BombImg = Image.FromFile("bomb.gif");
Block.BackGroundImg = Image.FromFile("backGround.gif");
_timer.Interval = ONE_SEC;
_timer.Tick += new EventHandler(timer_Tick);
}
public GameFrame(int blockWidth)
{
InitializeComponent();
Block.Owner = this;
Block.Width = blockWidth;
}
public void InitGame(int width, int height, int bombNum)
{
if (bombNum >= (width * height))
{
throw new Exception("要厚道!不能设置太多的地雷!");
}
this._bombNum = bombNum;
this.Width = width * Block.Width ;
this.Height = height * Block.Width;
this._blocks = new Block[height, width];
this._width = width;
this._height = height;
this.FormatBlocks(_blocks);
GameState = GameState.Ready;
this.Sec = ZERO;
this.OpenedBlockNum = ZERO;
this.FlagNum = ZERO;
this.RightFlagNum = ZERO;
this.Refresh();
if (OnGameInitialized != null)
{
OnGameInitialized(this, new EventArgs());
}
}
public void GameStart()
{
if (OnGemeStart != null)
{
OnGemeStart(this, new EventArgs());
}
GameState = GameState.Started;
}
private void GameVictory()
{
GameState = GameState.Victory;
if (OnGameVictory != null)
{
OnGameVictory(this, new EventArgs());
}
}
public void GameEnd()
{
GameState = GameState.Failed;
Showbomb();
if (OnGameEnd != null)
{
OnGameEnd(this, new EventArgs());
}
}
private void FormatBlocks(Block[,] blocks)
{
for (int y = ZERO; y < _height; y++)
{
for (int x = ZERO; x < _width; x++)
{
blocks[y, x] = new Block(x * Block.Width , y * Block.Width, false);
}
}
BombSetup(blocks, _bombNum);
BlockNumSetup(blocks);
}
private void BombSetup(Block[,] blocks,int bombNum)
{
Random rd = new Random((int)DateTime.Now.Ticks);
int num = ZERO;
while (num != bombNum)
{
int x = rd.Next(ZERO, _width);
int y = rd.Next(ZERO, _height);
Block block = _blocks[y, x];
if (!block.Bomb)
{
block.Bomb = true;
num++;
}
}
}
private void BlockNumSetup(Block[,] blocks)
{
for (int y = ZERO; y < _height; y++)
{
for (int x = ZERO; x < _width; x++)
{
Block block = _blocks[y, x];
if (!block.Bomb)
{
SetBlockNum(x, y);
}
}
}
}
private void SetBlockNum(int x,int y)
{
Block block = _blocks[y, x];
int num = ZERO;
if (GetBlockHasBomb(x - 1, y - 1))
{
num++;
}
if (GetBlockHasBomb(x, y - 1))
{
num++;
}
if (GetBlockHasBomb(x + 1, y - 1))
{
num++;
}
if (GetBlockHasBomb(x - 1, y))
{
num++;
}
if (GetBlockHasBomb(x + 1, y))
{
num++;
}
if (GetBlockHasBomb(x - 1, y + 1))
{
num++;
}
if (GetBlockHasBomb(x, y + 1))
{
num++;
}
if (GetBlockHasBomb(x + 1, y + 1))
{
num++;
}
block.Num = num;
}
private bool GetBlockHasBomb(int x,int y)
{
if (x < ZERO || y < ZERO || x >= _width || y >= _height)
{
return false;
}
Block block = _blocks[y, x];
return block.Bomb;
}
还放不下..