Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Tyro
- {
- /// <summary>
- /// A Question whose answer must contain
- /// </summary>
- public class ContainsElementQuestion : Question
- {
- /// <summary>
- /// The elements to be searched for within the proposed answer.
- /// </summary>
- public string[] answerElements;
- /// <summary>
- /// Should character case be considered in the answer?
- /// </summary>
- public bool isCaseSensitive;
- /// <summary>
- /// How many of the elements must be present in the propoised answer for it to be correct? 0 for every element.
- /// </summary>
- public int elementsRequiredInAnswer;
- /// <summary>
- /// Creates a Questions whose answer must contain certain elements.
- /// </summary>
- /// <param name="question">The text to be displayed, the literal question being asked.</param>
- /// <param name="answerElements">The elements to be searched for within the proposed answer.</param>
- /// <param name="isCaseSensitive">Should character case be considered in the answer?</param>
- /// <param name="elementsRequiredInAnswer">How many of the elements must be present in the proposed answer for it to be correct? 0 for every element.</param>
- public ContainsElementQuestion(string question, string[] answerElements, bool isCaseSensitive, int elementsRequiredInAnswer) : base(question)
- {
- this.answerElements = answerElements;
- this.isCaseSensitive = isCaseSensitive;
- this.elementsRequiredInAnswer = elementsRequiredInAnswer;
- }
- /// <summary>
- /// Determines if the proposed answer contains enough answer elements to be correct.
- /// </summary>
- /// <param name="proposedAnswer"></param>
- /// <returns>Returns true if the proposed answer contains as many or more answer elements to be correct. Returns false otherwise.</returns>
- public override bool IsCorrect(string proposedAnswer)
- {
- // Remove case if case isn't being checked for
- if (!isCaseSensitive)
- {
- proposedAnswer = proposedAnswer.ToLower();
- }
- // Check how many answer element the proposed answer contains
- int correctCount = 0;
- foreach(string element in answerElements)
- {
- // Take case sensitivity into account
- string elementCased = element;
- if (!isCaseSensitive)
- {
- elementCased = element.ToLower();
- }
- if (proposedAnswer.Contains(elementCased))
- {
- correctCount++;
- }
- }
- // Check if we have enough elements in the proposed answer, or we have EVERY element should elementsRequiredInAnswer equal 0
- if (correctCount >= elementsRequiredInAnswer || (elementsRequiredInAnswer == 0 && correctCount == answerElements.Length))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement