Skip to content

Instantly share code, notes, and snippets.

Created April 18, 2016 06:12
Show Gist options
  • Select an option

  • Save anonymous/52dc2817cfce1e76ce82324acf3987bf to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/52dc2817cfce1e76ce82324acf3987bf to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Apr 18, 2016.
    130 changes: 130 additions & 0 deletions Guess
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,130 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Index1
    {
    public class Program
    {

    static void Main(string[] args)
    {
    Console.WriteLine("Content-type: text/html\n");
    string data = Environment.GetEnvironmentVariable("QUERY_STRING");
    var inputValue = Parse("inputNumber", data);
    var randValue = Parse("randNumber", data);
    var attempts = Parse("attempt", data);
    if (randValue == null || attempts == null)
    {
    Console.WriteLine(newGame());
    }
    else
    {
    var randNumber = Convert.ToInt32(randValue);
    var attempt = Convert.ToInt32(attempts);

    if (++attempt < 8)
    {
    try
    {
    var x = Convert.ToInt32(inputValue);

    if (x < 0 || x > 100)
    {
    Console.WriteLine(formCreate("", "Введёное значение находится вне диапазона", randNumber, attempt));
    }
    else
    {
    if (x < randNumber)
    {
    Console.WriteLine(formCreate("", "Введёное значение меньше загаданого", randNumber, attempt));
    }
    else
    {
    if (x > randNumber)
    {
    Console.WriteLine(formCreate("", "Введёное значение больше загаданного", randNumber, attempt));
    }
    else
    {
    if (x == randNumber)
    {
    Console.WriteLine(formCreate("display:none", "Вы угадали загаданное число. Поздравляем!", randNumber, attempt));
    }
    }
    }
    }
    }
    catch (Exception)
    {
    Console.WriteLine(formCreate("", "Неверное значение", randNumber, attempt));
    }
    }
    else
    {
    Console.WriteLine(formCreate("display:none", "Попытки кончились. Попробуйте ещё раз", randNumber, attempt));
    }
    }
    }




    public static string newGame()
    {
    int attempt = 1, randNumber = (new Random()).Next(1, 100);
    return formCreate("", "", randNumber, attempt);
    }
    public static string formCreate(string style, string message, int randNumber, int attempt)
    {
    return string.Format(@"<html>
    <head>
    <title>Угадай число</title>
    </head>
    </html>
    <body>
    <h1>Лабораторная №1.PHP
    <br>Введите значение от 1 до 100</h1>
    <form method=""get"">
    <input type = ""text"" value ="""" autofocus name = ""inputNumber"" style=""{0}""/>
    <input type = ""submit"" name = ""input"" value = ""Ввести значение"" style=""{0}""/><br/>
    <input type = ""button"" name = ""restart"" value = ""Начать сначала"" onclick=""location.href='Index.cgi'""/><br/>
    <input type = ""hidden"" name = ""randNumber"" value=""{1}"">
    <input type = ""hidden"" name = ""attempt"" value = ""{2}""/>
    </form>
    <p>{3}<br>Попытка №: {2}</p>
    </body>
    </html>", style, randNumber.ToString(), attempt.ToString(), message);
    }

    public static string Parse(string paramName, string queryString)
    {
    if (queryString == null || paramName == null) return null;
    var c = queryString.Split('&');
    var res = new Dictionary<String, String>();
    foreach(string h in c)
    {
    var t = h.Split('=');
    res.Add(t[0], t[1]);
    }

    return res[paramName];
    }
    /* public static int? parse(string paramName, string queryString)
    {
    if (queryString == null || paramName == null) return null;
    paramName += '=';
    var index = queryString.IndexOf(paramName);
    if (index < 0 || index + paramName.Length > queryString.Length) return null;
    index = index + paramName.Length;
    var lenght = queryString.IndexOf("&", index) - index;
    if (lenght < 0) lenght = queryString.Length - index;
    var str = queryString.Substring(index, lenght);
    int res;
    if (!int.TryParse(str, out res)) return null;
    return res;
    }*/
    }
    }