public override void Move(SquareIdentifier to) { var castelingMoves = GetCastelingMoves() .Select(s => s.Identifier) .ToList(); if (castelingMoves.Contains(to)) { var oldFile = 'a'; var newFile = 'd'; var rank = '1'; if (to.File > CurrentSquare.Identifier.File) { oldFile = 'h'; newFile = 'f'; } if (Color == Army.Black) { rank = '8'; } var oldSquareIdentifier = new SquareIdentifier(oldFile, rank); var newSquareIdentifier = new SquareIdentifier(newFile, rank); var rook = CurrentSquare.Board[oldSquareIdentifier].Piece; CurrentSquare.Board[oldSquareIdentifier].Piece = null; CurrentSquare.Board[newSquareIdentifier].Piece = rook; rook.CurrentSquare = CurrentSquare.Board[newSquareIdentifier]; } try { base.Move(to); _hasMoved = true; } finally { _possibleCastelingMoves = null; } }
protected bool IsKingInCheckAfterMove(SquareIdentifier squareIdentifier) { var board = CurrentSquare.Board; var squareBefore = CurrentSquare; var squareAfter = board[squareIdentifier]; var pieceAtNewSquare = squareAfter.Piece; CurrentSquare.Piece = null; CurrentSquare = squareAfter; squareAfter.Piece = this; var isInCheckAfterMove = board.IsInCheck(Color); squareBefore.Piece = this; squareAfter.Piece = pieceAtNewSquare; CurrentSquare = squareBefore; return isInCheckAfterMove; }
public void Move(SquareIdentifier from, SquareIdentifier to) { if (_hasStarted == false) { throw new InvalidOperationException("Game hasn't been started."); } if (_hasEnded) { throw new InvalidOperationException("Game has been ended."); } var piece = Board[from].Piece; if (piece == null) { throw new IllegalMoveException(from, to, "No piece to move at the location."); } if (piece.Color != CurrentTurn) { throw new IllegalMoveException(from, to, string.Format("It is not {0}'s turn to move.", piece.Color)); } piece.Move(to); if (HasEnded()) { OnGameEnded(); } else { CurrentTurn = CurrentTurn == Army.White ? Army.Black : Army.White; OnNewTurn(); } }
public virtual void Move(SquareIdentifier to) { if (GetPossibleMoves().Contains(to) == false) { throw new IllegalMoveException(CurrentSquare.Identifier, to, string.Format("{0} to {1} is an invalid move.", CurrentSquare.Identifier, to)); } var from = CurrentSquare.Identifier; var movedPiece = this; var capturedPiece = CurrentSquare.Board[to].Piece; CurrentSquare.Piece = null; CurrentSquare.Board[to].Piece = this; CurrentSquare.Board[to].Piece.CurrentSquare = CurrentSquare.Board[to]; CurrentSquare.Board.History.Moves.Add( new Move { From = from, To = to, MovedPiece = movedPiece, CapturedPiece = capturedPiece }); }
private void InitializePosition(string positionText, Piece piece) { var position = new SquareIdentifier(positionText); var square = this[position]; square.Identifier = position; square.Piece = piece; square.Board = this; if (piece != null) { piece.CurrentSquare = square; } }
private static IntPair ToIndecies(SquareIdentifier squareIdentifier) { int fileIndex; switch (squareIdentifier.File) { case 'a': fileIndex = 0; break; case 'b': fileIndex = 1; break; case 'c': fileIndex = 2; break; case 'd': fileIndex = 3; break; case 'e': fileIndex = 4; break; case 'f': fileIndex = 5; break; case 'g': fileIndex = 6; break; case 'h': fileIndex = 7; break; default: throw new ArgumentOutOfRangeException("squareIdentifier", "Invaid label."); } var rankIndex = Convert.ToInt32(squareIdentifier.Rank.ToString()) - 1; var indices = new IntPair(fileIndex, rankIndex); return indices; }
public bool IsOccupied(SquareIdentifier squareIdentifier) { return this[squareIdentifier].IsOccupied(); }
public bool IsFree(SquareIdentifier squareIdentifier) { return this[squareIdentifier].IsFree(); }
public Square this[SquareIdentifier squareIdentifier] { get { var indicies = ToIndecies(squareIdentifier); return _squares[indicies.Item1, indicies.Item2]; } }
public IllegalMoveException(SquareIdentifier from, SquareIdentifier to, string message) : base(message) { From = from; To = to; }
public override void Move(SquareIdentifier to) { if (GetPossibleMoves().Contains(to) == false) { throw new IllegalMoveException(CurrentSquare.Identifier, to, string.Format("{0} to {1} is an invalid move.", CurrentSquare.Identifier, to)); } var from = CurrentSquare.Identifier; var movedPiece = this; var capturedPiece = CurrentSquare.Board[to].Piece; CurrentSquare.Piece = null; var toSquare = CurrentSquare.Board[to]; if (IsEnPassantMove(toSquare)) { if (Color == Army.White) { capturedPiece = toSquare.SquareBelow.Piece; toSquare.SquareBelow.Piece = null; } else { capturedPiece = toSquare.SquareAbove.Piece; toSquare.SquareAbove.Piece = null; } } toSquare.Piece = this; toSquare.Piece.CurrentSquare = toSquare; CurrentSquare.Board.History.Moves.Add( new Move { From = from, To = to, MovedPiece = movedPiece, CapturedPiece = capturedPiece }); if (IsPromotionSquare(toSquare)) { Piece promotedPiece; switch (CurrentSquare.Board.PromotionChoice()) { case PromotionChoice.Rook: promotedPiece = new Rook(); break; case PromotionChoice.Bishop: promotedPiece = new Bishop(); break; case PromotionChoice.Knight: promotedPiece = new Knight(); break; default: promotedPiece = new Queen(); break; } promotedPiece.CurrentSquare = toSquare; toSquare.Piece = promotedPiece; } }