在线演示地址:Silverlight+WCF 新手实例 象棋 在线演示
在Silverlight+WCF 新手实例 象棋 棋子移动-规则[附加上半盘限制](十)中,由Silenus-G提出规则还有点bug:
红车竟然可以走到红马的地盘:这是由于鼠标点在棋子之外的地方时,我们产生的是棋子移动[不是吃子],而在移动之时,我们又没有判断要移动到的位置上是不是有其它棋子从而引发了经济纠纷。因此,解决这个问题,我们只需在点击事件里增加一下判断棋子存不存在就可以了。
而在移动的规则里,这节我们同时进行补充,小调整一下其它两个规则:
1:车和炮的判断语句少了几个“括号”;
2:增加将军面对面可以吃子
下面开始:
对于红车抢马地盘这个问题,我们增加一行if代码就可以解决了,我们回到Chess象棋类里,找到鼠标点击事件:
void control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource is Canvas || e.OriginalSource is Line)
{
Chessman chessman =ReadyMoveChessman;
if (chessman != null)
{
Point fixPoint = FixPoint(e.GetPosition(null));
if (FindChessman(fixPoint) == null)//这里是新增加的一行判断语句
{
Action.MoveTo(chessman, fixPoint);
}
}
}
}
{
if (e.OriginalSource is Canvas || e.OriginalSource is Line)
{
Chessman chessman =ReadyMoveChessman;
if (chessman != null)
{
Point fixPoint = FixPoint(e.GetPosition(null));
if (FindChessman(fixPoint) == null)//这里是新增加的一行判断语句
{
Action.MoveTo(chessman, fixPoint);
}
}
}
}
OK,红车抢马的事故我们一行代码就解决了,下面为原有规则的车和炮增加括号:
我们回到ChessRule规则类里:
车规则修正:
//这一行是车原有的if判断:
if (x1 == x2 && yCount == 0 || y1 == y2 && xCount == 0)
//加上括号修正为:
if ((x1 == x2 && yCount == 0) || (y1 == y2 && xCount == 0))
if (x1 == x2 && yCount == 0 || y1 == y2 && xCount == 0)
//加上括号修正为:
if ((x1 == x2 && yCount == 0) || (y1 == y2 && xCount == 0))
炮规则修正:
//这个是炮原有的if语句
if (x1 == x2 && (yCount == 0 && !exits) || (yCount == 1 && exits)
|| y1 == y2 && (xCount == 0 && !exits) || (xCount == 1 && exits))
//现修正括号为:
if (x1 == x2 && ((yCount == 0 && !exits) || (yCount == 1 && exits))
|| y1 == y2 && ((xCount == 0 && !exits) || (xCount == 1 && exits)))
if (x1 == x2 && (yCount == 0 && !exits) || (yCount == 1 && exits)
|| y1 == y2 && (xCount == 0 && !exits) || (xCount == 1 && exits))
//现修正括号为:
if (x1 == x2 && ((yCount == 0 && !exits) || (yCount == 1 && exits))
|| y1 == y2 && ((xCount == 0 && !exits) || (xCount == 1 && exits)))
OK,接下来是增加将军面对面的规则了:
同时增加棋子同种颜色判断[目前来说该规则没啥用。吃子规则里同颜色会自动切换,不会产生吃子]:
/// <summary>
/// 移动规则
/// </summary>
/// <param name="move">棋子</param>
/// <param name="eat">移动的位置的数组</param>
/// <returns></returns>
public bool IsCanMove(Chessman chessman, Point moveTo)
{
//...省略N行...
if (x2 >= 0 && x2 < 9 && y2 >= 0 && y2 < 10 && !(x1 == x2 && y1 == y2))//在棋盘之内,非原步。
{
Chessman man = Action.Parent.FindChessman(moveTo);
if (man != null && chessman.Color == man.Color)//增加棋子存在和颜色判断
{
return false;
}
ChessType chessType = GetChessTypeByName(chessman.Name);
switch (chessType)
{
//...省略N行...
case ChessType.Jiang:
//将军面对面
if (man != null && GetChessTypeByName(man.Name) == ChessType.Jiang)
{
OutCount(chessman.MovePoint, moveTo, out xCount, out yCount);
return x1 == x2 && yCount == 0;
}
//...省略N行... }
}
return false;
}
/// 移动规则
/// </summary>
/// <param name="move">棋子</param>
/// <param name="eat">移动的位置的数组</param>
/// <returns></returns>
public bool IsCanMove(Chessman chessman, Point moveTo)
{
//...省略N行...
if (x2 >= 0 && x2 < 9 && y2 >= 0 && y2 < 10 && !(x1 == x2 && y1 == y2))//在棋盘之内,非原步。
{
Chessman man = Action.Parent.FindChessman(moveTo);
if (man != null && chessman.Color == man.Color)//增加棋子存在和颜色判断
{
return false;
}
ChessType chessType = GetChessTypeByName(chessman.Name);
switch (chessType)
{
//...省略N行...
case ChessType.Jiang:
//将军面对面
if (man != null && GetChessTypeByName(man.Name) == ChessType.Jiang)
{
OutCount(chessman.MovePoint, moveTo, out xCount, out yCount);
return x1 == x2 && yCount == 0;
}
//...省略N行... }
}
return false;
}
我们增加了棋子查找,然后在将军里判断一下移动到的位置的点上的棋子是不是对方的将军,如果是,就按类似车的规则[只竖走]进行移动。
OK,本节就点到为止了。