// Used to add a new user to the database
 public void AddUser(string username, string password)
 {
     if (ContainsUsername(username)) // If the database already contains the supplied username
     {
         throw new Exception("Username is already in use. Please choose another."); // Do not allow the user to be added and throw an exception describing the problem
     }
     userPasswords.Add(username, password); // Add the password for the User to the database
     UserQuestions questionSet = new UserQuestions(username); // Create a new empty question set that will be used for the new user
     string[] questionsAndAnswersT1a = { "From which of these is Carbon Dioxide produced?", "Respiration", "Ingestion", "Photosynthesis" }; // Add a template question as an example. This will be changed by the player or the teacher
     string[] questionsAndAnswersT1b = { "In which of the following does Photosynthesis occur?", "Plant Cell", "Animal Cell", "Bacteria Cell" };
     string[] questionsAndAnswersT1c = { "What do plants trap in their leaves?", "Light", "Heat", "Calcium" };
     questionSet.addQuestion("Biology Tier 1", questionsAndAnswersT1a); // Add the template question the question set under the various Biology Tiers
     questionSet.addQuestion("Biology Tier 1", questionsAndAnswersT1b);
     questionSet.addQuestion("Biology Tier 1", questionsAndAnswersT1c);
     string[] questionsAndAnswersT2a = { "What is the role of Cholorphyll?", "Captures Sunlight", "Attracts Carbon Dioxide", "Attracts Insects" };
     string[] questionsAndAnswersT2b = { "What Sugar is produced in Photosynthesis?", "Glucose", "Sucrose", "Brown Sugar" };
     string[] questionsAndAnswersT2c = { "What effect does a decrease in light intensity have on a plant?", "Less Photosynthesis", "Increased Respiration", "Less Respiration" };
     questionSet.addQuestion("Biology Tier 2", questionsAndAnswersT2a);
     questionSet.addQuestion("Biology Tier 2", questionsAndAnswersT2b);
     questionSet.addQuestion("Biology Tier 2", questionsAndAnswersT2c);
     string[] questionsAndAnswersT3a = { "Which of the following does not speed up Photsynthesis?", "Increase Soil acidity", "Increase temperature", "Increase Light Intensity" };
     string[] questionsAndAnswersT3b = { "Is Sugar an input or an Output of Photosynthesis?", "Output", "Input", "Neither" };
     string[] questionsAndAnswersT3c = { "What gas is likely to be collected from a plant in bright light?", "Oxygen", "Carbon Dioxide", "Nitrogen" };
     questionSet.addQuestion("Biology Tier 3", questionsAndAnswersT3a);
     questionSet.addQuestion("Biology Tier 3", questionsAndAnswersT3b);
     questionSet.addQuestion("Biology Tier 3", questionsAndAnswersT3c);
     userQuestions.Add(username, questionSet); // Add the question to the set of user questions
     userQuestionButtonState.Add(username, new QuestionButtonState(true, false, "")); // Add the question button state for this user to the database
 }
        // Used to change the state of this database to the most up-to-date state
        public void UpdateDatabase()
        {
            // Reading QuestionButtonState from Object File

            IFormatter formatter = new BinaryFormatter(); // Used to format objects into a serialized form
            Stream questionButtonStateStream = new FileStream("UserButtonStates.db", FileMode.Open, FileAccess.Read, FileShare.None); // Used to create the object file in the desired directory
            Stream gameCompletedDetailsStream = new FileStream("GameCompletedDetails.db", FileMode.Open, FileAccess.Read, FileShare.None); // Used to create the object file in the desired directory
            if ((int)formatter.Deserialize(questionButtonStateStream) != currentVersion) // If we are reading a file which is the wrong version
            {
                throw new IOException(); // Throw an error which will stop this method from advancing
            }
            userQuestionButtonState = formatter.Deserialize(questionButtonStateStream) as Dictionary<string, QuestionButtonState>; // Change the QuestionButtonState to the one read from the object file
            questionButtonStateStream.Close(); // Close the stream used to read the object file
            if ((int)formatter.Deserialize(gameCompletedDetailsStream) != currentVersion) // If we are reading a file which is the wrong version
            {
                throw new IOException(); // Throw an error which will stop this method from advancing
            }
            userGameCompletedDetails = formatter.Deserialize(gameCompletedDetailsStream) as Dictionary<string, GameCompletedDetails>;
            gameCompletedDetailsStream.Close();

            // Reading Password Details from Binary File

            FileStream passwordStream = new FileStream("Passwords.bin", FileMode.Open, FileAccess.Read); // Create a stream used for reading from the Passwords.bin binary file
            FileStream passwordForgotDetailsStream = new FileStream("ForgotPasswordDetails.bin", FileMode.Open, FileAccess.Read); // Create a stream used for from the ForgotPasswordDetails.bin binary file
            BinaryReader binaryReader = new BinaryReader(passwordStream); // Create the Reader used to read binary files, intitially set up to read the Passwords.bin file

            while (binaryReader.PeekChar() != -1) // As long as we have not reached the end of the file
            {
                userPasswords.Add(binaryReader.ReadString(), binaryReader.ReadString()); // Read the next password (and it's associated username from the file)
            }

            passwordStream.Close(); // Close the password stream

            binaryReader = new BinaryReader(passwordForgotDetailsStream); // Set up the Binary Reader to read the ForgotPasswordDetails.bin file

            while (binaryReader.PeekChar() != -1) // As long as we have not reached the end of the file
            {
                string username = binaryReader.ReadString(); // Record the username associated with the following details
                string[] questionAndAnswer = { binaryReader.ReadString(), binaryReader.ReadString() }; // Record the details used to authenticate a user before resetting their password
                userForgotPassword.Add(username, questionAndAnswer); // Add the Forgot Password Details to the dictionary
            }

            binaryReader.Close(); // Close the Binary reader
            passwordForgotDetailsStream.Close(); // Close the forgot password details steam

            // Reading Questions from Text File

            String currentDirectory = Directory.GetCurrentDirectory();
            StreamReader questionReader = new StreamReader(currentDirectory + "\\Questions.txt"); // Set up a StreamReader to read the Questions text file
            if (!questionReader.EndOfStream) // As long as the StreamReader has not reached the end of the file
            {
                string currentUsername = questionReader.ReadLine().Substring(1), questionSetID = questionReader.ReadLine().Substring(2); // Record the initial username and set ID of the questions being read
                UserQuestions currentQuestionSet = new UserQuestions(currentUsername); // Create a new UserQuestions which we will populate for the current user

                while (!questionReader.EndOfStream) // Loop until we reach the end of the file
                {
                    string line = questionReader.ReadLine();
                    if (line[2] == '>') // If we are reading a question (and it's answers)
                    {
                        char[] splitCharacters = { '•' }; // Split the question details so that they can be recorded correctly
                        string[] details = line.Substring(3).Split(splitCharacters); // Save the detals in the correct format
                        currentQuestionSet.addQuestion(questionSetID, details); // record the question details (question and answers)
                    }
                    else if (line[1] == '>') // If we are reading the name of a question set
                    {
                        questionSetID = line.Substring(2); // Record the new ID of the next question set for this user
                    }
                    else if (line[0] == '>') // If we are reading a username
                    {
                        userQuestions.Add(currentUsername, currentQuestionSet); // Record the question set of the current user in the database
                        currentUsername = line.Substring(1); // Record the next user's username
                        currentQuestionSet = new UserQuestions(currentUsername); // Create a new UserQuestion set for the new user
                    }
                }
                userQuestions.Add(currentUsername, currentQuestionSet); // Save the last user's questions

            }

            questionReader.Close();
        }