public static List <Move> ValidMovesForPawn (Color player, Square pawn, Board board, Game game) { List <Move> moves = new List <Move>(4); Move lastMove = game.LastMove; bool lastMoveWasLong = lastMove != null && lastMove.IsLong && lastMove.To.IsOccupiedBy(player.Inverse()); int moveShift = player == Color.White ? 1 : -1; int yForLongMove = player == Color.White ? 1 : Board.c_MaxIndex - 1; int newY = pawn.Y + moveShift; Square movePosition = board.GetSquare(pawn.X, newY); if (!movePosition.IsOccupied) { moves.Add(new Move(pawn, movePosition)); if (pawn.Y == yForLongMove) { int longMoveY = newY + moveShift; Square longMovePosition = board.GetSquare(pawn.X, longMoveY); if (!longMovePosition.IsOccupied) { moves.Add(new Move(pawn, longMovePosition)); } } } bool epCapturePossible = lastMoveWasLong && lastMove.To.Y == pawn.Y; for (int attackShift = -1; attackShift <= 1; attackShift += 2) { int newX = pawn.X + attackShift; if (newX < 0 || newX > Board.c_MaxIndex) { continue; } Square newSquare = board.GetSquare(newX, newY); if (newSquare.IsOccupiedBy(player.Inverse())) { moves.Add(new Move(pawn, newSquare, true, false)); } else if (epCapturePossible && lastMove.To.X == newX) { moves.Add(new Move(pawn, newSquare, true, true)); } } return(moves); }
public IList <Move> GetAvailableMovesForPawn(Square pawn) { List <Move> moves = new List <Move>(c_MaxMovesForPawn); Move lastMove = m_Game.LastMove; bool lastMoveWasLong = lastMove != null && lastMove.IsLong && lastMove.To.IsOccupiedBy(Opponent.Color); int newY = pawn.Y + r_MoveShift; Square movePosition = Board.GetSquare(pawn.X, newY); if (!movePosition.IsOccupied) { moves.Add(new Move(pawn, movePosition)); if (pawn.Y == r_YForLongMove) { int longMoveY = newY + r_MoveShift; Square longMovePosition = Board.GetSquare(pawn.X, longMoveY); if (!longMovePosition.IsOccupied) { moves.Add(new Move(pawn, longMovePosition)); } } } bool epCapturePossible = lastMoveWasLong && lastMove.To.Y == pawn.Y; for (int attackShift = -1; attackShift <= 1; attackShift += 2) { int newX = pawn.X + attackShift; if (newX < 0 || newX > Board.c_MaxIndex) { continue; } Square newSquare = Board.GetSquare(newX, newY); if (newSquare.IsOccupiedBy(Opponent.Color)) { moves.Add(new Move(pawn, newSquare, true, false)); } else if (epCapturePossible && lastMove.To.X == newX) { moves.Add(new Move(pawn, newSquare, true, true)); } } return(moves); }
public Move ParseMove(string san) { if (!(san.Length == 4 || san.Length == 2)) { return(null); } char[] sanChars = san.ToCharArray(); int moveShift = CurrentPlayer.Color == Color.White ? -1 : 1; int xIndex = sanChars[1] == 'x' ? 2 : 0; int finishX = sanChars[xIndex] - 'a'; int finishY = sanChars[xIndex + 1] - '1'; int startY = finishY + moveShift; int startX = finishX; if (startX < 0 || startX > Board.c_MaxIndex || startY < 0 || startY > Board.c_MaxIndex || finishX < 0 || finishX > Board.c_MaxIndex || finishY < 0 || finishY > Board.c_MaxIndex) { return(null); } Square finishSquare = Board.GetSquare(finishX, finishY); Square startSquare = Board.GetSquare(startX, startY); if (sanChars.Length == 4 && sanChars[1] == 'x') { startX = sanChars[0] - 'a'; startSquare = Board.GetSquare(startX, startY); if (SquareOccupiedByCurrentPlayer(startX, startY)) { if (SquareOccupiedByOtherPlayer(finishX, finishY)) { return(new Move(startSquare, finishSquare, true, false)); } else if (LastMove != null && LastMove.IsLong && SquareOccupiedByOtherPlayer(finishX, startY)) { return(new Move(startSquare, finishSquare, true, true)); } } return(null); } if (SquareOccupiedByCurrentPlayer(startX, startY)) { return(new Move(startSquare, finishSquare)); } else { int possibleY = CurrentPlayer.Color == Color.White ? 1 : Board.c_MaxIndex - 1; int actualY = finishY + (moveShift << 1); if (actualY >= 0 && actualY < Board.c_MaxCoord) { startSquare = Board.GetSquare(startX, actualY); if (actualY == possibleY && startSquare.IsOccupiedBy(CurrentPlayer.Color)) { return(new Move(startSquare, finishSquare)); } } } return(null); }