public override void Reply(CChatMaster chatMaster, CWindow window, CMessage message)
        {
            if (!(bool)window.PermaVariables.GetVariable("AcceptInput")) {
                return;
            }
            if (string.IsNullOrWhiteSpace(message.Message)) {
                return;
            }
            string sSwitch = "";
            string[] sTokens;

            if (!message.Message.Contains(" ")) {
                sSwitch = message.Message;
            } else {
                sTokens = message.Message.Split(' ');
                sSwitch = sTokens[0];
            }

            switch (sSwitch.ToLower()) {
                case "start":
                    Start(chatMaster, window);
                    break;

                case "restart":
                    Restart(chatMaster, window);
                    break;

                case "stop":
                    Close(chatMaster, window);
                    break;

                default:
                    if (!(bool)window.PermaVariables.GetVariable("InGame")) {
                        chatMaster.SendMessageToWindow(window, "Hi, " + window.ChatName + ". I'm not available, but would you like a game of hangman?"
                            + " If so, type 'start' to play!");
                    } else {
                        if (message.Message.Length > 1) {
                            if (sSwitch == new string((char[])window.PermaVariables.GetVariable("CurrentWord"))) {
                                Win(chatMaster, window);
                            } else {
                                chatMaster.SendMessageToWindow(window, "I don't know what \"" + message.Message + "\" means. If you're trying to play the game, "
                                + "please remember you can only try one letter at a time!");
                            }
                        } else {
                            TryLetter(chatMaster, window, sSwitch.ToLower()[0]);
                        }
                    }
                    return;
            }

            base.Reply(chatMaster, window, message);
        }
        public string GetProgress(CChatMaster chatMaster, CWindow window)
        {
            char[] progress = (char[])window.PermaVariables.GetVariable("Progress");
            List<char> triedLetters = (List<char>)window.PermaVariables.GetVariable("TriedLetters");

            string strResult = "";
            for (int i = 0; i < progress.Length; i++) {
                strResult += progress[i] + " ";
            }

            return GetCurrentHangman(window) +
                "\n" +
                strResult.Trim() +
                "\n"
                + string.Join(",", triedLetters.ToArray()) + "\n";
        }
 private void Win(CChatMaster chatMaster, CWindow window)
 {
     chatMaster.SendMessageToWindow(window, "Congratulations! You won! Hope you had fun! :)");
     Reset(chatMaster, window);
     window.PermaVariables.EditVariable("InGame", false);
 }
 private void Reset(CChatMaster chatMaster, CWindow window)
 {
     window.PermaVariables.EditVariable("CurrentHangman", "_______");
     window.PermaVariables.EditVariable("Stage", 0);
     window.PermaVariables.EditVariable("InGame", false);
     ((List<char>)window.PermaVariables.GetVariable("TriedLetters")).Clear();
 }
        private void TryLetter(CChatMaster chatMaster, CWindow window, char sSwitch)
        {
            if (HasTriedLetter(window, sSwitch)) {
                chatMaster.SendMessageToWindow(window, "You've already tried " + sSwitch.ToString() + "!");
                return;
            }

            char[] currentword = (char[])window.PermaVariables.GetVariable("CurrentWord");
            char[] progress = (char[])window.PermaVariables.GetVariable("Progress");

            if (currentword.Contains(sSwitch)) {
                for (int i = 0; i < currentword.Length; i++) {
                    if (currentword[i] == sSwitch) {
                        progress[i] = sSwitch;
                    }
                }
                window.PermaVariables.EditVariable("Progress", progress);

                if (new string(currentword) == new string(progress)) {
                    Win(chatMaster, window);
                    return;
                }
            } else {
                IncrementStage(chatMaster, window);
            }

            AddTriedLetter(window, sSwitch);
            chatMaster.SendMessageToWindow(window, GetProgress(chatMaster, window));
        }
        private void IncrementStage(CChatMaster chatMaster, CWindow window)
        {
            int stage = (int)window.PermaVariables.GetVariable("Stage");
            string sCurrentHangman = null;

            window.PermaVariables.EditVariable("CurrentHangman", "");
            stage++;

            if (stage == 10) {
                Death(chatMaster, window);
                return;
            }

            switch (stage) {
                case 1:
                    sCurrentHangman = "|     \n|     \n|______";
                    break;

                case 2:
                    sCurrentHangman = "|     \n|     \n|     \n|     \n|______";
                    break;

                case 3:
                    sCurrentHangman = "|/     \n|     \n|     \n|     \n|     \n|     \n|______";
                    break;

                case 4:
                    sCurrentHangman = "___\n|/     \n|     \n|     \n|     \n|     \n|     \n|______";
                    break;

                case 5:
                    sCurrentHangman = "__________\n|/     |\n|     \n|     \n|     \n|     \n|     \n|______";
                    break;

                case 6:
                    sCurrentHangman = "__________\n|/     |\n|     (_)\n|     \n|     \n|     \n|     \n|______";
                    break;

                case 7:
                    sCurrentHangman = "__________\n|/     |\n|     (_)\n|     \\|/\n|\n|\n|\n|______";
                    break;

                case 8:
                    sCurrentHangman = "__________\n|/     |\n|     (_)\n|     \\|/\n|      |\n|\n|\n|______";
                    break;

                case 9:
                    sCurrentHangman = "__________\n|/     |\n|     (_)\n|     \\|/\n|      |\n|     / \\\n|\n|______";
                    break;
            }

            window.PermaVariables.EditVariable("Stage", stage);
            window.PermaVariables.EditVariable("CurrentHangman", sCurrentHangman);
        }
 public void Close(CChatMaster chatMaster, CWindow window)
 {
     chatMaster.SendMessageToWindow(window, "Ok. I've stopped the game!");
     Reset(chatMaster, window);
     window.PermaVariables.EditVariable("InGame", false);
 }
        private void GetRandomWord(CChatMaster chatMaster, CWindow window)
        {
            SetAcceptInput(window, false);
            chatMaster.SendMessageToWindow(window, "Finding a word!");

            WebClient wc = new WebClient();
            wc.DownloadDataCompleted += delegate(object sender, DownloadDataCompletedEventArgs e) {
                this.BeginGame(chatMaster, window, CDataProcess.GetRandomWordFromRaw(e.Result));
                ((WebClient)sender).Dispose();
            };
            wc.DownloadDataAsync(new Uri("http://www.thefreedictionary.com/dictionary.htm"));
        }
 private void Death(CChatMaster chatMaster, CWindow window)
 {
     chatMaster.SendMessageToWindow(window, "Oh dear! You got hung! The word was " +
         new string((char[])window.PermaVariables.GetVariable("CurrentWord")) + ". Maybe you should try again?");
     Reset(chatMaster, window);
     window.PermaVariables.EditVariable("InGame", false);
 }
        private void BeginGame(CChatMaster chatMaster, CWindow window, string strWord)
        {
            char[] word = strWord.ToCharArray();
            char[] progress = new char[word.Length];

            window.PermaVariables.EditVariable("CurrentWord", word);

            for (int i = 0; i < progress.Length; i++) {
                progress[i] = '_';
            }

            window.PermaVariables.EditVariable("Progress", progress);
            window.PermaVariables.EditVariable("InGame", true);
            SetAcceptInput(window, true);
            chatMaster.SendMessageToWindow(window, "Remember you can stop at any time by typing 'stop'.\n" + GetProgress(chatMaster, window));
        }
 public void Start(CChatMaster chatMaster, CWindow window)
 {
     Reset(chatMaster, window);
     GetRandomWord(chatMaster, window);
 }
        public void Restart(CChatMaster chatMaster, CWindow window)
        {
            chatMaster.SendMessageToWindow(window, "Ok. I'll start a new game!");
            window.PermaVariables.EditVariable("InGame", false);

            Start(chatMaster, window);
        }