Advertisement
g-stoyanov

CSharpOperatorsGame

Dec 19th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | None | 0 0
  1. using System;
  2.  
  3. class CSharpOperatorsGame
  4. {
  5.     static string[] rightAnswer = new string[8];
  6.     static string[] calcAnswer(int levelOfOperator, int arreyIndexOfAnswer, string[,] question)
  7.     {
  8.         for (int i = 0; i < 8; i++)
  9.         {
  10.             if (levelOfOperator == int.Parse(question[1,i]))
  11.             {
  12.                 rightAnswer[arreyIndexOfAnswer] = question[0, i];
  13.                 arreyIndexOfAnswer++;
  14.             }
  15.         }
  16.         levelOfOperator--;
  17.         if (arreyIndexOfAnswer < 8)
  18.         {
  19.             calcAnswer(levelOfOperator, arreyIndexOfAnswer, question);
  20.         }
  21.         return rightAnswer;
  22.     }
  23.     static void Main()
  24.     {
  25.         string[,] dataBase =
  26.         {
  27.             {"x.y", "f(x)", "a[x]", "x++", "x--", "new", "typeof", "checked", "unchecked", "default", "delegate", "+x", "-x", "!x", "~x", "++x", "--x", "(T)x", "true", "false", "sizeof", "*", "/", "%", "+", "-", "<<", ">>", "<", "<=", ">", ">=", "is", "as", "==", "!=", "&", "^", "|", "&&", "||", "??", "?:", "=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=", "=>"},
  28.             {"14", "14", "14", "14", "14", "14", "14", "14", "14", "14", "14", "13", "13", "13", "13", "13", "13", "13", "13", "13", "13", "12", "12", "12", "11", "11", "10", "10", "9", "9", "9", "9", "9", "9", "8", "8", "7", "6", "5", "4", "3", "2", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"},
  29.             {"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"},
  30.         };
  31.         string[,] question = new string[3, 8];
  32.         string [] temp = new string [8];
  33.         string input = "";
  34.         string answer = "";
  35.         string checkString = "";
  36.         int numberOfRightToLeft = 0;
  37.         int result = 0;
  38.         Random randomOperator = new Random();
  39.         for (int y = 1; y <= 25; y++)
  40.         {
  41.             numberOfRightToLeft = 0;
  42.             for (int i = 0; i < 8; i++)
  43.             {
  44.                 int random = randomOperator.Next(0, 55);
  45.                 for (int a = 0; a < 3; a++)
  46.                 {
  47.                     question[a, i] = dataBase[a, random];
  48.                 }
  49.             }
  50.             calcAnswer(14, 0, question);
  51.             for (int i = 0; i < 8; i++)
  52.             {
  53.                 if (question[2, i] == "0")
  54.                 {
  55.                     numberOfRightToLeft++;
  56.                 }
  57.             }
  58.             for (int i = 1; i <= numberOfRightToLeft; i++)
  59.             {
  60.                 temp[8 - i] = rightAnswer[7 - numberOfRightToLeft + i];
  61.             }
  62.             for (int i = 8 - numberOfRightToLeft; i < 8; i++)
  63.             {
  64.                 rightAnswer[i] = temp[i];
  65.             }
  66.             Console.WriteLine();
  67.             Console.WriteLine("Question N{0}:\n", y);
  68.             for (int i = 0; i < 8; i++)
  69.             {
  70.                 Console.WriteLine("Operator N{0}: {1}", i + 1, question[0, i]);
  71.             }
  72.             input = "";
  73.             while (input.Length < 8)
  74.             {
  75.                 Console.WriteLine();
  76.                 Console.WriteLine("Enter your answer (example: 36812457):");
  77.                 input = Console.ReadLine();
  78.             }
  79.             answer = "";
  80.             checkString = "";
  81.             for (int i = 0; i < 8; i++)
  82.             {
  83.                 answer += rightAnswer[i];
  84.                 checkString += question[0, int.Parse(Convert.ToString(input[i])) - 1];
  85.             }
  86.             if (answer == checkString)
  87.             {
  88.                 Console.WriteLine("Correct!!!");
  89.                 result++;
  90.             }
  91.             else
  92.             {
  93.                 Console.WriteLine("Inncorrect!!!");
  94.                 Console.Write("Correct answer is: ");
  95.                 Console.Write(rightAnswer[0] + " ");
  96.                 for (int i = 1; i < 8; i++)
  97.                 {
  98.                     Console.Write(",  {0} ", rightAnswer[i]);
  99.                 }
  100.                 Console.WriteLine("\n");
  101.             }
  102.         }
  103.         Console.WriteLine();
  104.         Console.WriteLine("Your result is: " + result);
  105.         Console.ReadLine();
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement