📄 layout.cs
字号:
using System;
using System.Collections;
namespace HRD.Core
{
public class Layout
{
private Mediator _mediator;
private Chessman[] _chessmen = new Chessman[10];
private bool _gotTheAnswer = false;
private int _current = 0;
private BlankPosition _blankPosition;
#region 搜索可行布局并封装
//===========================================================
// 根据当前棋盘布局获取下一步可走的方案
//===========================================================
public void CheckAvailableSteps()
{
CallBackDelegate _callback = new CallBackDelegate(EncapsulateChessStep);
for(_current = 0; _current<10; _current++)
_chessmen[_current].CheckAvailableSteps(_blankPosition, _callback);
}
//===========================================================
// 获取移动棋子后,棋盘的布局
//===========================================================
private void EncapsulateChessStep(int newPosition, BlankPosition newBlankPosition, MoveMethod mm)
{
if( _gotTheAnswer )
return;
Layout l = _mediator.AllocateLayout();
// 生成新布局
for(int i = 0; i<10; i++)
{
if(i == _current)
l.chessmen[i].position = newPosition;
else
l.chessmen[i].position = this.chessmen[i].position;
}
l.blankPosition = newBlankPosition;
ChessStep cs = new ChessStep();
cs.layout = l.ToInt64();
cs.chessmanPosition = this.chessmen[_current].position;
cs.moveMethod = mm;
// 将封装好的ChessStep发送给mediator作进一步处理
if(l.IsFinished)
{
_gotTheAnswer = true;
_mediator.Finished(cs);
}
else
_mediator.CheckStep(cs);
}
#endregion
#region 将棋局转换整数
//===========================================================
// 将当前棋局转换为一整数
//===========================================================
public Int64 ToInt64()
{
Int64 result = 0;
Int64 typeCode, positionCode;
for(int i=0; i<10; i++)
{
typeCode = (int)_chessmen[i].chessmanType;
positionCode = _chessmen[i].position - (_chessmen[i].position/8) * 4;
result += typeCode << ((int)(3 * positionCode));
}
return result;
}
#endregion
#region Property Method
public bool IsFinished
{
get
{
for(int i=0; i<10; i++)
if(_chessmen[i].chessmanType == ChessmanType.General &&
_chessmen[i].position == 25)
return true;
return false;
}
}
public Chessman[] chessmen
{
get
{
return _chessmen;
}
set
{
_chessmen = value;
}
}
public BlankPosition blankPosition
{
get
{
return _blankPosition;
}
set
{
_blankPosition = value;
}
}
public Mediator mediator
{
set
{
_mediator = value;
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -