Skip to content

Commit

Permalink
Added in engine javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
temp authored and temp committed May 12, 2012
1 parent 34ad179 commit af5d162
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 6 deletions.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ <h1>code machine</h1>
<a href="" id="more-info">Find out more on MDN!</a>
</div>


<script src="options.json"></script>
<script src="library/js/codemachine.js"></script>
<script src="library/js/jquery.min.js"></script>
<script src="library/js/modernizr.js"></script>
<script src="library/js/common.js"></script>
Expand Down
93 changes: 93 additions & 0 deletions library/js/codemachine.js
Original file line number Diff line number Diff line change
@@ -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;

}

};

};
41 changes: 37 additions & 4 deletions library/js/common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var game;

var roller = (function() {

var $start,
Expand Down Expand Up @@ -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;
};

Expand All @@ -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 += '<li>' + Math.floor(Math.random() * 100) + '</li>';

for(var i=start; i<stop; i++) {

for( var k in game.collection[i] ) {
html += '<li>' + k + '</li>';
};


}

$(this).append(html);

if (numRunning === 0) {
Expand All @@ -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 );


});
})()
}
Expand Down
2 changes: 1 addition & 1 deletion options.json
Original file line number Diff line number Diff line change
@@ -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}]
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}];

0 comments on commit af5d162

Please sign in to comment.