From af5d162380972fab0c26a38faf85635255e9a6ee Mon Sep 17 00:00:00 2001 From: temp Date: Sat, 12 May 2012 17:07:42 +0100 Subject: [PATCH] Added in engine javascript --- index.html | 3 +- library/js/codemachine.js | 93 +++++++++++++++++++++++++++++++++++++++ library/js/common.js | 41 +++++++++++++++-- options.json | 2 +- 4 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 library/js/codemachine.js diff --git a/index.html b/index.html index 430045d..432769b 100755 --- a/index.html +++ b/index.html @@ -63,7 +63,8 @@

code machine

Find out more on MDN! - + + diff --git a/library/js/codemachine.js b/library/js/codemachine.js new file mode 100644 index 0000000..d7f324e --- /dev/null +++ b/library/js/codemachine.js @@ -0,0 +1,93 @@ +var _debug = function( obj ) { + window.console && console.log( obj ); +}; + +var CodeGenerator = function( options ) { + + var options = { + tags : options.tags || null, + score : options.score || 10 + } + + return { + + tags : options.tags, + + score : options.score, + collection : [], + rollers : 3, + + roll : function( number ) { + this.makeCollection( number ); + this.setScore(); + _debug( this.collection ); + }, + + makeCollection : function( number ) { + this.collection = []; + var number = ++number * this.rollers; + while( --number ) { + var item = this.randomize(); + this.collection.push( item ); + } + }, + + check : function() { + + }, + + reset : function() { + this.collection = []; + this.score = options.score; + }, + + randomize : function() { + return this.tags[Math.floor(this.tags.length * Math.random())] + }, + + setScore : function( ) { + + var total = 0; + + var keys = []; + + // Dirty dirty hack + for( var i in this.collection[8] ) { + keys.push( i ); + total += this.collection[8][i]; + _debug( i ); + }; + + for( var i in this.collection[18] ) { + keys.push( i ); + total += this.collection[18][i]; + _debug( i ); + }; + + for( var i in this.collection[28] ) { + keys.push( i ); + total += this.collection[28][i]; + _debug( i ); + }; + // end dirty hack, have a cry, try to forget it! + + + if( total == 3 ) { + this.score++; + + // if we have all the same then extra point!! + if( ( keys[0] == keys[1] ) && ( keys[1] == keys[2] ) ) { + this.score += 2; + } + + } else { + this.score--; + } + + return this.score; + + } + + }; + +}; \ No newline at end of file diff --git a/library/js/common.js b/library/js/common.js index 3cd7357..04d591f 100755 --- a/library/js/common.js +++ b/library/js/common.js @@ -1,3 +1,5 @@ +var game; + var roller = (function() { var $start, @@ -25,17 +27,26 @@ var roller = (function() { $roller.each(function() { + var start = 0; + var stop = 0; + // give each a different animation switch(numRunning) { case 0: $(this).addClass('medium'); + start = 0; + stop = 9; break; case 1: $(this).addClass('short'); + start = 10; + stop = 19; break; case 2: default: $(this).addClass('long'); + start = 20; + stop = 29; break; }; @@ -52,9 +63,16 @@ var roller = (function() { $(this).removeClass('short medium long finish').children().slice(0,7).remove(); var html = ''; - for (var i=0; i<7; i++) { - html += '
  • ' + Math.floor(Math.random() * 100) + '
  • '; + + for(var i=start; i'; + }; + + } + $(this).append(html); if (numRunning === 0) { @@ -72,15 +90,30 @@ var roller = (function() { $(document).ready(function() { - + + game = new CodeGenerator( { "tags" : codeGeneratorTags } ); + $start = $('#start'); $roller = $('.roller'); - + $start.on('click', function(e) { roller.start(e); + game.roll( 10 ); }); + + + /* + game = new CodeGenerator( { "tags" : codeGeneratorTags } ); + game.roll( 10 ); + _debug( 'SCORE: ' + game.score ); + */ + + + //console.log( codeGeneratorTags ); + + }); })() } diff --git a/options.json b/options.json index 1f2f814..dcd65be 100644 --- a/options.json +++ b/options.json @@ -1 +1 @@ -[{"h2": 1},{"br": 1},{"head": 1},{"body": 1},{"html": 1},{"footer": 1},{"header": 1},{"section": 1},{"img": 1},{"li": 1},{"ul": 1},{"ol": 1},{"menu": 1},{"div": 1},{"nav": 1},{"p": 1},{"q": 1},{"section": 1},{"script": 1},{"span": 1},{"table": 1},{"b": 1},{"h1": 1},{"h2.5": 0},{"foot": 0},{"fr": 0},{"weak": 0},{"z": 0},{"picture": 0},{"part": 0},{"division": 0},{"line": 0},{"page": 0},{"music": 0}] \ No newline at end of file +var codeGeneratorTags = [{"h2": 1},{"br": 1},{"head": 1},{"body": 1},{"html": 1},{"footer": 1},{"header": 1},{"section": 1},{"img": 1},{"li": 1},{"ul": 1},{"ol": 1},{"menu": 1},{"div": 1},{"nav": 1},{"p": 1},{"q": 1},{"section": 1},{"script": 1},{"span": 1},{"table": 1},{"b": 1},{"h1": 1},{"h2.5": 0},{"foot": 0},{"fr": 0},{"weak": 0},{"z": 0},{"picture": 0},{"part": 0},{"division": 0},{"line": 0},{"page": 0},{"music": 0}]; \ No newline at end of file