Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- string scoresDir = @"F:\Documents\Computing\Projects\File management\scores\highScores.dll";
- if (File.Exists(scoresDir) == false)
- {
- //Create one.
- using (File.Create(scoresDir)) { }
- }
- Console.WriteLine("This is a file-management based quiz application.");
- Console.WriteLine("Press ENTER to begin.");
- Console.ReadKey();
- Console.Clear();
- Console.WriteLine();
- Console.WriteLine("Please select which quiz you'd like to answer: ");
- Console.WriteLine();
- DirectoryInfo dinfo = new DirectoryInfo(@"F:\Documents\Computing\Projects\File management\");
- FileInfo[] quizzes = dinfo.GetFiles("*.txt");
- int counter = 1;
- foreach (FileInfo quiz in quizzes)
- {
- int index = quiz.Name.IndexOf(".");
- string fileName = quiz.Name.Substring(0, index);
- Console.WriteLine("[" + counter + "] " + fileName);
- counter = counter + 1;
- }
- Console.WriteLine();
- Console.Write("Selection > ");
- int quizSelected = int.Parse(Console.ReadLine());
- Console.Clear();
- string quizSelectedName = quizzes[quizSelected - 1].Name.ToString().Substring(0, quizzes[quizSelected - 1].Name.ToString().IndexOf('.'));
- Console.WriteLine();
- Console.WriteLine("Starting your selected quiz: " + quizSelectedName + "!");
- string quizDir = @"F:\Documents\Computing\Projects\File management\" + quizSelectedName + ".txt";
- Console.WriteLine("Press ENTER to start!");
- Console.ReadKey();
- // Formatting guide.
- // Files are formatted as follows:
- //
- // What is the answer to question 1?;Answer1
- // What is the answer to question 2?;Answer2
- // What is the answer to question 3?;Answer3
- StreamReader quizReader = new StreamReader(quizDir);
- int lines = File.ReadAllLines(quizDir).Count();
- int score = 0;
- for (int i = 0; i < lines; i++)
- {
- Console.Clear();
- string thisLine = quizReader.ReadLine();
- string thisQuestion = thisLine.Substring(0, thisLine.IndexOf(";"));
- string thisAnswer = thisLine.Substring(thisLine.IndexOf(";") + 1).ToLower();
- Console.WriteLine();
- Console.WriteLine("Current Score: {0}", score);
- Console.WriteLine("You've answered " + i + " questions so far.");
- Console.WriteLine();
- Console.WriteLine("------ QUESTION " + (i + 1) + " ------");
- Console.WriteLine();
- Console.WriteLine("Q > " + thisQuestion);
- Console.Write("A > ");
- string givenAnswer = Console.ReadLine().ToLower();
- if (givenAnswer == thisAnswer)
- {
- // Correct!
- score = score + 1;
- Console.WriteLine();
- Console.WriteLine("Answer correct! :)");
- }
- else
- {
- // Incorrect!
- Console.WriteLine();
- Console.WriteLine("Answer incorrect, sorry! :(");
- }
- Console.ReadKey();
- }
- Console.Clear();
- Console.WriteLine();
- Console.WriteLine("Quiz \"" + quizSelectedName + "\" completed!");
- float Percentage = ((score / lines) * 100);
- Console.WriteLine("You scored " + score + "/" + lines + " (" + Percentage + "%)!");
- Console.WriteLine();
- Console.Write("Looking to save your score? Enter \"Y\" for yes, and \"N\" for no: ");
- string gAnswer = Console.ReadLine().ToLower();
- if (gAnswer == "y")
- {
- Console.Clear();
- Console.WriteLine();
- Console.Write("Enter a name to save your score ("+Percentage+"%) under: ");
- string savedName = Console.ReadLine();
- savedName = savedName.Replace(";", "-");
- savedName = savedName.Replace(",", "-");
- // High score saving etc.
- // Format:
- //
- // Quiz name;Person1,Score
- // Quiz name;Person2,Score
- StreamReader scoresReader = new StreamReader(scoresDir);
- List<string> scoresLines = File.ReadLines(scoresDir).ToList();
- foreach (string scores in scoresLines)
- {
- if (scores.Substring(0, scores.IndexOf(";")) == quizSelectedName)
- {
- //This is the quiz being played.
- Console.WriteLine(scores);
- Console.WriteLine(scores.IndexOf(";") + 1);
- Console.WriteLine(scores.IndexOf(","));
- Console.WriteLine(scores.Substring(scores.IndexOf(";") + 1, (scores.IndexOf(",")- scores.IndexOf(";") + 1)));
- if (scores.Substring(scores.IndexOf(";") + 1, (scores.IndexOf(",") - scores.IndexOf(";") + 1)) == savedName)
- {
- //This is same playername as a previous score.
- string oldScore = scores.Substring(scores.IndexOf(",") + 1);
- if (float.Parse(oldScore) < Percentage) {
- //New highscore.
- Console.WriteLine("Congratulations! Your score of "+Percentage+"% is a new highscore for this user!");
- var newScoresList = scoresLines.Select(s => s.Replace((quizSelectedName + ";" + savedName + "," + oldScore), (quizSelectedName + ";" + savedName + "," + Percentage))).ToList();
- File.WriteAllText(scoresDir, String.Empty);
- TextWriter scoresWriter = new StreamWriter(scoresDir);
- foreach (string s in newScoresList)
- {
- scoresWriter.WriteLine(s);
- }
- scoresWriter.Close();
- }
- }
- }
- }
- }
- else if (gAnswer == "n")
- {
- }
- else
- {
- }
- // End of program.
- Console.WriteLine("Application completed. Press ENTER to exit.");
- Console.ReadKey();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement