/// <summary> /// 移动指定位置棋格的棋子 /// </summary> /// <param name="pair">指定的成对的位置,First值为源棋格,Second值是目标棋格</param> public void PieceMoveIn(PositionPair pair) { Enums.ActionCollection actions = new Enums.ActionCollection(); Piece piece; if (this.TryGetPiece(pair.First.Dot, out piece)) { //第1步,判断一下过路兵的格子的逻辑 if (piece is PiecePawn) { if (pair.First.Y == pair.Second.Y) { return; } } actions.Add(Enums.Action.General); //第2步,判断目标棋格中是否有棋子。 //如果有棋子,更改Action,调用PieceMoveOut方法。 if (this.ContainsPiece(pair.Second.Dot)) { actions.Add(Enums.Action.Capture); this.PieceMoveOut(pair.Second); } //第3步,调用内部方法PieceMoveIn,移动棋子,并更改FEN记录 this.PieceMoveIn(piece, actions, pair.First, pair.Second); //第4步,如上一步棋出现过“过路兵”的局面,而现在进行了新的一步棋了,故而取消上一步棋的“过路兵”局面。 if (_piecePawnByEnPassanted != null) { _piecePawnByEnPassanted.EnableEnPassanted = false; _piecePawnByEnPassanted = null; } //第5步,判断是否过路兵的局面 if (piece is PiecePawn) { #region 过路兵 //成为能被吃过路兵的“兵” if ((pair.First.Y == 1 && pair.Second.Y == 3) || (pair.First.Y == 6 && pair.Second.Y == 4)) { char pawnChar; Position tmpPos = pair.Second.ShiftWest(); if (tmpPos != null) { if (this.TryGetPiece(tmpPos.Dot, out pawnChar)) { Enums.GameSide side = char.IsLower(pawnChar) ? Enums.GameSide.Black : Enums.GameSide.White; if (this.GameSide == side) { _piecePawnByEnPassanted = piece as PiecePawn; _piecePawnByEnPassanted.EnableEnPassanted = true; } } } tmpPos = pair.Second.ShiftEast(); if (tmpPos != null) { if (this.TryGetPiece(tmpPos.Dot, out pawnChar)) { Enums.GameSide side = char.IsLower(pawnChar) ? Enums.GameSide.Black : Enums.GameSide.White; if (this.GameSide == side) { _piecePawnByEnPassanted = piece as PiecePawn; _piecePawnByEnPassanted.EnableEnPassanted = true; } } } } else { ((PiecePawn)piece).EnableEnPassanted = false; } #endregion } //第6步,移动棋子后,根据移动棋子后的局面生成Action,主要是判断另一战方的“王”是否被将军 actions.Add(this.IsCheckPieceKing(piece)); //第7步,注册棋子移动后事件 OnPieceMoveInEvent(piece, actions, pair); } }