'));
}
});
},
displayCards4: function() {
var self = this;
if (self.cards1.length == 0) {
return false;
}
$.each(self.cards4, function(index, card){
var cardNode = $('
', {
class: "card"
}).appendTo(self.$board4);
cardNode.data({
'id': card.id,
'shape': card.shape,
'fill': card.fill,
'color': card.color,
'border': card.border,
'number': card.number
});
var shapeNode = $("
");
shapeNode.addClass('shape ' + card.color + ' ' + card.shape + ' ' + card.fill + ' ' + card.border);
for (var i = 0; i < card.number; i++) {
cardNode.append(shapeNode.clone());
}
self.$board4.append(cardNode);
// display 4 cards per row
if ((index+1) % 3 === 0) {
self.$board4.append($('
'));
}
});
},
existingSet: function() {
var colors = [];
var shapes =[];
var fills= [];
var numbers= [];
var valid;
var self = this;
var cartes = self.cards;
var cartes1 = cartes;
var cartes2 = cartes;
var cpt=0;
var carte1;
var carte2;
var carte3;
var i, j, k;
for(i=0; i= 0) {
self.deselectCard(card);
} else {
self.selectCard(card);
}
if (self.selected.length === 3) {
self.silentSubmission();
}
});
},
setPageListeners: function() {
var self = this;
// if the user clicks on the page outside the game board, clear selected
$(document).on('click', function() {
self.clearSelections.call(self);
});
},
selectCard: function(card) {
$(card).addClass('selected');
this.selected.push(card);
if (this.selected.length > 3) {
this.clearSelections.call(this);
}
},
deselectCard: function(card) {
var self = this;
var index = self.selected.indexOf(card);
if (index > -1) {
self.selected.splice(index, 1);
}
$(card).removeClass('selected');
},
clearSelections: function() {
$.each(this.selected, function(index, card) {
$(card).removeClass('selected');
});
this.selected = [];
},
validateSet: function() {
var self = this;
var colors = $.map(self.selected, function(el) { return $(el).data("color");});
var shapes = $.map(self.selected, function(el) { return $(el).data("shape"); });
var fills= $.map(self.selected, function(el) { return $(el).data("fill"); });
var numbers = $.map(self.selected, function(el) { return $(el).data("number"); });
return (self.isSet(colors) && self.isSet(shapes) && self.isSet(fills) && self.isSet(numbers));
},
validateSet35: function() {
var self = this;
var colors = $.map(self.selected, function(el) { return $(el).data("color");});
var shapes = $.map(self.selected, function(el) { return $(el).data("shape"); });
var fills= $.map(self.selected, function(el) { return $(el).data("fill"); });
var borders= $.map(self.selected, function(el) { return $(el).data("border"); });
var numbers = $.map(self.selected, function(el) { return $(el).data("number"); });
return (self.isSet(colors) && self.isSet(shapes) && self.isSet(fills) && self.isSet(borders) && self.isSet(numbers));
},
validateSet44: function() {
var self = this;
var colors = $.map(self.selected, function(el) { return $(el).data("color");});
var shapes = $.map(self.selected, function(el) { return $(el).data("shape"); });
var fills= $.map(self.selected, function(el) { return $(el).data("fill"); });
var numbers = $.map(self.selected, function(el) { return $(el).data("number"); });
return (self.isSet4(colors) && self.isSet4(shapes) && self.isSet4(fills) && self.isSet4(numbers));
},
validateSet45: function() {
var self = this;
var colors = $.map(self.selected, function(el) { return $(el).data("color");});
var shapes = $.map(self.selected, function(el) { return $(el).data("shape"); });
var fills= $.map(self.selected, function(el) { return $(el).data("fill"); });
var borders= $.map(self.selected, function(el) { return $(el).data("border"); });
var numbers = $.map(self.selected, function(el) { return $(el).data("number"); });
return (self.isSet4(colors) && self.isSet4(shapes) && self.isSet4(borders) && self.isSet4(fills) && self.isSet4(numbers));
},
validateSet55: function() {
var self = this;
var colors = $.map(self.selected, function(el) { return $(el).data("color");});
var shapes = $.map(self.selected, function(el) { return $(el).data("shape"); });
var fills= $.map(self.selected, function(el) { return $(el).data("fill"); });
var borders= $.map(self.selected, function(el) { return $(el).data("border"); });
var numbers = $.map(self.selected, function(el) { return $(el).data("number"); });
return (self.isSet5(colors) && self.isSet5(shapes) && self.isSet5(fills) && self.isSet5(borders) && self.isSet5(numbers));
},
isSet: function(arr) {
var unique = [];
$.each(arr, function(i, el){
if($.inArray(el, unique) === -1) unique.push(el);
});
//console.log(unique);
return unique.length === 1 || unique.length === 3;
},
isSet4: function(arr) {
var unique = [];
$.each(arr, function(i, el){
if($.inArray(el, unique) === -1) unique.push(el);
});
//console.log(unique);
return unique.length === 1 || unique.length === 4;
},
isSet5: function(arr) {
var unique = [];
$.each(arr, function(i, el){
if($.inArray(el, unique) === -1) unique.push(el);
});
//console.log(unique);
return unique.length === 1 || unique.length === 5;
},
silentSubmission: function() {
var valid = this.validateSet();
if (valid) {
this.submitSet();
}
},
submitSet: function() {
var self = this;
var ids = $.map(self.selected, function(el) { return $(el).data("id");});
// ajax request to get initial set of cards
var newCardRequest = $.ajax({
url: 'set.php?action=submit',
type: 'GET',
dataType: 'json',
success: function(data) {
self.clearCards(ids);
// to do - implement game complete check on server
if (!data.gameComplete) {
self.updateCards(data);
self.existingSet();
self.increaseScore();
} else {
self.gameWon();
}
},
error: function() {
console.log(arguments);
}
});
this.clearSelections();
},
clearCards: function(ids) {
// remove submitted cards game's card array and clear the board
var self = this;
this.selected = [];
this.$board.empty();
var cardIds = $.map(self.cards, function(card) { return card.id; });
$.each(ids, function(idx, id) {
var location = cardIds.indexOf(id);
if (location > -1) {
cardIds.splice(location, 1);
self.cards.splice(location, 1);
}
});
},
updateCards: function(newCards) {
this.cards = this.cards.concat(newCards);
this.displayCards();
},
increaseScore: function() {
this.$score.html(++this.score);
},
startRound: function() {
// todo
// reset timer to 30 seconds
},
gameWon: function() {
alert("you won!");
},
gameLost: function() {
alert("you lost :(");
}
};
Game.deal();
/*
var canvases = $('.red');
console.log(canvases);
canvases.each(function(i, c) {
var context = c.getContext('2d');
console.log(c);
drawDiamond(context, 50, 50, 75, 100);
context.fillStyle = "red";
context.fill();
});
function drawDiamond(context, x, y, width, height){
context.save();
context.beginPath();
context.moveTo(x, y);
// top left edge
context.lineTo(x - width / 2, y + height / 2);
// bottom left edge
context.lineTo(x, y + height);
// bottom right edge
context.lineTo(x + width / 2, y + height / 2);
// closing the path automatically creates
// the top right edge
context.closePath();
context.restore();
}
/*
var canvases = $( ".red" );
for (let canvas of canvases) {
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
ctx.fillRect(10, 10, 100, 100);
}
/*
var canvases = document.getElementsByClassName('shape diamond red solid');
console.log(canvases);
for (let canvas of canvases) {
var context = canvas.getContext('2d');
drawDiamond(context, 50, 50, 75, 100);
canvas.fillStyle("#FF0000");
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function drawDiamond(context, x, y, width, height){
context.save();
context.beginPath();
context.moveTo(x, y);
// top left edge
context.lineTo(x - width / 2, y + height / 2);
// bottom left edge
context.lineTo(x, y + height);
// bottom right edge
context.lineTo(x + width / 2, y + height / 2);
// closing the path automatically creates
// the top right edge
context.closePath();
context.fillStyle = "red";
context.fill();
context.restore();
}*/