bool IChess.MoveTo(IChessPoint targetPoint) { var flag = PureMoveTo(targetPoint); Log(_lastPoint, targetPoint, flag); return(flag); }
/// <summary> /// 象移动算法 /// </summary> protected override bool CanMoveTo(IChessPoint p) { //绝对值法 if (Math.Abs(_currentPoint.X - p.X) != 2 || Math.Abs(_currentPoint.Y - p.Y) != 2) { return(false); } //中间不能有棋子 if (Table[(_currentPoint.X + p.X) / 2, (_currentPoint.Y + p.Y) / 2] != null) { return(false); } //越界算法 if (_currentPoint.Y <= 4 && p.Y > 4) { return(false); } if (_currentPoint.Y >= 5 && p.Y < 5) { return(false); } return(true); }
bool PureMoveTo(IChessPoint targetPoint) { //目标棋子和当前棋子颜色不能一致 IChess targetChess = Table[targetPoint]; if (targetChess != null && targetChess.Square == this.Square) { return(false); } //是否满足规则 if (!CanMoveTo(targetPoint)) { return(false); } //吃掉对方老王 if (Table[targetPoint] is King) { Log(_currentPoint, targetPoint, true); throw new GameLoseException(this.Color == ChessColor.Red ? "红方胜" : "黑方胜"); } //移动 Table[_currentPoint] = null; //吃掉棋子或移动棋子 Table[targetPoint] = this; _lastPoint = _currentPoint; _currentPoint = targetPoint; return(true); }
private void Log(IChessPoint lastPoint, IChessPoint targetPoint, bool flag) { if (_square.Logger == null) { return; } var msg = GetMoveMsg(this.Square.Camp, this.ChessType, lastPoint.RelativeX, lastPoint.RelativeY, targetPoint.RelativeX, targetPoint.RelativeY, flag); _square.Logger.LogInfo(msg); }
private int _step = 0; //记录步数 /// <summary> /// 构造函数 /// </summary> public Pawns(ISquare square, IChessPoint point) : base(square, point) { if (_currentPoint.Y > 4) { _step = -1; } else { _step = 1; } }
/// <summary> /// 棋子是否能够移动到目标点 /// </summary> protected override bool CanMoveTo(IChessPoint targetPoint) { //两点在一条直线上 if (targetPoint.X != _currentPoint.X && targetPoint.Y != _currentPoint.Y) { return(false); } //中间不能有棋子 if (GetChessCount(_currentPoint, targetPoint) > 0) { return(false); } return(true); }
/// <summary> /// 兵移动算法 /// </summary> protected override bool CanMoveTo(IChessPoint tragPoint) { //“士”不能出城 if (!((tragPoint.X >= 3 && tragPoint.X <= 5) && (tragPoint.Y <= 2 || tragPoint.Y >= 7))) { return(false); } //“士”每次只能走一步且只能是斜线 if (!(Math.Abs(tragPoint.X - this._currentPoint.X) == 1 && Math.Abs(tragPoint.Y - this._currentPoint.Y) == 1)) { return(false); } return(true); }
IChess ITable.this[IChessPoint chessPoint] { get { var key = _dict.Keys.FirstOrDefault(k => k.X == chessPoint.X && k.Y == chessPoint.Y); return(key != null ? _dict[key] : null); } set { var key = _dict.Keys.FirstOrDefault(k => k.X == chessPoint.X && k.Y == chessPoint.Y); key = key != null ? key : chessPoint; _dict[key] = value; if (value == null)//吃掉或移动 { _dict.Remove(key); } } }
/// <summary> /// 马移动算法 /// </summary> protected override bool CanMoveTo(IChessPoint p) { //蹩脚算法 //横向移动 if (Math.Abs(_currentPoint.X - p.X) == 2 && Math.Abs(_currentPoint.Y - p.Y) == 1) { if (Table[(_currentPoint.X + p.X) / 2, _currentPoint.Y] == null) { return(true); } } if (Math.Abs(_currentPoint.X - p.X) == 1 && Math.Abs(_currentPoint.Y - p.Y) == 2) { if (Table[_currentPoint.X, (_currentPoint.Y + p.Y) / 2] == null) { return(true); } } return(false); }
/// <summary> /// 棋子是否能够移动到目标点 /// </summary> protected override bool CanMoveTo(IChessPoint targPoint) { //两点在一条直线上 if (targPoint.X != _currentPoint.X && targPoint.Y != _currentPoint.Y) { return(false); } //目标点不为空,中间只能由一个棋子 if (Table[targPoint] != null && GetChessCount(_currentPoint, targPoint) != 1) { return(false); } //目标点为空,中间不能有棋子 if (Table[targPoint] == null && GetChessCount(_currentPoint, targPoint) > 0) { return(false); } return(true); }
/// <summary> /// 兵移动算法 /// </summary> protected override bool CanMoveTo(IChessPoint p) { //兵不能往后走 if (p.Y - _currentPoint.Y == -_step) { return(false); } //兵一步一步走 if (Math.Abs(_currentPoint.X - p.X) + Math.Abs(_currentPoint.Y - p.Y) != 1) { return(false); } //过河 if (!_isRiverd) { if (_currentPoint.Y == 4 && p.Y == 5) { _isRiverd = true; } if (_currentPoint.Y == 5 && p.Y == 4) { _isRiverd = true; } } //过河前只能向前走 if (!_isRiverd) { if (p.Y - _currentPoint.Y != _step) { return(false); } } return(true); }
/// <summary> /// 获取两点之间的棋子数 /// </summary> public int GetChessCount(IChessPoint start, IChessPoint end) { //如果Y相同 if (start.Y == end.Y) { //获取最大X和最小X值 int min = Math.Min(start.X, end.X); int max = Math.Max(start.X, end.X); //棋子计数器 int count = 0; for (int i = min + 1; i < max; i++) { if (Table[i, start.Y] != null) { count++; } } return(count); } else { int min = Math.Min(start.Y, end.Y); int max = Math.Max(start.Y, end.Y); int count = 0; for (int i = min + 1; i < max; i++) { if (Table[start.X, i] != null) { count++; } } return(count); } }
public Chess(ISquare square, IChessPoint point) { _square = square; this._color = square.Color; this._currentPoint = point; }
/// <summary> /// 构造函数 /// </summary> public Rooks(ISquare square, IChessPoint point) : base(square, point) { // }
/// <summary> /// 构造函数 /// </summary> public Cannons(ISquare square, IChessPoint point) : base(square, point) { }
/// <summary> /// 构造函数 /// </summary> public Elephants(ISquare square, IChessPoint point) : base(square, point) { // }
/// <summary> /// 构造函数 /// </summary> public King(ISquare square, IChessPoint point) : base(square, point) { }
/// <summary> /// 构造函数 /// </summary> public Mandarins(ISquare square, IChessPoint point) : base(square, point) { // }
/// <summary> /// 是否能够移动 /// </summary> protected abstract bool CanMoveTo(IChessPoint p);
/// <summary> /// 构造函数 /// </summary> public Knights(ISquare square, IChessPoint point) : base(square, point) { // }