Advertisement
Tacidus

Code.org | Unscramble

Apr 27th, 2025 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var wordList = [ // List of words separated into different difficuties.
  2.  
  3.   // EASY DIFFICULTY
  4.  
  5.   {word: "CAT", difficulty: "EASY"},
  6.   {word: "DOG", difficulty: "EASY"},
  7.   {word: "BALL", difficulty: "EASY"},
  8.   {word: "FISH", difficulty: "EASY"},
  9.   {word: "TREE", difficulty: "EASY"},
  10.   {word: "BOOK", difficulty: "EASY"},
  11.   {word: "MOON", difficulty: "EASY"},
  12.   {word: "STAR", difficulty: "EASY"},
  13.   {word: "CAKE", difficulty: "EASY"},
  14.   {word: "BIRD", difficulty: "EASY"},
  15.   {word: "SNOW", difficulty: "EASY"},
  16.   {word: "FROG", difficulty: "EASY"},
  17.   {word: "SAND", difficulty: "EASY"},
  18.   {word: "CAR", difficulty: "EASY"},
  19.   {word: "BEE", difficulty: "EASY"},
  20.   {word: "BEAR", difficulty: "EASY"},
  21.   {word: "SHIP", difficulty: "EASY"},
  22.   {word: "LAND", difficulty: "EASY"},
  23.   {word: "BOAT", difficulty: "EASY"},
  24.   {word: "APPLE", difficulty: "EASY"},
  25.   {word: "MILK", difficulty: "EASY"},
  26.   {word: "HAND", difficulty: "EASY"},
  27.   {word: "FOOT", difficulty: "EASY"},
  28.   {word: "GAME", difficulty: "EASY"},
  29.   {word: "KEY", difficulty: "EASY"},
  30.   {word: "DOOR", difficulty: "EASY"},
  31.   {word: "FARM", difficulty: "EASY"},
  32.   {word: "RAIN", difficulty: "EASY"},
  33.   {word: "POND", difficulty: "EASY"},
  34.   {word: "BACK", difficulty: "EASY"},
  35.   {word: "BARK", difficulty: "EASY"},
  36.   {word: "SALT", difficulty: "EASY"},
  37.   {word: "WIND", difficulty: "EASY"},
  38.   {word: "WORM", difficulty: "EASY"},
  39.   {word: "KING", difficulty: "EASY"},
  40.   {word: "QUEEN", difficulty: "EASY"},
  41.   {word: "SPADE", difficulty: "EASY"},
  42.   {word: "JOKE", difficulty: "EASY"},
  43.  
  44.   // MEDIUM DIFFICULTY
  45.  
  46.   {word: "TIGER", difficulty: "MEDIUM"},
  47.   {word: "DANCE", difficulty: "MEDIUM"},
  48.   {word: "BREAD", difficulty: "MEDIUM"},
  49.   {word: "GRASS", difficulty: "MEDIUM"},
  50.   {word: "CHAIR", difficulty: "MEDIUM"},
  51.   {word: "OCEAN", difficulty: "MEDIUM"},
  52.   {word: "MUSIC", difficulty: "MEDIUM"},
  53.   {word: "CANDY", difficulty: "MEDIUM"},
  54.   {word: "WATER", difficulty: "MEDIUM"},
  55.   {word: "CLOUD", difficulty: "MEDIUM"},
  56.   {word: "TABLE", difficulty: "MEDIUM"},
  57.   {word: "BRUSH", difficulty: "MEDIUM"},
  58.   {word: "EAGLE", difficulty: "MEDIUM"},
  59.   {word: "TRAIN", difficulty: "MEDIUM"},
  60.   {word: "PAPER", difficulty: "MEDIUM"},
  61.   {word: "WATCH", difficulty: "MEDIUM"},
  62.   {word: "SUGAR", difficulty: "MEDIUM"},
  63.   {word: "SLIME", difficulty: "MEDIUM"},
  64.   {word: "SMILE", difficulty: "MEDIUM"},
  65.   {word: "MONEY", difficulty: "MEDIUM"},
  66.   {word: "GREEN", difficulty: "MEDIUM"},
  67.   {word: "BLUE", difficulty: "MEDIUM"},
  68.   {word: "FIELD", difficulty: "MEDIUM"},
  69.   {word: "CLOCK", difficulty: "MEDIUM"},
  70.   {word: "SHOES", difficulty: "MEDIUM"},
  71.   {word: "STORM", difficulty: "MEDIUM"},
  72.   {word: "ISLAND", difficulty: "MEDIUM"},
  73.   {word: "TRAVEL", difficulty: "MEDIUM"},
  74.   {word: "TICKET", difficulty: "MEDIUM"},
  75.   {word: "JADE", difficulty: "MEDIUM"},
  76.   {word: "ZEBRA", difficulty: "MEDIUM"},
  77.  
  78.   // HARD DIFFICULTY
  79.  
  80.   {word: "ELEPHANT", difficulty: "HARD"},
  81.   {word: "FREEDOM", difficulty: "HARD"},
  82.   {word: "UNIVERSE", difficulty: "HARD"},
  83.   {word: "WORLD", difficulty: "HARD"},
  84.   {word: "FOREST", difficulty: "HARD"},
  85.   {word: "SYMPHONY", difficulty: "HARD"},
  86.   {word: "HOSPITAL", difficulty: "HARD"},
  87.   {word: "PICTURE", difficulty: "HARD"},
  88.   {word: "EXERCISE", difficulty: "HARD"},
  89.   {word: "WISDOM", difficulty: "HARD"},
  90.   {word: "HOLOGRAM", difficulty: "HARD"},
  91.  
  92. ];
  93.  
  94. var messages = [
  95.  
  96.   {text: "This is a good start!", amount: 1},
  97.   {text: "Three right-answer-teers!", amount: 3},
  98.   {text: "You are on a roll!", amount: 5},
  99.   {text: "This is impressive!", amount: 8},
  100.   {text: "You're really good at this..!", amount: 10},
  101.   {text: "This is my highest score ever", amount: 14},
  102.   {text: "A whole 20 words with no misses??", amount: 20},
  103.   {text: "How are you doing this?", amount: 22},
  104.   {text: "Okay this is starting to get worrying", amount: 25},
  105.   {text: "You have to be cheating.", amount: 30}
  106.  
  107.  
  108.   ];
  109.  
  110. var currentWord = ""; // Placeholder variabe for the current word chosen
  111. var scrambledWord = ""; // PLaceholder variable for the current word scrambled
  112. var hintUsed = false; // Bool value meant to hold whether or not a hint has been used
  113. var playerScore = 0; // Holds player current score
  114. var playerHighscore = 0; // Holds player highest score
  115.  
  116. // Main Screen Handlers
  117.  
  118. onEvent("mainScreenBegin", "click", function(){ // Functions runs when begin button is pressed
  119.  
  120.   setScreen("gameScreen"); // Sets screen to the game screen
  121.   updateScreen(); // Calls updatescreen() function
  122.  
  123. });
  124.  
  125. // Game Screen Handlers
  126.  
  127. function updateScreen(){ // function ran when UI update is needed
  128.  
  129.   var randomPick = Math.floor(Math.random() * wordList.length); // Picks a random word from the list of words
  130.   currentWord = wordList[randomPick].word; // Updates currentWord vaue to chosen word
  131.   scrambledWord = scrambleWord(currentWord); // Runs scrambleWord(word) function with currentWord as sent parameter.
  132.  
  133.   setText("gameScreenScrambleBox", scrambledWord); // Updates the textbox to the scrambled word
  134.   setText("gameScreenDifficulty", wordList[randomPick].difficulty); // Updates textbox to match current word difficulty
  135.  
  136.   // Series of if-statements ran in order to update the text color of word difficulty text box
  137.  
  138.   if (wordList[randomPick].difficulty == "EASY") {
  139.    
  140.     setProperty("gameScreenDifficulty", "text-color", "#17b300");
  141.    
  142.   } else if (wordList[randomPick].difficulty == "MEDIUM") {
  143.    
  144.     setProperty("gameScreenDifficulty", "text-color", "#ffcb4c");
  145.    
  146.   } else {
  147.    
  148.     setProperty("gameScreenDifficulty", "text-color", "#fb7070");
  149.    
  150.   }
  151.  
  152.   // Updates player score and player highscore text boxes
  153.  
  154.   setText("gameScreenScore", "Score: " + playerScore);
  155.   setText("gameScreenHScore", "Highscore: " + playerHighscore);
  156.  
  157.   hintUsed = false; // Resets hintUsed to false
  158.   setText("gameScreenHDisplay", ""); // Resets textbox to blank
  159.  
  160.   setText("gameScreenTextInput", ""); // Resets user input text box to blank
  161.  
  162. }
  163.  
  164. // Parameter word holds the current randomly chosen word.
  165. function scrambleWord(word){ // Function ran when sent word needs to be scrambled
  166.  
  167.   var letters = word.split(""); // Separates characters into individuals and assigns them to the letters list
  168.  
  169.   for (var i = letters.length - 1; i > 0; i--){ // Loops through all elements in letters list
  170.    
  171.     var tempIndex = Math.floor(Math.random() * (i + 1)); // Creates a temporary index bet
  172.    
  173.     var tempVariable = letters[i]; // Temporary variable meant to hold current chosen letter
  174.     letters[i] = letters[tempIndex]; // Changes the value of the letters[i] element to randomly chosen index
  175.     letters[tempIndex] = tempVariable; // Changes the value of letters[tempIndex] element to formerly updated variable
  176.    
  177.   }
  178.  
  179.   return letters.join(""); // Joins the individual characters into one chunk of characters (scrambled word)
  180.  
  181. }
  182.  
  183. function updateScore(bool){
  184.  
  185.   if (bool){
  186.    
  187.     playerScore++;
  188.    
  189.     if (playerScore > playerHighscore){
  190.      
  191.       playerHighscore = playerScore;
  192.      
  193.     }
  194.    
  195.     for (var i = 0; i < messages.length - 1; i++){
  196.      
  197.       if (messages[i].amount == playerScore){
  198.        
  199.         setText("gameMessageDisplay", messages[i].text);
  200.        
  201.       }
  202.      
  203.     }
  204.    
  205.   } else {
  206.    
  207.     playerScore = 0;
  208.    
  209.     setText("gameMessageDisplay", "");
  210.    
  211.   }
  212.  
  213. }
  214.  
  215. onEvent("gameScreenBack", "click", function(){ // Event ran when the back button is pressed
  216.  
  217.   setScreen("mainScreen"); // Sets screen to main/home screen
  218.  
  219.  
  220. });
  221.  
  222. onEvent("gameScreenSubmit", "click", function(){
  223.  
  224.   if (getText("gameScreenTextInput").toUpperCase() == currentWord || getText("gameScreenTextInput").toUpperCase() == currentWord + " " ){
  225.    
  226.     updateScore(true);
  227.    
  228.     setProperty("gameScreenTextInput", "border-color", "green");
  229.    
  230.     var currTime = getTime();
  231.     while (getTime() - currTime < 1000){}
  232.    
  233.     setProperty("gameScreenTextInput", "border-color", rgb(153, 153, 153));
  234.    
  235.     playSound("sound://category_notifications/game_notification_83.mp3");
  236.    
  237.   } else{
  238.    
  239.     updateScore(false);
  240.    
  241.     setProperty("gameScreenTextInput", "border-color", "red");
  242.    
  243.     var currTime0 = getTime();
  244.     while (getTime() - currTime0 < 1000){}
  245.    
  246.     setProperty("gameScreenTextInput", "border-color", rgb(153, 153, 153));
  247.    
  248.     playSound("sound://category_alerts/comedy_game_over_1.mp3");
  249.    
  250.   }
  251.  
  252.   updateScreen();
  253.  
  254. });
  255.  
  256. onEvent("gameScreenHint", "click", function(){ // Function ran when the hint button is pressed
  257.  
  258.   var firstLetter = currentWord[0]; // Gathers the first letter of the current word
  259.  
  260.   // Checks whether the hint has already been used on this word or not.
  261.  
  262.   if (!hintUsed){ // If hint hasnt been used provides hint and updates hint text box while setting hintUsed variable to true
  263.    
  264.     setText("gameScreenHDisplay", "THE FIRST LETTER OF THE WORD IS: " + firstLetter);
  265.    
  266.     hintUsed = true;
  267.    
  268.   } else { // If hint has been used provides warning and then updates hint text box with current hint
  269.    
  270.     setText("gameScreenHDisplay", "HINT ALREADY USED!");
  271.    
  272.     // Functions as a one second pause in the middle of the event
  273.    
  274.     var currTime = getTime();
  275.     while (getTime() - currTime < 1000){}
  276.    
  277.     setText("gameScreenHDisplay", "THE FIRST LETTER OF THE WORD IS: " + firstLetter);
  278.    
  279.   }
  280.  
  281. });
  282.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement