-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards3-template.js
115 lines (86 loc) · 3.23 KB
/
cards3-template.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function makeCard(id) {
// If id is invalid (out of range, etc)
// ...
return null;
// Otherwise build an instance object with an id property,
// representing one card, and attach to it four methods:
// rank()
// suit()
// color()
// name()
// Each method property should be just a link to the corresponding method
// of the factory itself.
return /* that instance here */;
}
//-----------------------------
// Methods called though instances (where 'this' means the instance):
//-----------------------------
makeCard.rank = function() { // --> 1..13, NaN
// code here...
};
makeCard.suit = function() { // --> 1..4, NaN
// code here...
};
makeCard.color = function() { // -->"red,"black",NaN
// code here...
};
makeCard.cardName = function() { //--> string, NaN
// This method can't have the key 'name' within the makeCard function,
// but instance objects can store a reference to it called 'name'
// code here...
};
//-----------------------
// Methods to be called through factory only:
//-----------------------
makeCard.isCard = function(thing) { // --> true,false
// return true if thing is a valid card instance made by this factory
}
//---------------------
// Additional factory properties
//---------------------
makeCard.fullSet = []; //<-- instead, generate array of 52 card instances
//----------------------
// Simple Testing suite
// Supplement as needed!
function assert(claim,message) {
if (!claim) console.error(message);
}
// card instances needed for assertions:
var card0 = makeCard(0);
var card3 = makeCard(3);
var card5 = makeCard(5);
var card51 = makeCard(51);
// Test instance methods:
assert(card0.rank()===1, "Test 1 failed");
assert(card3.rank()===1, "Test 2 failed");
assert(card51.rank()===13,"Test 3 failed");
assert(card0.suit()===1, "Test 4 failed");
assert(card5.suit()===2, "Test 5 failed");
assert(card51.suit()===4, "Test 6 failed");
assert(card0.color()==='red', "Test 10 failed");
assert(card3.color()==='black', "Test 11 failed");
assert(card5.name()==='Two of Diamonds', "Test 12 failed");
assert(card51.name()==='King of Clubs', "Test 13 failed");
// Test makeCard.isCard:
assert(makeCard.isCard(card0), "Test 21 failed")
assert(makeCard.isCard(card51), "Test 22 failed")
assert(!makeCard.isCard(0), "Test 23 failed")
assert(!makeCard.isCard({}), "Test 24 failed")
// Test failed card-making results:
assert(!makeCard(52),"Test 26 failed");
assert(!makeCard("0"),"Test 27 failed");
assert(!makeCard(-1),"Test 28 failed");
assert(!makeCard(false),"Test 30 failed");
assert(!makeCard(true),"Test 31 failed");
// Test fullSet array:
assert(typeof makeCard.fullSet === 'object', "Test 40 failed");
assert(makeCard.fullSet.length === 52, "Test 41 failed");
assert(makeCard.isCard(makeCard.fullSet[0]), "Test 42 failed")
assert(makeCard.fullSet[5].name() === card5.name(), "Test 43 failed");
assert(makeCard.fullSet[51].name() === card51.name(), "Test 44 failed");
// Test that methods are shared:
assert(card0 !== card3, "Test 50 failed"); //first prove different cards
assert(card0.rank === card3.rank, "Test 51 failed");
assert(card0.suit === card3.suit, "Test 52 failed");
assert(card0.name === card3.name, "Test 53 failed");
//etc...