-
Notifications
You must be signed in to change notification settings - Fork 1
/
Card.js
32 lines (32 loc) · 978 Bytes
/
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
var Card = this.Card = function(shape, color, shading, number) {
if (shape < 0 || shape >= 3) {
throw new Error('shape must be 0, 1, or 2');
}
if (color < 0 || color >= 3) {
throw new Error('color must be 0, 1, or 2');
}
if (shading < 0 || shading >= 3) {
throw new Error('shading must be 0, 1, or 2');
}
if (number < 0 || number >= 3) {
throw new Error('number must be 0, 1, or 2');
}
this.attributes = [shape, color, shading, number];
};
Card.createFromJSON = function(cardData) {
return new Card(cardData.attributes[0], cardData.attributes[1], cardData.attributes[2], cardData.attributes[3]);
};
Card.equals = function(cardA, cardB) {
if (!cardA && !cardB) {
return true;
}
if (!cardA || !cardB) {
return false;
}
for (var i = 0; i < 4; i += 1) {
if (cardA.attributes[i] !== cardB.attributes[i]) {
return false;
}
}
return true;
}