Esempio n. 1
0
        public ActionResult GuessLetter()
        {
            char newLetter = char.Parse(Request.Form["guessLetter"]);

            if (Guess.CheckDuplicate(newLetter) == false)
            {
                Dictionary <string, string> dict = new Dictionary <string, string>();
                dict.Add("gameOver", "false");
                //make a new Guess instance and update page
                Guess newGuess = new Guess(newLetter);
                if (generator.targetWord.Contains(newLetter.ToString()))
                {
                    for (int i = 0; i < generator.targetWord.Length; i++)
                    {
                        if (generator.targetWord[i] == newLetter)
                        {
                            string first = Guess.correctLetters.Substring(0, i);
                            string last  = Guess.correctLetters.Substring(i + 1);
                            Guess.correctLetters = first + newLetter.ToString() + last;
                        }
                    }
                }
                else
                {
                    Guess.incorrectLetters += newLetter.ToString();
                    if (Guess.incorrectLetters.Length >= 6)
                    {
                        generator              = new TargetWordGenerator(wordList);
                        Guess.correctLetters   = "";
                        Guess.incorrectLetters = "";
                        Guess.guesses.Clear();
                        dict["gameOver"] = "true";
                    }
                }

                dict.Add("correctLetters", Guess.correctLetters);
                dict.Add("incorrectLetters", Guess.incorrectLetters);
                return(View("Index", dict));
            }
            else
            {
                Dictionary <string, string> dict = new Dictionary <string, string>();
                dict.Add("gameOver", "false");
                dict.Add("correctLetters", Guess.correctLetters);
                dict.Add("incorrectLetters", Guess.incorrectLetters);
                return(View("Index", dict));
            }
        }