0

I need help asking my program a series of questions. For example:

I may say "hi computer" and I want my computer to respond saying "Hi, Sir. How are you?" Then my computer would say "Fine and yourself?" and my computer would say something else.

As of now I'm using Case statements. An example of my code below:

//Kindness
            case "thank you":
            case "thank you jarvis":
            case "thanks":
            case "thanks jarvis":
                if (ranNum <= 3) { QEvent = ""; JARVIS.Speak("You're Welcome Sir"); }
                else if (ranNum <= 6) { QEvent = ""; JARVIS.Speak("Anytime"); }
                else if (ranNum <= 10) { QEvent = ""; JARVIS.Speak("No problem boss"); }
                break;
user2764826
  • 1
  • 1
  • 3

2 Answers2

1

An approach I've had good success with is to create 'Contexts', which are (nested) collections of responses and scripts. When you find a matching Context, you push that Context onto a stack, and start looking for responses in the inner Contexts. If no response matches the current set of contexts, you pop the stack and retry. If the stack is empty, you generate a default "I don't understand" response.

An interesting implementation of this could be based on the answers to this question, particularly this answer, which nicely maps the response/action pairs.

Community
  • 1
  • 1
Eric Brown
  • 13,774
  • 7
  • 30
  • 71
  • @user2764826 I put together a sample that demonstrates contexts (and reflection, anonymous types, Linq, XML, and a few other technologies) [here](http://sdrv.ms/1982IFa). It allows static string responses and programmatic responses. The obvious next step would be to support string interpolation (merging programmatic responses with string responses). – Eric Brown Sep 17 '13 at 04:57
0

A factory pattern is what you need.

The factory simply reflects on all the methods in MySpeechMethods, looks for ones with SpeechAttributes and sends back the MethodInfo to invoke.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MyApp.SpeechMethods;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var methods = new MySpeechMethods();
            MethodInfo myMethod;
            myMethod = SpeechFactory.GetSpeechMethod("Thank you");
            myMethod.Invoke(methods, null);
            myMethod = SpeechFactory.GetSpeechMethod("Say something funny");
            myMethod.Invoke(methods, null);
            myMethod = SpeechFactory.GetSpeechMethod("I said funny dammit!");
            myMethod.Invoke(methods, null);
        }
    }

    public static class SpeechFactory
    {
        private static Dictionary<string, MethodInfo> speechMethods = new Dictionary<string, MethodInfo>();
        public static MethodInfo GetSpeechMethod(string speechText)
        {
            MethodInfo methodInfo;
            var mySpeechMethods = new MySpeechMethods();
            if (speechMethods.Count == 0)
            {
                var methodNames =
                    typeof (MySpeechMethods).GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
                var speechAttributeMethods = methodNames.Where(y => y.GetCustomAttributes().OfType<SpeechAttribute>().Any());
                foreach (var speechAttributeMethod in speechAttributeMethods)
                {
                    foreach (var attribute in speechAttributeMethod.GetCustomAttributes(true))
                    {
                        speechMethods.Add(((SpeechAttribute)attribute).SpeechValue, speechAttributeMethod);
                    }
                }
                methodInfo = speechMethods[speechText];
            }
            else
            {
                methodInfo = speechMethods[speechText];
            }

            return methodInfo;
        }
    }
}

namespace MyApp.SpeechMethods
{
    public class MySpeechMethods
    {
        [Speech("Thank you")]
        [Speech("Thank you Jarvis")]
        [Speech("Thanks")]
        public void YourWelcome()
        {
            JARVIS.Speak("You're Welcome Sir"); 
        }

        [Speech("Say something funny")]
        public void SayFunny()
        {
            JARVIS.Speak("A priest, a rabbi and a cabbage walk into a bar"); 
        }

        [Speech("I said funny dammit!")]
        public void TryFunnyAgain()
        {
            JARVIS.Speak("My apologies sir."); 
        }
    }

    [System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)]
    public class SpeechAttribute : System.Attribute
    {
        public string SpeechValue { get; set; }

        public SpeechAttribute(string textValue)
        {
            this.SpeechValue = textValue;
        }
    }
}
crthompson
  • 15,653
  • 6
  • 58
  • 80