-
Notifications
You must be signed in to change notification settings - Fork 1
/
card.js
81 lines (70 loc) · 1.96 KB
/
card.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var CardConstants = {
SHAPES: ["A", "B", "C"],
FILLS: ["L", "M", "N"],
COLORS: ["X", "Y", "Z"],
COUNTS: [1, 2, 3]
};
var Deck = function () {
this.solution = [];
this.playableCards = this.getTwelveCards();
};
Deck.isASet = function (threeCards) {
var isASet = true,
card1 = threeCards[0],
card2 = threeCards[1],
card3 = threeCards[2];
for (var i = 0; i < 4; i++) {
if (
!((card1[i] === card2[i] && card2[i] === card3[i]) && card1[i] === card3[i]) &&
!((card1[i] !== card2[i] && card2[i] !== card3[i]) && card1[i] !== card3[i])
) {
isASet = false;
}
}
return isASet;
};
Deck.allCards = function () {
var allCards = [];
CardConstants.SHAPES.forEach(function (shape) {
CardConstants.FILLS.forEach(function (fill) {
CardConstants.COLORS.forEach(function (color) {
CardConstants.COUNTS.forEach(function (count) {
allCards.push(shape + fill + color + count);
});
});
});
});
return allCards;
};
Deck.solve = function (cards) {
var solutions = [];
for (var i = 0; i < (cards.length - 2); i++) {
for (var j = i + 1; j < (cards.length - 1); j++) {
for (var k = j; k < cards.length; k++) {
var threeCards = [cards[i], cards[j], cards[k]];
if (Deck.isASet(threeCards)) {
solutions.push(threeCards);
}
}
}
}
return solutions;
};
Deck.prototype.getTwelveCards = function () {
var playableDeck = [], allCards = Deck.allCards();
for (var i = 0; i < 12; i++) {
randomIdx = Math.floor(Math.random() * allCards.length);
playableDeck.push(allCards.splice(randomIdx, 1)[0]);
}
var solution = Deck.solve(playableDeck);
if (solution.length > 0) {
this.solution = solution;
return playableDeck;
} else {
return this.getTwelveCards().bind(this);
}
};
// var three = [ [ 'CMZ1' ], [ 'AMY1' ], [ 'CLX2' ] ];
// var cards = new Deck ();
// console.log(Deck.isASet(three));
// console.log(cards.solution.length);