예제 #1
0
        /// <summary>
        /// Revert the last chess draw by restoring the game situation before the draw was made from the chess draws history.
        /// </summary>
        public void RevertLastDraw()
        {
            // make sure the chess draws stack is not empty (otherwise throw exception)
            if (_drawHistory.Count == 0)
            {
                throw new InvalidOperationException("There are no draws to be reverted. Stack is empty.");
            }

            // remove the last chess draw from chess draws history
            _drawHistory.Pop();

            // create a new chess board and apply all previous chess draws (-> this results in the situation before the last chess draw was applied)
            Board = ChessBoard.StartFormation.ApplyDraws(_drawHistory.Reverse().ToList());

            // change the side that has to draw
            SideToDraw = SideToDraw.Opponent();
        }
예제 #2
0
        // TODO: maybe rework this attibute as this is quite "quick n dirty"

        #endregion Members

        #region Methods

        /// <summary>
        /// Apply the chess draw to the current game situation on the chess board.
        /// Furthermore change the side that has to draw and store the chess draw in the chess draws history (stack).
        /// </summary>
        /// <param name="draw">The chess draw to be made</param>
        /// <param name="validate">Indicates whether the chess draw should be validated</param>
        /// <returns>boolean whether the draw could be applied</returns>
        public bool ApplyDraw(ChessDraw draw, bool validate = false)
        {
            var  lastDraw    = (_drawHistory?.Count > 0) ? (ChessDraw?)_drawHistory.Peek() : null;
            bool isDrawValid = !validate || draw.IsValid(Board, lastDraw);

            // info: Validate() throws an exception if the draw is invalid -> catch this exception and make use of the exception message
            if (isDrawValid)
            {
                // draw the chess piece
                Board = Board.ApplyDraw(draw);

                // apply the chess draw to the chess draws history
                _drawHistory.Push(draw);

                // change the side that has to draw
                SideToDraw = SideToDraw.Opponent();
            }

            return(isDrawValid);
        }