您的位置:首页 > 编程语言

北大青鸟 中国象棋 实习项目 (项目源代码级说明五)

2008-07-19 19:54 393 查看
棋子马

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace chess
{
/// <summary>
/// 棋子马
/// </summary>
[Serializable()]
class chessItemMa : ChessItemBase
{
/// <summary>
/// 判断绊脚
/// </summary>
/// <param name="begin">起始点</param>
/// <param name="end">结束点</param>
/// <returns>是否有绊脚情况bool</returns>

public bool HaveChess(Point begin, Point end)
{
//判断是否绊脚
Panel panel = (Panel)this.Parent;
ControlCollection colection = panel.Controls;
bool flag = true;
Point temp = new Point();
//往上跳
if ((end.Y < begin.Y) && ((end.X - begin.X == 1) || (end.X - begin.X == -1)))
{
temp = new Point(begin.X, begin.Y - 1);
}
//往下跳
if ((end.Y > begin.Y )&& ((end.X - begin.X == 1) || (end.X - begin.X == -1)))
{
temp = new Point(begin.X,begin.Y+1);
}
//往左方跳
if ((end.X < begin.X) && ((end.X - begin.X == 2) || (end.X - begin.X == -2)))
{
temp = new Point(begin.X-1,begin.Y);
}
//往右方跳
if ((end.X > begin.X) && ((end.X - begin.X == 2) || (end.X - begin.X == -2)))
{
temp = new Point(begin.X+1,begin.Y);
}
foreach (Control control in colection)
{
Point point = PointToGrid(control.Location.X, control.Location.Y);
point.Offset(1, 1);
if ((temp.X == point.X) && (temp.Y == point.Y))
{
flag = false;
}
}
return flag;
}
/// <summary>
/// 重写父类限制走法方法
/// </summary>
/// <param name="ChessX">坐标点的X坐标</param>
/// <param name="ChessY">坐标点的Y坐标</param>
/// <returns>返回结果bool</returns>
protected override bool LimitPoint(int ChessX, int ChessY)
{
//先执行基类中的方法
if (base.LimitPoint(ChessX, ChessY))
{
Point begin = PointToGrid(this.Location.X, this.Location.Y);
begin.Offset(1, 1);
Point end = PointToGrid(ChessX, ChessY);
//是否走的日字格
if (((begin.X - end.X) == -1) || ((begin.X - end.X) == 1))
{
if (((begin.Y - end.Y) == -2) || ((begin.Y - end.Y) == 2))
{
if (this.chesstype == ChessTypes.black)
{
if (HaveChess(begin, end))
{
return true;
}
}
else if (this.chesstype == ChessTypes.blue)
{
if (HaveChess(begin, end))
{
return true;
}
}
}
}
if (((begin.X - end.X) == -2) || ((begin.X - end.X) == 2))
{
if (((begin.Y - end.Y) == -1) || ((begin.Y - end.Y) == 1))
{
if (this.chesstype == ChessTypes.black)
{
if (HaveChess(begin, end))
{
return true;
}
}
else if (this.chesstype == ChessTypes.blue)
{
if (HaveChess(begin, end))
{
return true;
}
}
}
}

}
return false;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: