- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Diagnostics;
- namespace Quiz_1._0
- {
- class Program
- {
- static void Main(string[] args)
- {
- /* verifica os temas
- * pergunta o tema
- * fazer as perguntas
- * mostra a quantidade de respostas
- * *encaixar o contador
- *
- * */
- //variáveis
- string tema;
- bool exist_Tema = false;
- int modo = 0;
- Console.WriteLine("Seja bem vindo ao Quiz FTT!");
- do
- {
- do
- {
- Console.Clear();
- Console.WriteLine("O que deseja fazer?\n1-Jogar\n2-Ler as regras\n3-Sair");
- try
- {
- modo = Convert.ToInt16(Console.ReadLine());
- }
- catch
- {
- Console.WriteLine("Número inválido");
- }
- } while (modo > 4 && modo < 0);
- if (modo == 1)
- {
- do
- {
- Console.WriteLine("Nós temos os seguintes temas, qual deles deseja jogar hoje?");
- Identifica_temas();
- tema = Console.ReadLine();
- exist_Tema = Procura_tema(tema);
- if (exist_Tema == false)
- {
- Console.WriteLine("Tema inválido. Confirme se a escrita do tema está correta!");
- Console.ReadKey();
- Console.Clear();
- }
- } while (exist_Tema == false);
- Console.Clear();
- int n_perguntas = numero_perguntas(tema);
- Console.WriteLine("Serão {0} perguntas do tema {1}.\nPressione qualquer tecla para iniciar!", n_perguntas, tema);
- Console.ReadKey();
- int acertos = questionario(tema, n_perguntas);
- Console.WriteLine($"Você acertou um total de:{acertos} perguntas!");
- Console.ReadKey();
- }
- else if(modo == 2)
- {
- Console.Clear();
- 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.");
- Console.ReadKey();
- }
- } while (modo != 3);
- }
- static int questionario(string tema_pedido, int total_perguntas)
- {
- try
- {
- int cont_perguntas = 0, cont_acertos = 0;
- foreach (string linha in File.ReadLines("QUIZ.txt"))
- /* dei uma bela pesquisada e encontrei esse classe chamado File, nele tem o método ReadLines que le linha por linha o arquivo texto.
- 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.
- Ele funciona de forma bem parecida com o ReadAllText que o professor já tinha ensinado.
- **/
- {
- string[] variaveis = new string[7];
- int aux = 0;
- string auxiliar = "";
- if (linha.Contains(tema_pedido))
- /* Aqui eu usei o método Contains da biblioteca de substrings, ele vai procurar se essa string (tema_pedido) está presente na linha procurada.
- Isso deu uma bela resumida no programa que teria que procurar o tema char por char em todas as linhas.
- (Pelo menos era isso que eu estava fznd antes de achar esse método)
- **/
- {
- Console.Clear();
- for (int i = 0; i < 7; i++)
- {
- if (i == 0)
- {
- aux = linha.IndexOf("|");
- variaveis[i] = linha.Substring(0, linha.IndexOf("|"));
- }
- else if (i == 1)
- {
- auxiliar = linha.Substring(variaveis[i - 1].Length + 1);
- variaveis[i] = auxiliar.Substring(0, auxiliar.IndexOf("|"));
- aux = auxiliar.IndexOf("|");
- }
- else
- {
- auxiliar = auxiliar.Substring(variaveis[i - 1].Length + 1);
- try
- {
- variaveis[i] = auxiliar.Substring(0, auxiliar.IndexOf("|"));
- aux = auxiliar.IndexOf("|");
- }
- catch
- {
- variaveis[i] = auxiliar;
- }
- }
- }
- if (cont_perguntas < total_perguntas)
- {
- Console.WriteLine("Acertos:{0} Perguntas:{1}/{2}",cont_acertos,cont_perguntas+1 ,total_perguntas);
- 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());
- string resposta = Console.ReadLine();
- if (resposta.Trim() == variaveis[2].Trim())
- {
- Console.WriteLine("Parabéns, você acertou a pergunta!");
- cont_acertos += 1;
- Console.ReadLine();
- }
- else
- {
- Console.WriteLine("Infelizmente você errou, a resposta correta era: {0}", variaveis[2].Trim());
- Console.ReadLine();
- }
- cont_perguntas += 1;
- }
- }
- }
- return cont_acertos;
- }
- catch
- {
- Console.WriteLine("Aparentemente está faltando o arquivo de texto QUIZ na sua pasta do aplicativo, tente adiciona-lá e rodar novamente o executável!");
- Console.ReadKey();
- Environment.Exit(-1);
- return 0;
- }
- }
- static void ContagemRegressiva()
- {
- /*
- * O método Thread.Sleep() suspende o programa por uma determinada quantidade de tempo em milisegundos
- */
- for (int contagem = 15; contagem >= 0; contagem--)
- {
- Console.Write("{0} ", contagem);
- Thread.Sleep(1000);
- }
- }
- static void Identifica_temas()
- {
- int contagem_temas = 0;
- int temas_comparados = 0;
- string[] temas = new string[10];
- foreach (string linha in File.ReadLines("QUIZ.txt"))
- {
- string[] vetorAuxiliar = new string[2];
- int aux = 0;
- string auxiliar = "";
- for (int i = 0; i < 2; i++)
- {
- if (i == 0)
- {
- aux = linha.IndexOf("|");
- vetorAuxiliar[i] = linha.Substring(0, linha.IndexOf("|"));
- }
- else if (i == 1)
- {
- auxiliar = linha.Substring(vetorAuxiliar[i - 1].Length + 1);
- vetorAuxiliar[i] = auxiliar.Substring(0, auxiliar.IndexOf("|")); //vetorAuxliar[1] = ao tema
- aux = auxiliar.IndexOf("|");
- }
- }
- if (contagem_temas == 0)
- {
- temas[0] = vetorAuxiliar[1];
- contagem_temas += 1;
- }
- else
- {
- temas_comparados = 0;
- for (int i = 0; i < contagem_temas; i++)
- {
- if (vetorAuxiliar[1].Trim() == temas[i].Trim())
- break;
- else
- {
- temas_comparados += 1;
- if (temas_comparados == contagem_temas)
- {
- temas[contagem_temas] = vetorAuxiliar[1].Trim();
- contagem_temas += 1;
- break;
- }
- }
- }
- }
- }
- Console.WriteLine("\n Temas disponíveis:");
- for (int i = 0; i<contagem_temas; i++)
- {
- try
- {
- Console.WriteLine("-{0}", temas[i].Trim());
- }
- catch
- {
- Console.WriteLine("-{0}", temas[i]);
- }
- }
- }
- static bool Procura_tema(string tema)
- {
- int contagem_temas = 0;
- int temas_comparados = 0;
- string[] temas = new string[10];
- foreach (string linha in File.ReadLines("QUIZ.txt"))
- {
- string[] vetorAuxiliar = new string[2];
- int aux = 0;
- string auxiliar = "";
- for (int i = 0; i < 2; i++)
- {
- if (i == 0)
- {
- aux = linha.IndexOf("|");
- vetorAuxiliar[i] = linha.Substring(0, linha.IndexOf("|"));
- }
- else if (i == 1)
- {
- auxiliar = linha.Substring(vetorAuxiliar[i - 1].Length + 1);
- vetorAuxiliar[i] = auxiliar.Substring(0, auxiliar.IndexOf("|")); //vetorAuxliar[1] = ao tema
- aux = auxiliar.IndexOf("|");
- }
- }
- if (contagem_temas == 0)
- {
- temas[0] = vetorAuxiliar[1];
- contagem_temas += 1;
- }
- else
- {
- temas_comparados = 0;
- for (int i = 0; i < contagem_temas; i++)
- {
- if (vetorAuxiliar[1].Trim() == temas[i].Trim())
- break;
- else
- {
- temas_comparados += 1;
- if (temas_comparados == contagem_temas)
- {
- temas[contagem_temas] = vetorAuxiliar[1].Trim();
- contagem_temas += 1;
- break;
- }
- }
- }
- }
- }
- for(int i=0; i<contagem_temas; i++)
- {
- if (tema.Trim() == temas[i].Trim())
- return true;
- }
- return false;
- }
- static int numero_perguntas(string tema)
- {
- var rand = new Random();
- int perguntas_tema = 0, nPerguntas;
- foreach (string linha in File.ReadLines("QUIZ.txt"))
- {
- if(linha.Contains(tema))
- {
- perguntas_tema += 1;
- }
- }
- nPerguntas = (rand.Next(perguntas_tema/2, perguntas_tema) +rand.Next(perguntas_tema/2, perguntas_tema)) / 2;
- return nPerguntas;
- }
- }
- }