-
Notifications
You must be signed in to change notification settings - Fork 2
/
cards.js
142 lines (125 loc) · 3.51 KB
/
cards.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Suit = function(name, color) {
this.name = name
this.color = color
this.toString = function() {
return "Suit[" + this.name + "," + this.color + "]"
}
}
Value = function(value, name, shortName) {
this.value = value
this.name = name
this.shortName = shortName
if (!name) {
this.shortName = this.name = value.toString()
} else if (!shortName) {
this.shortName = this.name.substring(0,1)
}
this.toString = function() {
return "Value[" + this.value + "," + this.name + "," + this.shortName + "]"
}
}
Deck = function() {
this.values = [
this.ACE, this.TWO, this.THREE, this.FOUR, this.FIVE, this.SIX, this.SEVEN,
this.EIGHT, this.NINE, this.TEN, this.JACK, this.QUEEN, this.KING
]
this.suits = [this.SPADE, this.CLUB, this.DIAMOND, this.HEART]
for(var i = 0; i < this.suits.length; i++) {
var suit = this.suits[i]
for(var j = 0; j < this.values.length; j++) {
var value = this.values[j]
this.cards.push(new Card(this, value, suit))
}
}
}
Deck.prototype = {
SPADE: new Suit("Spade", "black"),
CLUB: new Suit("Club", "black"),
DIAMOND: new Suit("Diamond", "red"),
HEART: new Suit("Heart", "red"),
TWO: new Value(2),
THREE: new Value(3),
FOUR: new Value(4),
FIVE: new Value(5),
SIX: new Value(6),
SEVEN: new Value(7),
EIGHT: new Value(8),
NINE: new Value(9),
TEN: new Value(10),
JACK: new Value(11, "Jack"),
QUEEN: new Value(12, "Queen"),
KING: new Value(13, "King"),
ACE: new Value(14, "Ace"),
shuffle_count: 4,
cards: [],
drawn: [],
shuffle: function() {
for(var i = 0; i < this.shuffle_count; i++) {
this.cards.sort(function() {
return Math.round(Math.random(1)) || -1
})
}
},
draw: function(number) {
number = number || 1
var result = []
for(var i = 0; i < number; i++) {
var card = this.cards.pop()
result.push(card)
this.drawn.push(card)
}
return (number > 1) ? result : result[0]
},
collect: function() {
while(this.drawn.length > 0) {
var card = this.drawn.pop()
card.removeView()
card.show()
this.cards.push(card)
}
},
toString: function() {
return this.cards.toString()
}
}
Card = function(deck, value, suit) {
this.deck = deck
this.suit = suit
this.value = value
}
Card.prototype = {
toString: function() {
return this.value.name + " of " + this.suit.name + "s"
},
hide: function() {
var view = this.createView()
view.className += ' hidden'
},
show: function() {
var view = this.createView()
var className = view.className
if (className.indexOf(' hidden') >= 0) {
view.className = className.replace(/ hidden/, '')
}
},
removeView: function() {
if (this.view && this.view.parentNode) {
this.view.parentNode.removeChild(this.view)
}
},
createView: function() {
if (this.view) return this.view
var view = document.createElement('div')
view.className = 'card ' + this.suit.color + ' ' + this.suit.name.toLowerCase() + ' ' + this.value.name.toLowerCase()
var valueTop = view.appendChild(document.createElement('span'))
valueTop.className = 'value top'
valueTop.appendChild(document.createTextNode(this.value.shortName))
var valueBottom = view.appendChild(document.createElement('span'))
valueBottom.className = 'value bottom'
valueBottom.appendChild(document.createTextNode(this.value.shortName))
var suit = view.appendChild(document.createElement('span'))
suit.className = 'suit'
this.view = view
return view
}
}