Example #1
0
 // If the position is not valid (verified in the previous method - PositionIsValid),
 // it will launch an exception message.
 public void ValidatePositionException(PositionBoard position)
 {
     if (!PositionIsValid(position))
     {
         throw new BoardException("Invalid Position!");
     }
 }
Example #2
0
 // We have a board that is 8 by 8, so we cannot allow the position of the pieces to be outside these limits.
 // In this case, the line cannot be less than zero or greater than or equal to the value stipulated in the variable "lines", in this case 8.
 // The same in the case of the columns.
 // Tests whether the position is valid or not.
 public bool PositionIsValid(PositionBoard position)
 {
     if (position.Row < 0 || position.Row >= Rows || position.Column < 0 || position.Column >= Columns)
     {
         return(false);
     }
     return(true);
 }
Example #3
0
        // Checks whether a piece exists in a given position
        public bool ThereIsAPiece(PositionBoard position)
        {
            // If there is a position validation error, the error message is displayed
            ValidatePositionException(position);

            // If this is true, there is a piece in this position
            return(Piece(position) != null);
        }
Example #4
0
        // Places a piece "piece" in the position "position"(has row and column from PositionBoard).
        public void PlacePieceOnBoard(Piece piece, PositionBoard position)
        {
            // If method "ThereIsAPiece" returns true, then an error message is displayed.
            // In this case that a piece already exists in that position
            if (ThereIsAPiece(position))
            {
                throw new BoardException("There is already a piece in that position!");
            }
            // Places the piece in the matrix
            pieces[position.Row, position.Column] = piece;

            // The position of the piece is now "position"
            piece.PositionBoard = position;
        }
Example #5
0
        public Piece RemovePiece(PositionBoard position)
        {
            // There is no piece in this position to be removed.
            if (Piece(position) == null)
            {
                return(null);
            }
            // If the previous "if" passed then there is a piece in that position.
            Piece pieceToRemove = Piece(position);

            // Now the position of that piece will cease to exist, it was removed from the board.
            pieceToRemove.PositionBoard = null;
            // The position on the board is null.
            pieces[position.Row, position.Column] = null;
            return(pieceToRemove);
        }
 public bool CanMoveTo(PositionBoard position)
 {
     return(PossibleMoviments()[position.Row, position.Column]);
 }
Example #7
0
 public Piece Piece(PositionBoard position)
 {
     return(pieces[position.Row, position.Column]);
 }