Advertisement
DevByPowerPoint

ELHC 472 Game Production and Development

Aug 2nd, 2024 (edited)
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This function checks if one string contains all words from another string, regardless of order
  2. function containsAllWords(container, contained) {
  3.     var containerWords = container.split(" ");
  4.     var containedWords = contained.split(" ");
  5.     return containedWords.every(word => containerWords.includes(word));
  6. }
  7.  
  8. // This function checks the learner's answer against alternative answers
  9. function checkAlternativeAnswers(player, learnerAnswer) {
  10.     var altAnswers = [
  11.         player.GetVar("Alt_Answer1"),
  12.         player.GetVar("Alt_Answer2"),
  13.         player.GetVar("Alt_Answer3"),
  14.         player.GetVar("Alt_Answer4")
  15.     ];
  16.    
  17.     var processedLearnerAnswer = processAnswer(learnerAnswer);
  18.    
  19.     for (var i = 0; i < altAnswers.length; i++) {
  20.         if (altAnswers[i] && altAnswers[i].trim() !== "") {
  21.             var processedAltAnswer = processAnswer(altAnswers[i]);
  22.             if (containsAllWords(processedLearnerAnswer, processedAltAnswer) ||
  23.                 containsAllWords(processedAltAnswer, processedLearnerAnswer)) {
  24.                 player.SetVar("Accuracy_Score", 90);
  25.                 player.SetVar("Contains_Correct_Answer", false);
  26.                 return true; // Match found, terminate further checks
  27.             }
  28.         }
  29.     }
  30.     return false; // No match found
  31. }
  32.  
  33. // This is the main function that compares the learner's answer to the correct answer
  34. function compareAnswers() {
  35.     var player = GetPlayer();
  36.     var learnerAnswer = player.GetVar("Learner_Answer");
  37.     var correctAnswer = player.GetVar("Correct_Answer");
  38.    
  39.     var processedLearnerAnswer = processAnswer(learnerAnswer);
  40.     var processedCorrectAnswer = processAnswer(correctAnswer);
  41.    
  42.     // Check for exact match first
  43.     if (processedLearnerAnswer === processedCorrectAnswer) {
  44.         player.SetVar("Accuracy_Score", 100);
  45.         player.SetVar("Contains_Correct_Answer", true);
  46.         return;
  47.     }
  48.    
  49.     // Check for partial match with correct answer
  50.     if (containsAllWords(processedLearnerAnswer, processedCorrectAnswer) ||
  51.         containsAllWords(processedCorrectAnswer, processedLearnerAnswer)) {
  52.         player.SetVar("Accuracy_Score", 90);
  53.         player.SetVar("Contains_Correct_Answer", true);
  54.         return;
  55.     }
  56.    
  57.     // Check against alternative answers
  58.     if (checkAlternativeAnswers(player, learnerAnswer)) {
  59.         return; // If alternative answer matched, terminate the function
  60.     }
  61.    
  62.     // Calculate partial score for incorrect answers
  63.     var partialScore = calculatePartialScore(processedLearnerAnswer, processedCorrectAnswer);
  64.     player.SetVar("Accuracy_Score", partialScore);
  65.     player.SetVar("Contains_Correct_Answer", false);
  66.    
  67.     if (partialScore === 0) {
  68.         brieflyChangeScore(player);
  69.     }
  70. }
  71.  
  72. // This function cleans up the answers for easier comparison
  73. function processAnswer(answer) {
  74.     return answer.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "").trim().toUpperCase().replace(/\s+/g, " ");
  75. }
  76.  
  77. // Function to calculate partial scores, capped at 89%
  78. function calculatePartialScore(learnerAnswer, correctAnswer) {
  79.     var learnerWords = learnerAnswer.split(" ");
  80.     var correctWords = correctAnswer.split(" ");
  81.     var matchedWords = correctWords.filter(word => learnerWords.includes(word));
  82.     var score = Math.round((matchedWords.length / correctWords.length) * 89);
  83.     return score > 0 ? score : 0; // Ensure we don't return a negative score
  84. }
  85.  
  86. // This function briefly changes the score from 0% to 1% and back
  87. function brieflyChangeScore(player) {
  88.     player.SetVar("Accuracy_Score", 1);
  89.     setTimeout(function() {
  90.         player.SetVar("Accuracy_Score", 0);
  91.     }, 50);
  92. }
  93.  
  94. // This line runs the main function when the script is executed
  95. compareAnswers();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement