public PieceBishop(Enums.GameSide manSide, Position position) : base(position) { this.GameSide = manSide; switch (manSide) { case Enums.GameSide.White: this.PieceType = Enums.PieceType.WhiteBishop; break; case Enums.GameSide.Black: this.PieceType = Enums.PieceType.BlackBishop; break; } }
public PieceQueen(Enums.GameSide side, Position position) : base(position) { this.GameSide = side; switch (side) { case Enums.GameSide.White: this.PieceType = Enums.PieceType.WhiteQueen; break; case Enums.GameSide.Black: this.PieceType = Enums.PieceType.BlackQueen; break; } }
public PiecePawn(Enums.GameSide side, Position position) : base(position) { this.GameSide = side; this.EnableEnPassanted = false; switch (side) { case Enums.GameSide.White: this.PieceType = Enums.PieceType.WhitePawn; break; case Enums.GameSide.Black: this.PieceType = Enums.PieceType.BlackPawn; break; } }
public PieceKing(Enums.GameSide side, Position position) : base(position) { this.GameSide = side; this.IsMoved = false; switch (side) { case Enums.GameSide.White: this.PieceType = Enums.PieceType.WhiteKing; break; case Enums.GameSide.Black: this.PieceType = Enums.PieceType.BlackKing; break; } }
protected override Position InitPosition(Position position) { if (position == Position.Empty) { switch (this.GameSide) { case Enums.GameSide.White: return new Position(4, 1); case Enums.GameSide.Black: return new Position(4, 8); default: throw new PieceException(ExString.PositionIsEmpty); } } else return position; }
/// <summary> /// 象的基本路线(即斜向) /// </summary> internal static void BishopShift( Enums.GameSide side, ISituation situation, Position position, Positions moveInPs, Positions capturePs) { bool canContine = true; Position tgtPos = Position.Empty; canContine = true; tgtPos = position; while (canContine) { tgtPos = tgtPos.ShiftEastNorth(); canContine = Position.Shift(side, situation, tgtPos, moveInPs, capturePs); } canContine = true; tgtPos = position; while (canContine) { tgtPos = tgtPos.ShiftEastSouth(); canContine = Position.Shift(side, situation, tgtPos, moveInPs, capturePs); } canContine = true; tgtPos = position; while (canContine) { tgtPos = tgtPos.ShiftWestNorth(); canContine = Position.Shift(side, situation, tgtPos, moveInPs, capturePs); } canContine = true; tgtPos = position; while (canContine) { tgtPos = tgtPos.ShiftWestSouth(); canContine = Position.Shift(side, situation, tgtPos, moveInPs, capturePs); } }
/// <summary> /// 内部方法:移动棋子,并更改FEN记录 /// </summary> /// <param name="piece">被移动的棋子</param> /// <param name="srcPos">源棋格</param> /// <param name="tgtPos">目标棋格</param> private void PieceMoveIn(Piece piece, Enums.ActionCollection actions, Position srcPos, Position tgtPos) { switch (piece.PieceType) { #region case 王车易位,过路兵,升变 case Enums.PieceType.WhiteKing: case Enums.PieceType.BlackKing: { //TODO:在这里实现王车易位 break; } case Enums.PieceType.WhitePawn: case Enums.PieceType.BlackPawn: { //移走被吃的过路兵 if (_piecePawnByEnPassanted != null) { if (tgtPos.X == _piecePawnByEnPassanted.Position.X) { actions.Add(Enums.Action.EnPassant); PieceMoveOut(_piecePawnByEnPassanted.Position); } } //TODO:在这里实现“升变” break; } case Enums.PieceType.WhiteQueen: case Enums.PieceType.WhiteRook: case Enums.PieceType.WhiteBishop: case Enums.PieceType.WhiteKnight: case Enums.PieceType.BlackQueen: case Enums.PieceType.BlackRook: case Enums.PieceType.BlackBishop: case Enums.PieceType.BlackKnight: case Enums.PieceType.AllKings: case Enums.PieceType.AllQueens: case Enums.PieceType.AllRooks: case Enums.PieceType.AllBishops: case Enums.PieceType.AllKnights: case Enums.PieceType.AllPawns: case Enums.PieceType.All: case Enums.PieceType.None: default: break; #endregion } piece.Position = tgtPos; this.SetByPosition(srcPos.Dot, '1'); this.SetByPosition(tgtPos.Dot, Enums.FromPieceType(piece.PieceType)[0]); this.GameSide = Enums.GetOtherGameSide(this.GameSide); if (this.GameSide == Enums.GameSide.White) { this.FullMoveNumber++; } this.HalfMoveClock++; }
protected virtual void OnPieceMoveOutEvent(Piece piece, Position pos) { if (PieceMoveOutEvent != null) { PieceMoveOutEvent(piece, pos); } }
/// <summary> /// 移除指定位置棋格的棋子 /// </summary> /// <param name="pos">指定位置</param> public void PieceMoveOut(Position pos) { Piece piece = this.ActivedPieces[pos.Dot]; piece.IsCaptured = true; this.ActivedPieces.Remove(piece); this.MovedPieces.Add(piece); this.HalfMoveClock = 0; OnPieceMoveOutEvent(piece, pos);//注册棋子被俘事件 }
public static bool Shift( Enums.GameSide side, ISituation situation, Position tgtPos, Positions moveInPs, Positions capturePs) { return Shift(side, situation, tgtPos, moveInPs, capturePs, true); }
/// <summary> /// 从当前的位置移动到新位置。 /// 而后,判断该位置中是否有棋子,并根据判断结果更新两个Position集合(可移动"位置"集合,可杀棋"位置"集合)。 /// 并返回是否可以继续移动的判断。 /// </summary> /// <param name="situation">当前局面</param> /// <param name="tgtPos">目标位置</param> /// <param name="moveInPs">可移动"位置"集合</param> /// <param name="capturePs">可杀棋"位置"集合</param> /// <param name="enableToMoved">该棋格为空时,是否可以移入,一般是指“兵”,“兵”的斜向的两个棋格对于“兵”来讲能吃子不能移入。</param> /// <returns>移动到新位置后是否可以继续移动(当目标棋格中无棋子)</returns> public static bool Shift( Enums.GameSide side, ISituation situation, Position tgtPos, Positions moveInPs, Positions capturePs, bool enableToMoved) { if (tgtPos == Position.Empty) return false; char pieceChar; if (situation.TryGetPiece(tgtPos.Dot, out pieceChar)) { if (char.IsLower(pieceChar) && side == Enums.GameSide.White) capturePs.Add(tgtPos); if (char.IsUpper(pieceChar) && side == Enums.GameSide.Black) capturePs.Add(tgtPos); return false; } else { if (enableToMoved) { moveInPs.Add(tgtPos); return true; } else return false; } }