Guest User

Untitled

a guest
Jul 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. /* We used closures to mimic an OO structure in Javascript. Refreshing to see clean and elegant JS code, isn't it? */
  2. Jumble = {
  3. /* Letter distribution according to Scrabble */
  4. consonants: ['b', 'b', 'c', 'c', 'd', 'd', 'd', 'd', 'f', 'f', 'g', 'g', 'g', 'h', 'h', 'j', 'k', 'l', 'l', 'l', 'l', 'm', 'm', 'n', 'n', 'n', 'n', 'n', 'n', 'p', 'p', 'q', 'r', 'r', 'r', 'r', 'r', 'r', 's', 's', 's', 's', 't', 't', 't', 't', 't', 't', 'v', 'v', 'w', 'w', 'x', 'z'],
  5. vowels: ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y',],
  6. /* Value of each letter according to Scrabble */
  7. letterValues: {'a':1, 'b':3, 'c':3, 'd':2, 'e':1, 'f':4, 'g':2, 'h':4, 'i':1, 'j':8, 'k':5, 'l':1, 'm':3, 'n':1, 'o':1, 'p':3, 'q':10, 'r':1, 's':1, 't':1, 'u':1, 'v':4, 'w':4, 'x':8, 'y':4, 'z':10},
  8. score: 0,
  9. /* Is the game in progress? */
  10. on: false,
  11. timeLeft: 60,
  12. /* Words entered */
  13. words: [],
  14. /* List of words */
  15. dictionary: '',
  16.  
  17. start: function() {
  18. /* Choose 2 random vowels */
  19. for(i = 0; i < 2; i++) {
  20. var randomVowelIndex = Math.floor(Math.random() * this.vowels.length);
  21. if($('#stand-letters > #' + this.vowels[randomVowelIndex]).length == 1) { // If duplicate, generate new ID
  22. var newID = this.vowels[randomVowelIndex] + ($('img[id^=' + this.vowels[randomVowelIndex] + ']').length + 1);
  23. $('#' + this.vowels[randomVowelIndex]).clone().attr('id', newID).appendTo('#stand-letters');
  24. } else {
  25. $('#' + this.vowels[randomVowelIndex]).appendTo('#stand-letters');
  26. }
  27. }
  28. /* Choose 4 random consonants */
  29. for(var z = 0; z < 4; z++) {
  30. var randomConsonantIndex = Math.floor(Math.random() * this.consonants.length);
  31. if($('#stand-letters > #' + this.consonants[randomConsonantIndex]).length == 1) { // If duplicate, generate new ID
  32. var newID = this.consonants[randomConsonantIndex] + ($('img[id^=' + this.consonants[randomConsonantIndex] + ']').length + 1);
  33. $('#' + this.consonants[randomConsonantIndex]).clone().attr('id', newID).appendTo('#stand-letters');
  34. } else {
  35. $('#' + this.consonants[randomConsonantIndex]).appendTo('#stand-letters');
  36. }
  37. }
  38.  
  39. this.on = true;
  40. $('button:disabled').each(function() { // Enable buttons
  41. $(this).removeAttr('disabled');
  42. } );
  43. $('#start').attr('disabled', 'disabled');
  44. this.timer(); // Start the timer
  45. },
  46.  
  47. end: function() {
  48. this.on = false; // Turn game off
  49. $('button').each(function() { // Disable buttons
  50. $(this).attr('disabled', 'disabled');
  51. } );
  52. $('#container').slideUp('slow'); // Hide game
  53. setTimeout(function() { $('#score-form').slideDown('slow'); }, 2000); // Show score form
  54. },
  55.  
  56. timer: function() {
  57. if(this.timeLeft == 0) {
  58. this.end();
  59. } else {
  60. if(this.timeLeft <= 10) { // If time is < 10, change it to red
  61. $('#time-number').css({'color' : 'red'});
  62. }
  63. this.timeLeft--;
  64. $('#time-number').html(this.timeLeft);
  65. setTimeout('Jumble.timer();', 1000);
  66. }
  67. },
  68.  
  69. /* Is the word listed in the dictionary? */
  70. isWordValid: function(word) {
  71. if(this.dictionary.indexOf(word) != -1) {
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. },
  77.  
  78. updateScore: function(points) {
  79. this.score += points;
  80. $('#score-number').html(this.score);
  81. $('#points').html('+' + points); // Show points from word w/ animation
  82. $('#points').animate( {
  83. top: "-=50px", opacity: 0
  84. }, 2000, function() {
  85. $('#points').html(''); // Clear
  86. $('#points').css({opacity : '', top: ''}); // Reset
  87. } );
  88. },
  89.  
  90. /* Helper function to show errors */
  91. showError: function(error) {
  92. $('#error').html(error).fadeIn('slow').animate({delay: 1}, 2000).fadeOut('slow');
  93. },
  94.  
  95. /* Move letter from stand to board and vice versa */
  96. moveLetter: function(letter) {
  97. if(!this.on) { return; }
  98. if($('#' + letter).parent().attr('id') == 'stand-letters') {
  99. $('#' + letter).appendTo('#board-letters');
  100. } else {
  101. $('#' + letter).appendTo('#stand-letters');
  102. }
  103. },
  104.  
  105. submitScore: function() {
  106. if($('#name').val() == '') {
  107. this.showError('You forgot to enter your name.');
  108. return
  109. }
  110.  
  111. $('#new').removeAttr('disabled');
  112.  
  113. $('#score-form').slideUp('slow');
  114. $.post('post-score.php', { // Post score to SQL database
  115. name: $('#name').val(),
  116. score: this.score
  117. }, function(data) {
  118. $('#message').prepend(data).fadeIn('slow');
  119. } );
  120. },
  121.  
  122. clearBoard: function() {
  123. var self = this;
  124. $('#board-letters > img').each(function(image) {
  125. var letter = $(this).attr('id');
  126. self.moveLetter(letter);
  127. } );
  128. },
  129.  
  130. submit: function() {
  131. if($('#board-letters >img').length == 0) { return; }
  132. var word = '';
  133. var points = 0;
  134. var self = this;
  135. $('#board-letters > img').each(function(image) {
  136. var id = $(this).attr('id');
  137. var letter = $(this).attr('id')[0];
  138. word = word + letter;
  139. points += self.letterValues[letter];
  140. self.moveLetter(id);
  141. } );
  142.  
  143. if(this.words.indexOf(word) != -1) {
  144. this.showError('You have already guessed that word!');
  145. } else if((this.isWordValid(word)) && (word.length != 1)) {
  146. this.words.push(word);
  147. this.updateScore(points);
  148. } else {
  149. this.showError('Sorry, that is not a valid word!');
  150. }
  151. }
  152.  
  153. }
  154.  
  155. /* Event handlers */
  156. $(document).ready(function() {
  157. /* Read database */
  158. $.get('./words.txt', function(database) {
  159. Jumble.dictionary = database.split('\n');
  160. } );
  161. } );
Add Comment
Please, Sign In to add comment