Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Tyro
  8. {
  9. /// <summary>
  10. /// A Question whose answer must contain
  11. /// </summary>
  12. public class ContainsElementQuestion : Question
  13. {
  14. /// <summary>
  15. /// The elements to be searched for within the proposed answer.
  16. /// </summary>
  17. public string[] answerElements;
  18.  
  19. /// <summary>
  20. /// Should character case be considered in the answer?
  21. /// </summary>
  22. public bool isCaseSensitive;
  23.  
  24. /// <summary>
  25. /// How many of the elements must be present in the propoised answer for it to be correct? 0 for every element.
  26. /// </summary>
  27. public int elementsRequiredInAnswer;
  28.  
  29. /// <summary>
  30. /// Creates a Questions whose answer must contain certain elements.
  31. /// </summary>
  32. /// <param name="question">The text to be displayed, the literal question being asked.</param>
  33. /// <param name="answerElements">The elements to be searched for within the proposed answer.</param>
  34. /// <param name="isCaseSensitive">Should character case be considered in the answer?</param>
  35. /// <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>
  36. public ContainsElementQuestion(string question, string[] answerElements, bool isCaseSensitive, int elementsRequiredInAnswer) : base(question)
  37. {
  38. this.answerElements = answerElements;
  39. this.isCaseSensitive = isCaseSensitive;
  40. this.elementsRequiredInAnswer = elementsRequiredInAnswer;
  41. }
  42.  
  43. /// <summary>
  44. /// Determines if the proposed answer contains enough answer elements to be correct.
  45. /// </summary>
  46. /// <param name="proposedAnswer"></param>
  47. /// <returns>Returns true if the proposed answer contains as many or more answer elements to be correct. Returns false otherwise.</returns>
  48. public override bool IsCorrect(string proposedAnswer)
  49. {
  50. // Remove case if case isn't being checked for
  51. if (!isCaseSensitive)
  52. {
  53. proposedAnswer = proposedAnswer.ToLower();
  54.  
  55. }
  56.  
  57. // Check how many answer element the proposed answer contains
  58. int correctCount = 0;
  59. foreach(string element in answerElements)
  60. {
  61. // Take case sensitivity into account
  62. string elementCased = element;
  63. if (!isCaseSensitive)
  64. {
  65. elementCased = element.ToLower();
  66. }
  67.  
  68. if (proposedAnswer.Contains(elementCased))
  69. {
  70. correctCount++;
  71. }
  72. }
  73.  
  74. // Check if we have enough elements in the proposed answer, or we have EVERY element should elementsRequiredInAnswer equal 0
  75. if (correctCount >= elementsRequiredInAnswer || (elementsRequiredInAnswer == 0 && correctCount == answerElements.Length))
  76. {
  77. return true;
  78. }
  79. else
  80. {
  81. return false;
  82. }
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement