;(function( window ) {
//'use strict';
/**
* Extend object function
*
*/
function extend( a, b ) {
for( var key in b ) {
if( b.hasOwnProperty( key ) ) {
a[key] = b[key];
}
}
return a;
}
/**
* Shuffle array function
*
*/
function shuffle(o) {
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
/**
* Memory constructor
*
*/
function Memory( options, level, nbcase ) {
//console.log(nbcase);
//console.log("bob");
this.options = extend( {}, this.options );
extend( this.options, options );
this._init(level, nbcase);
}
/**
* Memory _init - initialise Memory
*
* Creates all the game content areas, adds the id's and classes, and gets
* ready for game setup.
*/
Memory.prototype._init = function(level,nbcase) {
this.game = document.createElement("div");
this.game.id = "mg";
this.game.className = "mg";
document.getElementById(this.options.wrapperID).appendChild(this.game);
this.gameWrapper = document.createElement("div");
this.gameWrapper.id = "mg__wrapper";
this.gameWrapper.className = "mg__wrapper";
this.gameContents = document.createElement("div");
this.gameContents.id = "mg__contents";
this.gameWrapper.appendChild(this.gameContents);
this.gameMessages = document.createElement("div");
this.gameMessages.id = "mg__onend";
this.gameMessages.className = "mg__onend";
this._setupGame(level,nbcase);
};
/**
* Memory _setupGame - Sets up the game
*
* We're caching all game related variables, and by default, displaying the
* meta info bar and start screen HTML.
*
* A NOTE ABOUT GAME STATES:
*
* There are 4 game states in total, governed by the variable this.gameState.
* Each game state allows for a certain series of functions to be performed.
* The gameStates are as follows:
*
* 1 : default, allows user to choose level
* 2 : set when user chooses level, and game is in play
* 3 : game is finished
*/
Memory.prototype._setupGame = function(level, nbcase) {
var self = this;
this.gameState = 1;
this.cards = shuffle(this.options.cards);
this.card1 = "";
this.card2 = "";
this.card1id = "";
this.card2id = "";
this.card1flipped = false;
this.card2flipped = false;
this.flippedTiles = 0;
this.chosenLevel = "";
this.numMoves = 0;
this._setupGameWrapper(level, nbcase);
}
/**
* Memory _setupGameWrapper
*
* This function sets up the game wrapper, which is where the actual memory
* tiles will reside and where all the game play happens.
*/
Memory.prototype._setupGameWrapper = function(levelNode,nbcase) {
this.nbcase = nbcase;
this.level = levelNode;
this.gameContents.className = "mg__contents mg__level-"+this.level;
this.game.appendChild(this.gameWrapper);
this.chosenLevel = this.level;
this._renderTiles();
};
/**
* Memory _renderTiles
*
* This renders the actual tiles with content. A few thing happen here:
*
* 1. Calculate grid X and Y based on user level selection
* 2. Calculate num tiles
* 3. Create new cards array based on level, and draw cards from original array
* 4. Shuffle the new cards array
* 5. Cards get distributed into tiles
* 6. gamePlay function gets triggered, taking care of all the game play action.
*/
Memory.prototype._renderTiles = function() {
if(this.level == 1) {this.gridX = 2; this.gridY= 2; }
else if(this.level == 2) {this.gridX = 3; this.gridY= 2; }
else { this.gridX = 2; this.gridY=4 ; }
// this.gridY = this.gridX ;
this.numTiles = this.nbcase*2;
//this.halfNumTiles = this.gridX;
this.halfNumTiles = this.numTiles/2;
this.newCards = [];
for ( var i = 0; i < this.halfNumTiles; i++ ) {
this.newCards.push(this.cards[i], this.cards[i]);
}
this.newCards = shuffle(this.newCards);
this.tilesHTML = '';
var n = 0;
for ( var i = 0; i < this.numTiles; i++ ) {
n = n + 1;
if(this.level == 3 && n == 5 ){n = 9}
if(this.level == 2 && n == 4 ){n = 7}
this.tilesHTML += '