public IActionResult Index() { int rndNumber = GuessingGame.RandomNumber(); ViewBag.Rnd = rndNumber; HttpContext.Session.Clear(); if (string.IsNullOrEmpty(HttpContext.Session.GetString(SessionKeyRndNumber))) { HttpContext.Session.SetInt32(SessionKeyRndNumber, rndNumber); HttpContext.Session.SetString(SessionKeyTryNumber, ""); } return(View()); }
public ActionResult GuessGame(int GuessNum = 0) { List <int> Guesses = new List <int>(); List <string> OldGuesses = new List <string>(); string msg = ""; string oldmsg = ""; if (Session["Guesses"] != null) { Guesses = (List <int>)Session["Guesses"]; } Session["Guesses"] = Guesses; Guesses.Add(GuessNum); if (Session["oldGuesses"] != null) { OldGuesses = (List <string>)Session["oldGuesses"]; } Session["oldGuesses"] = OldGuesses; if (Session["Random"] != null) { if (GuessNum <= 0 || GuessNum >= 101) { msg = "You not guessing within the limit range!"; Guesses.Remove(Guesses.Last()); } else if (GuessNum == Convert.ToInt32(Session["Random"])) { msg = "You guessed Correct with " + Guesses.Count + " guesses. New number drawn."; oldmsg = "The number was " + Session["Random"] + ". It took you " + Guesses.Count + " attempts to guess right."; OldGuesses.Add(oldmsg); Session["Random"] = GuessingGame.RandomNumber(); Guesses.Clear(); } else if (GuessNum < Convert.ToInt32(Session["Random"])) { msg = "You guessed to low. You have guessed " + Guesses.Count + " times so far."; } else if (GuessNum > Convert.ToInt32(Session["Random"])) { msg = "You guessed to high. You have guessed " + Guesses.Count + " times so far."; } } Session["msg"] = msg; return(View()); }
public IActionResult Index(int guessNumber, string autoRestart) { var rndNumber = HttpContext.Session.GetInt32(SessionKeyRndNumber); var tryNumbers = HttpContext.Session.GetString(SessionKeyTryNumber); string tryingNumbers = ""; string startAgain = ""; tryNumbers = GuessingGame.TryNumbers(tryNumbers, guessNumber); HttpContext.Session.SetString(SessionKeyTryNumber, tryNumbers); GuessingGame guessingGame = new GuessingGame(); string guessResult = guessingGame.Guess(guessNumber, (int)rndNumber); if (guessResult == "Congratulation!") { tryingNumbers = "The number was " + rndNumber + " and your guessing numbers are: " + tryNumbers; // Button when finish guessing ViewBag.Restart = "Show"; // Autorestart when finish guessing startAgain = "You can now continue guessing on a new number."; int getRndNumber = GuessingGame.RandomNumber(); HttpContext.Session.SetInt32(SessionKeyRndNumber, getRndNumber); HttpContext.Session.SetString(SessionKeyTryNumber, ""); // Redirect to run HttpGet //return RedirectToAction("Index", "GuessingGame"); } ViewBag.Index = guessResult + autoRestart; ViewBag.Numbers = tryingNumbers; ViewBag.Again = startAgain; ViewBag.Rnd = rndNumber; return(View()); }
public ActionResult GuessGame() { Session["Random"] = GuessingGame.RandomNumber(); return(View()); }