コード例 #1
0
        public void MovePiece(CellId source, CellId target)
        {
            if (!IsValidMove(source, target))
            {
                throw new InvalidOperationException("Invalid move!");
            }
            // Figure out if it is a castle operation.
            //
            var sourceCell = _chessboard.GetCell(source);
            var targetCell = _chessboard.GetCell(target);

            // We know it is a valid move. We just have to identify if it is a castle
            // operation.
            //
            if (sourceCell.PieceType == EChessPieceType.King &&
                targetCell.PieceType == EChessPieceType.Rook &&
                sourceCell.PieceColor == targetCell.PieceColor)
            {
                _chessboard.SwapPiece(source, target);
            }
            else
            {
                _chessboard.MovePiece(source, target);
            }
            nextTurn();
        }
コード例 #2
0
        public void MovePiece(CellId source, CellId target)
        {
            if (!IsValidMove(source, target))
            {
                throw new InvalidOperationException("Invalid move!");
            }
            // Figure out if it is a castle operation.
            //
            var sourceCell = _chessboard.GetCell(source);
            var targetCell = _chessboard.GetCell(target);

            // We know it is a valid move. We just have to identify if it is a castle
            // operation.
            //
            if (sourceCell.PieceType == EChessPieceType.King &&
                targetCell.PieceType == EChessPieceType.Rook &&
                sourceCell.PieceColor == targetCell.PieceColor)
            {
                _chessboard.SwapPiece(source, target);
            }
            else
            {
                if (targetCell.PieceColor != sourceCell.PieceColor) // The player captured a piece
                {
                    PieceCaptured(this, new PieceCapturedEventArgs(targetCell.PieceType, targetCell.PieceColor));
                    // Check if the king was captured.
                    //
                    if (targetCell.PieceType == EChessPieceType.King)
                    {
                        GameFinished(this, new GameEndedEventArgs(ActivePlayer));
                    }
                }
                _chessboard.MovePiece(source, target);
            }
            nextTurn();
        }