var game = {
deck : [],
cardTemplate : { 'numb' : '0', 'suit' : 'green', 'show' : 0 },
turn : 1,
pick1 : 0,
pick2 : 0,
pickState : 0,
attempts : 0,
matches : 0,
createDeck () {
// create red
console.log('createDeck');
for (i = 0; i < 5; i++) {
this.deck.push({ 'numb' : i, 'suit' : 'red', 'show' : 0 });
};
for (i = 0; i < 5; i++) {
this.deck.push({ 'numb' : i, 'suit' : 'red', 'show' : 0 });
};
// create blue
for (i = 0; i < 5; i++) {
console.log(i);
this.deck.push({ 'numb' : i, 'suit' : 'blue', 'show' : 0 });
};
for (i = 0; i < 5; i++) {
console.log(i);
this.deck.push({ 'numb' : i, 'suit' : 'blue', 'show' : 0 });
};
},
shuffleDeck (a) {
var j, x, i;
for (i = a.length; i; i--) {
j = Math.floor(Math.random() * i);
x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
},
showDeck () {
$.each(this.deck, function( index, value ) {
carddiv = '
';
carddiv += '
';
carddiv += this['numb'] + '
';
carddiv += '
';
carddiv += '
';
carddiv += '
';
$('body').append(carddiv);
});
},
flipCarttoBack(card) {
console.log(card);
console.log(card.find(".upCard").css('display'));
if(card.find(".upCard").css('display') === 'none'){
card.find(".upCard").show();
card.find(".downCard").hide();
card.attr('data-active', '1');
};
},
flipCartOver(card) {
card.find(".downCard").show();
card.find(".upCard").hide();
card.find(".upCard").hide();
},
updateinterface(){
$('.attempts').html(this.attempts);
$('.matches').html(this.matches);
},
checkGameState(currentClick){
console.log(this.pickState);
if(this.pickState === 1){
this.pick1 = currentClick;
};
if(this.pickState === 2){
this.pick2 = currentClick;
};
c1 = this.pick1.data('color') + this.pick1.data('num');
c2 = this.pick2.data('color') + this.pick2.data('num');
console.log(c1);
console.log(c2);
_this = this;
if(this.pickState === 2){
if(c1 === c2){
_this.pick1 = [];
_this.pick2 = [];
_this.attempts++;
_this.matches++;
_this.updateinterface();
} else {
this.pickState = 0;
console.log(this.pick1);
console.log(this.pick2);
console.log(this.pick1.css('background-color'));
console.log(this.pick1.attr("class"));
setTimeout(
function()
{
_this.flipCartOver(_this.pick1);
_this.flipCartOver(_this.pick2);
_this.flipCartOver(_this.pick2);
_this.pick1 = [];
_this.pick2 = [];
_this.attempts++;
_this.updateinterface();
}, 500);
}
};
this.pickState = 0;
},
setCardClicks () {
var _this = this;
$('.cardholder').click(function(e) {
if($(e.target).hasClass("downCard")){
console.log("downCard");
_this.flipCarttoBack($(e.target).parent());
_this.pickState++;
_this.checkGameState($(e.target).parent());
};
if($(e.target).hasClass("upCard")){
console.log("upCard");
_this.flipCartOver($(e.target).parent());
};
});
},
init () {
this.createDeck();
this.shuffleDeck(this.deck);
this.showDeck();
this.setCardClicks();
},
end () {
}
};
var newgame = game.init();