Advertisement
Guest User

sdtgyh

a guest
Sep 17th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.82 KB | None | 0 0
  1. static void Main(string[] args)
  2.         {
  3.             string scoresDir = @"F:\Documents\Computing\Projects\File management\scores\highScores.dll";
  4.             if (File.Exists(scoresDir) == false)
  5.             {
  6.                 //Create one.
  7.                 using (File.Create(scoresDir)) { }
  8.             }
  9.             Console.WriteLine("This is a file-management based quiz application.");
  10.             Console.WriteLine("Press ENTER to begin.");
  11.             Console.ReadKey();
  12.             Console.Clear();
  13.             Console.WriteLine();
  14.             Console.WriteLine("Please select which quiz you'd like to answer: ");
  15.             Console.WriteLine();
  16.             DirectoryInfo dinfo = new DirectoryInfo(@"F:\Documents\Computing\Projects\File management\");
  17.             FileInfo[] quizzes = dinfo.GetFiles("*.txt");
  18.             int counter = 1;
  19.             foreach (FileInfo quiz in quizzes)
  20.             {
  21.                 int index = quiz.Name.IndexOf(".");
  22.                 string fileName = quiz.Name.Substring(0, index);
  23.                 Console.WriteLine("[" + counter + "] " + fileName);
  24.                 counter = counter + 1;
  25.             }
  26.             Console.WriteLine();
  27.             Console.Write("Selection > ");
  28.             int quizSelected = int.Parse(Console.ReadLine());
  29.             Console.Clear();
  30.             string quizSelectedName = quizzes[quizSelected - 1].Name.ToString().Substring(0, quizzes[quizSelected - 1].Name.ToString().IndexOf('.'));
  31.             Console.WriteLine();
  32.             Console.WriteLine("Starting your selected quiz: " + quizSelectedName + "!");
  33.             string quizDir = @"F:\Documents\Computing\Projects\File management\" + quizSelectedName + ".txt";
  34.             Console.WriteLine("Press ENTER to start!");
  35.             Console.ReadKey();
  36.  
  37.             // Formatting guide.
  38.             // Files are formatted as follows:
  39.             //
  40.             // What is the answer to question 1?;Answer1
  41.             // What is the answer to question 2?;Answer2
  42.             // What is the answer to question 3?;Answer3
  43.  
  44.             StreamReader quizReader = new StreamReader(quizDir);
  45.             int lines = File.ReadAllLines(quizDir).Count();
  46.             int score = 0;
  47.             for (int i = 0; i < lines; i++)
  48.             {
  49.                 Console.Clear();
  50.                 string thisLine = quizReader.ReadLine();
  51.                 string thisQuestion = thisLine.Substring(0, thisLine.IndexOf(";"));
  52.                 string thisAnswer = thisLine.Substring(thisLine.IndexOf(";") + 1).ToLower();
  53.                 Console.WriteLine();
  54.                 Console.WriteLine("Current Score: {0}", score);
  55.                 Console.WriteLine("You've answered " + i + " questions so far.");
  56.                 Console.WriteLine();
  57.                 Console.WriteLine("------ QUESTION " + (i + 1) + " ------");
  58.                 Console.WriteLine();
  59.                 Console.WriteLine("Q > " + thisQuestion);
  60.                 Console.Write("A > ");
  61.                 string givenAnswer = Console.ReadLine().ToLower();
  62.                 if (givenAnswer == thisAnswer)
  63.                 {
  64.                     // Correct!
  65.                     score = score + 1;
  66.                     Console.WriteLine();
  67.                     Console.WriteLine("Answer correct! :)");
  68.                 }
  69.                 else
  70.                 {
  71.                     // Incorrect!
  72.                     Console.WriteLine();
  73.                     Console.WriteLine("Answer incorrect, sorry! :(");
  74.                 }
  75.                 Console.ReadKey();
  76.             }
  77.             Console.Clear();
  78.             Console.WriteLine();
  79.             Console.WriteLine("Quiz \"" + quizSelectedName + "\" completed!");
  80.             float Percentage = ((score / lines) * 100);
  81.             Console.WriteLine("You scored " + score + "/" + lines + " (" + Percentage + "%)!");
  82.             Console.WriteLine();
  83.             Console.Write("Looking to save your score? Enter \"Y\" for yes, and \"N\" for no: ");
  84.             string gAnswer = Console.ReadLine().ToLower();
  85.             if (gAnswer == "y")
  86.             {
  87.                 Console.Clear();
  88.                 Console.WriteLine();
  89.                 Console.Write("Enter a name to save your score ("+Percentage+"%) under: ");
  90.                 string savedName = Console.ReadLine();
  91.                 savedName = savedName.Replace(";", "-");
  92.                 savedName = savedName.Replace(",", "-");
  93.                 // High score saving etc.
  94.                 // Format:
  95.                 //
  96.                 // Quiz name;Person1,Score
  97.                 // Quiz name;Person2,Score
  98.                 StreamReader scoresReader = new StreamReader(scoresDir);
  99.                 List<string> scoresLines = File.ReadLines(scoresDir).ToList();
  100.                 foreach (string scores in scoresLines)
  101.                 {
  102.                     if (scores.Substring(0, scores.IndexOf(";")) == quizSelectedName)
  103.                     {
  104.                         //This is the quiz being played.
  105.                         Console.WriteLine(scores);
  106.                         Console.WriteLine(scores.IndexOf(";") + 1);
  107.                         Console.WriteLine(scores.IndexOf(","));
  108.                         Console.WriteLine(scores.Substring(scores.IndexOf(";") + 1, (scores.IndexOf(",")- scores.IndexOf(";") + 1)));
  109.                         if (scores.Substring(scores.IndexOf(";") + 1, (scores.IndexOf(",") - scores.IndexOf(";") + 1)) == savedName)
  110.                         {
  111.                             //This is same playername as a previous score.
  112.                             string oldScore = scores.Substring(scores.IndexOf(",") + 1);
  113.                             if (float.Parse(oldScore) < Percentage) {
  114.                                 //New highscore.
  115.                                 Console.WriteLine("Congratulations! Your score of "+Percentage+"% is a new highscore for this user!");
  116.                                 var newScoresList = scoresLines.Select(s => s.Replace((quizSelectedName + ";" + savedName + "," + oldScore), (quizSelectedName + ";" + savedName + "," + Percentage))).ToList();
  117.                                 File.WriteAllText(scoresDir, String.Empty);
  118.                                 TextWriter scoresWriter = new StreamWriter(scoresDir);
  119.                                 foreach (string s in newScoresList)
  120.                                 {
  121.                                     scoresWriter.WriteLine(s);
  122.                                 }
  123.                                 scoresWriter.Close();
  124.                             }
  125.                         }
  126.                     }
  127.                 }
  128.             }
  129.             else if (gAnswer == "n")
  130.             {
  131.  
  132.             }
  133.             else
  134.             {
  135.  
  136.             }
  137.             // End of program.
  138.             Console.WriteLine("Application completed. Press ENTER to exit.");
  139.             Console.ReadKey();
  140.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement