Facebook
From Obese Crane, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 94
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. using System.Diagnostics;
  10.  
  11. namespace Quiz_1._0
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             /*      verifica os temas
  18.              *      pergunta o tema
  19.              *      fazer as perguntas
  20.              *      mostra a quantidade de respostas
  21.              *      *encaixar o contador
  22.              *      
  23.              * */
  24.             //variáveis
  25.             string tema;
  26.             bool exist_Tema = false;
  27.             int modo = 0;
  28.  
  29.             Console.WriteLine("Seja bem vindo ao Quiz FTT!");
  30.             do
  31.             {
  32.                 do
  33.                 {
  34.                     Console.Clear();
  35.                     Console.WriteLine("O que deseja fazer?\n1-Jogar\n2-Ler as regras\n3-Sair");
  36.                     try
  37.                     {
  38.                         modo = Convert.ToInt16(Console.ReadLine());
  39.                     }
  40.                     catch
  41.                     {
  42.                         Console.WriteLine("Número inválido");
  43.                     }
  44.                 } while (modo > 4 && modo < 0);
  45.  
  46.                 if (modo == 1)
  47.                 {
  48.                     do
  49.                     {
  50.                         Console.WriteLine("Nós temos os seguintes temas, qual deles deseja jogar hoje?");
  51.                         Identifica_temas();
  52.                         tema = Console.ReadLine();
  53.                         exist_Tema = Procura_tema(tema);
  54.                         if (exist_Tema == false)
  55.                         {
  56.                             Console.WriteLine("Tema inválido. Confirme se a escrita do tema está correta!");
  57.                             Console.ReadKey();
  58.                             Console.Clear();
  59.                         }
  60.                     } while (exist_Tema == false);
  61.  
  62.                     Console.Clear();
  63.                     int n_perguntas = numero_perguntas(tema);
  64.                     Console.WriteLine("Serão {0} perguntas do tema {1}.\nPressione qualquer tecla para iniciar!", n_perguntas, tema);
  65.                     Console.ReadKey();
  66.  
  67.                     int acertos = questionario(tema, n_perguntas);
  68.                     Console.WriteLine($"Você acertou um total de:{acertos} perguntas!");
  69.                     Console.ReadKey();
  70.                 }
  71.                 else if(modo == 2)
  72.                 {
  73.                     Console.Clear();
  74.                     Console.WriteLine("1-Você deve escolher um dos temas mostrados para iniciar;\n2-O computador escolherá quantas perguntas estarão em jogo;\n3-Você deve escolher a alternativa correta antes do tempo acabar;\n4-O objetivo é acertar o máximo de perguntas.");
  75.                     Console.ReadKey();
  76.                 }
  77.             } while (modo != 3);
  78.  
  79.         }
  80.         static int questionario(string tema_pedido, int total_perguntas)
  81.         {
  82.             try
  83.                 {
  84.                 int cont_perguntas = 0, cont_acertos = 0;
  85.                 foreach (string linha in File.ReadLines("QUIZ.txt"))
  86.                 /*  dei uma bela pesquisada e encontrei esse classe chamado File, nele tem o método ReadLines que le linha por linha o arquivo texto.
  87.                     Tive que usar o foreach pq ele cria a espécie de um vetor de strings com cada linha do arquivo .txt, sendo cada linha uma variável desse vetor.
  88.                     Ele funciona de forma bem parecida com o ReadAllText que o professor já tinha ensinado.
  89.                 **/
  90.  
  91.                 {
  92.                     string[] variaveis = new string[7];
  93.                     int aux = 0;
  94.                     string auxiliar = "";
  95.  
  96.  
  97.                     if (linha.Contains(tema_pedido))
  98.                     /* Aqui eu usei o método Contains da biblioteca de substrings, ele vai procurar se essa string (tema_pedido) está presente na linha procurada.
  99.                         Isso deu uma bela resumida no programa que teria que procurar o tema char por char em todas as linhas.
  100.                         (Pelo menos era isso que eu estava fznd antes de achar esse método)
  101.                     **/
  102.                     {
  103.                         Console.Clear();
  104.  
  105.                         for (int i = 0; i < 7; i++)
  106.                         {
  107.                             if (i == 0)
  108.                             {
  109.                                 aux = linha.IndexOf("|");
  110.                                 variaveis[i] = linha.Substring(0, linha.IndexOf("|"));
  111.                             }
  112.                             else if (i == 1)
  113.                             {
  114.                                 auxiliar = linha.Substring(variaveis[i - 1].Length + 1);
  115.                                 variaveis[i] = auxiliar.Substring(0, auxiliar.IndexOf("|"));
  116.                                 aux = auxiliar.IndexOf("|");
  117.                             }
  118.                             else
  119.                             {
  120.                                 auxiliar = auxiliar.Substring(variaveis[i - 1].Length + 1);
  121.                                 try
  122.                                 {
  123.                                     variaveis[i] = auxiliar.Substring(0, auxiliar.IndexOf("|"));
  124.                                     aux = auxiliar.IndexOf("|");
  125.                                 }
  126.                                 catch
  127.                                 {
  128.                                     variaveis[i] = auxiliar;
  129.                                 }
  130.                             }
  131.                         }
  132.  
  133.                         if (cont_perguntas < total_perguntas)
  134.                         {
  135.  
  136.                             Console.WriteLine("Acertos:{0}  Perguntas:{1}/{2}",cont_acertos,cont_perguntas+1 ,total_perguntas);
  137.                             Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}\nQual a resposta correta?", variaveis[0].Trim(), variaveis[3].Trim(), variaveis[4].Trim(), variaveis[5].Trim(), variaveis[6].Trim());
  138.                             string resposta = Console.ReadLine();
  139.  
  140.                                     if (resposta.Trim() == variaveis[2].Trim())
  141.                                     {
  142.                                         Console.WriteLine("Parabéns, você acertou a pergunta!");
  143.                                         cont_acertos += 1;
  144.                                         Console.ReadLine();
  145.                                
  146.                                     }
  147.                                     else
  148.                                     {
  149.                                         Console.WriteLine("Infelizmente você errou, a resposta correta era: {0}", variaveis[2].Trim());
  150.                                         Console.ReadLine();
  151.                                 }
  152.                             cont_perguntas += 1;
  153.                         }
  154.                     }
  155.                 }
  156.                 return cont_acertos;
  157.             }
  158.             catch
  159.             {
  160.                 Console.WriteLine("Aparentemente está faltando o arquivo de texto QUIZ na sua pasta do aplicativo, tente adiciona-lá e rodar novamente o executável!");
  161.                 Console.ReadKey();
  162.                 Environment.Exit(-1);
  163.                 return 0;
  164.             }
  165.  
  166.         }
  167.  
  168.         static void ContagemRegressiva()
  169.         {
  170.             /*
  171.              * O método Thread.Sleep() suspende o programa por uma determinada quantidade de tempo em milisegundos
  172.             */
  173.             for (int contagem = 15; contagem >= 0; contagem--)
  174.             {
  175.                 Console.Write("{0} ", contagem);
  176.                 Thread.Sleep(1000);
  177.             }
  178.         }
  179.  
  180.         static void Identifica_temas()
  181.         {
  182.             int contagem_temas = 0;
  183.             int temas_comparados = 0;
  184.             string[] temas = new string[10];
  185.  
  186.             foreach (string linha in File.ReadLines("QUIZ.txt"))
  187.             {
  188.                 string[] vetorAuxiliar = new string[2];
  189.                 int aux = 0;
  190.                 string auxiliar = "";
  191.  
  192.                 for (int i = 0; i < 2; i++)
  193.                 {
  194.                     if (i == 0)
  195.                     {
  196.                         aux = linha.IndexOf("|");
  197.                         vetorAuxiliar[i] = linha.Substring(0, linha.IndexOf("|"));
  198.                     }
  199.                     else if (i == 1)
  200.                     {
  201.                         auxiliar = linha.Substring(vetorAuxiliar[i - 1].Length + 1);
  202.                         vetorAuxiliar[i] = auxiliar.Substring(0, auxiliar.IndexOf("|")); //vetorAuxliar[1] = ao tema
  203.                         aux = auxiliar.IndexOf("|");
  204.                     }
  205.                 }
  206.  
  207.                 if (contagem_temas == 0)
  208.                 {
  209.                     temas[0] = vetorAuxiliar[1];
  210.                     contagem_temas += 1;
  211.                 }
  212.                 else
  213.                 {
  214.                     temas_comparados = 0;
  215.                     for (int i = 0; i < contagem_temas; i++)
  216.                     {
  217.                         if (vetorAuxiliar[1].Trim() == temas[i].Trim())
  218.                             break;
  219.                         else
  220.                         {
  221.                             temas_comparados += 1;
  222.                             if (temas_comparados == contagem_temas)
  223.                             {
  224.                                 temas[contagem_temas] = vetorAuxiliar[1].Trim();
  225.                                 contagem_temas += 1;
  226.                                 break;
  227.                             }
  228.                         }
  229.                     }
  230.                 }
  231.             }
  232.             Console.WriteLine("\n Temas disponíveis:");
  233.             for (int i = 0; i<contagem_temas; i++)
  234.             {
  235.                 try
  236.                 {
  237.                     Console.WriteLine("-{0}", temas[i].Trim());
  238.                 }
  239.                 catch
  240.                 {
  241.                     Console.WriteLine("-{0}", temas[i]);
  242.                 }
  243.             }
  244.  
  245.         }
  246.  
  247.         static bool Procura_tema(string tema)
  248.         {
  249.             int contagem_temas = 0;
  250.             int temas_comparados = 0;
  251.             string[] temas = new string[10];
  252.  
  253.             foreach (string linha in File.ReadLines("QUIZ.txt"))
  254.             {
  255.                 string[] vetorAuxiliar = new string[2];
  256.                 int aux = 0;
  257.                 string auxiliar = "";
  258.  
  259.                 for (int i = 0; i < 2; i++)
  260.                 {
  261.                     if (i == 0)
  262.                     {
  263.                         aux = linha.IndexOf("|");
  264.                         vetorAuxiliar[i] = linha.Substring(0, linha.IndexOf("|"));
  265.                     }
  266.                     else if (i == 1)
  267.                     {
  268.                         auxiliar = linha.Substring(vetorAuxiliar[i - 1].Length + 1);
  269.                         vetorAuxiliar[i] = auxiliar.Substring(0, auxiliar.IndexOf("|")); //vetorAuxliar[1] = ao tema
  270.                         aux = auxiliar.IndexOf("|");
  271.                     }
  272.                 }
  273.  
  274.                 if (contagem_temas == 0)
  275.                 {
  276.                     temas[0] = vetorAuxiliar[1];
  277.                     contagem_temas += 1;
  278.                 }
  279.                 else
  280.                 {
  281.                     temas_comparados = 0;
  282.                     for (int i = 0; i < contagem_temas; i++)
  283.                     {
  284.                         if (vetorAuxiliar[1].Trim() == temas[i].Trim())
  285.                             break;
  286.                         else
  287.                         {
  288.                             temas_comparados += 1;
  289.                             if (temas_comparados == contagem_temas)
  290.                             {
  291.                                 temas[contagem_temas] = vetorAuxiliar[1].Trim();
  292.                                 contagem_temas += 1;
  293.                                 break;
  294.                             }
  295.                         }
  296.                     }
  297.                 }
  298.             }
  299.             for(int i=0; i<contagem_temas; i++)
  300.             {
  301.                 if (tema.Trim() == temas[i].Trim())
  302.                     return true;
  303.             }
  304.             return false;
  305.         }
  306.  
  307.         static int numero_perguntas(string tema)
  308.         {
  309.             var rand = new Random();
  310.  
  311.             int perguntas_tema = 0, nPerguntas;
  312.  
  313.             foreach (string linha in File.ReadLines("QUIZ.txt"))
  314.             {
  315.  
  316.                 if(linha.Contains(tema))
  317.                 {
  318.                     perguntas_tema += 1;
  319.                 }
  320.             }
  321.  
  322.             nPerguntas = (rand.Next(perguntas_tema/2, perguntas_tema) +rand.Next(perguntas_tema/2, perguntas_tema)) / 2;
  323.             return nPerguntas;
  324.         }
  325.  
  326.     }
  327. }
  328.  
  329.