Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* We used closures to mimic an object-oriented structure in Javascript. */
- Jumble = {
- /* Letter distribution according to Scrabble */
- 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'],
- 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',],
- /* Value of each letter according to Scrabble */
- 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},
- score: 0,
- /* Is the game in progress? */
- on: false,
- timeLeft: 60,
- /* Words entered */
- words: [],
- /* List of words */
- dictionary: '',
- start: function() {
- /* Choose 2 random vowels */
- for(i = 0; i < 2; i++) {
- var randomVowelIndex = Math.floor(Math.random() * this.vowels.length);
- if($('#stand-letters > #' + this.vowels[randomVowelIndex]).length == 1) { // If duplicate, generate new ID
- var newID = this.vowels[randomVowelIndex] + ($('img[id^=' + this.vowels[randomVowelIndex] + ']').length + 1);
- $('#' + this.vowels[randomVowelIndex]).clone().attr('id', newID).appendTo('#stand-letters');
- } else {
- $('#' + this.vowels[randomVowelIndex]).appendTo('#stand-letters');
- }
- }
- /* Choose 4 random consonants */
- for(var z = 0; z < 4; z++) {
- var randomConsonantIndex = Math.floor(Math.random() * this.consonants.length);
- if($('#stand-letters > #' + this.consonants[randomConsonantIndex]).length == 1) { // If duplicate, generate new ID
- var newID = this.consonants[randomConsonantIndex] + ($('img[id^=' + this.consonants[randomConsonantIndex] + ']').length + 1);
- $('#' + this.consonants[randomConsonantIndex]).clone().attr('id', newID).appendTo('#stand-letters');
- } else {
- $('#' + this.consonants[randomConsonantIndex]).appendTo('#stand-letters');
- }
- }
- this.on = true;
- $('button:disabled').each(function() { // Enable buttons
- $(this).removeAttr('disabled');
- } );
- $('#start').attr('disabled', 'disabled');
- this.timer(); // Start the timer
- },
- end: function() {
- this.on = false; // Turn game off
- $('button').each(function() { // Disable buttons
- $(this).attr('disabled', 'disabled');
- } );
- $('#container').slideUp('slow'); // Hide game
- setTimeout(function() { $('#score-form').slideDown('slow'); }, 2000); // Show score form
- },
- timer: function() {
- if(this.timeLeft == 0) {
- this.end();
- } else {
- if(this.timeLeft <= 10) { // If time is < 10, change it to red
- $('#time-number').css({'color' : 'red'});
- }
- this.timeLeft--;
- $('#time-number').html(this.timeLeft);
- setTimeout('Jumble.timer();', 1000);
- }
- },
- /* Is the word listed in the dictionary? */
- isWordValid: function(word) {
- if(this.dictionary.indexOf(word) != -1) {
- return true;
- } else {
- return false;
- }
- },
- updateScore: function(points) {
- this.score += points;
- $('#score-number').html(this.score);
- $('#points').html('+' + points); // Show points from word w/ animation
- $('#points').animate( {
- top: "-=50px", opacity: 0
- }, 2000, function() {
- $('#points').html(''); // Clear
- $('#points').css({opacity : '', top: ''}); // Reset
- } );
- },
- /* Helper function to show errors */
- showError: function(error) {
- $('#error').html(error).fadeIn('slow').animate({delay: 1}, 2000).fadeOut('slow');
- },
- /* Move letter from stand to board and vice versa */
- moveLetter: function(letter) {
- if(!this.on) { return; }
- if($('#' + letter).parent().attr('id') == 'stand-letters') {
- $('#' + letter).appendTo('#board-letters');
- } else {
- $('#' + letter).appendTo('#stand-letters');
- }
- },
- submitScore: function() {
- if($('#name').val() == '') {
- this.showError('You forgot to enter your name.');
- return
- }
- $('#new').removeAttr('disabled');
- $('#score-form').slideUp('slow');
- $.post('post-score.php', { // Post score to SQL database
- name: $('#name').val(),
- score: this.score
- }, function(data) {
- $('#message').prepend(data).fadeIn('slow');
- } );
- },
- clearBoard: function() {
- var self = this;
- $('#board-letters > img').each(function(image) {
- var letter = $(this).attr('id');
- self.moveLetter(letter);
- } );
- },
- submit: function() {
- if($('#board-letters >img').length == 0) { return; }
- var word = '';
- var points = 0;
- var self = this;
- $('#board-letters > img').each(function(image) {
- var id = $(this).attr('id');
- var letter = $(this).attr('id')[0];
- word = word + letter;
- points += self.letterValues[letter];
- self.moveLetter(id);
- } );
- if(this.words.indexOf(word) != -1) {
- this.showError('You have already guessed that word!');
- } else if((this.isWordValid(word)) && (word.length != 1)) {
- this.words.push(word);
- this.updateScore(points);
- } else {
- this.showError('Sorry, that is not a valid word!');
- }
- }
- }
- /* Event handlers */
- $(document).ready(function() {
- /* Read database */
- $.get('./words.txt', function(database) {
- Jumble.dictionary = database.split('\n');
- } );
- } );
Add Comment
Please, Sign In to add comment