-
Notifications
You must be signed in to change notification settings - Fork 0
/
deckOfCards.html
83 lines (75 loc) · 2.64 KB
/
deckOfCards.html
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
<!DOCTYPE html>
<html>
<head>
<title>Deck of cards</title>
<script type="text/javascript">
var suits = ['hearts', 'spades', 'diamonds', 'clubs'];
var face = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
class Card {
constructor(name, suit, val) {
this.name = name;
this.suit = suit;
this.val = val;
}
}
class Deck {
constructor(suits, face) {
this.deck = [];
this.reset = (suits, face) => {
for (let i = 0; i < suits.length; i++) {
for (let j = 0; j < face.length; j++) {
if (!isNaN(parseInt(face[j]))) {
this.deck.push(new Card(face[j], suits[i], parseInt(face[j])));
} else if (face[j] == 'J' || face[j] == 'Q' || face[j] == 'K') {
this.deck.push(new Card(face[j], suits[i], 10));
} else {
this.deck.push(new Card(face[j], suits[i], 11));
}
}
}
};
this.reset(suits, face);
this.shuffle = () => {
var currIdx = this.deck.length;
while (0 !== currIdx) {
let randIdx = Math.floor(Math.random() * currIdx);
currIdx -= 1;
let temp = this.deck[currIdx];
this.deck[currIdx] = this.deck[randIdx];
this.deck[randIdx] = temp;
}
};
this.deal = () => {
return this.deck.pop();
};
}
}
class Player {
constructor(name) {
this.name = name;
this.hand = [];
this.takeCard = deal => {
var card;
if (typeof(deal) == 'function') {
card = deal();
} else {
card = deal;
}
this.hand.push(card);
};
this.discard = () => {
this.hand.pop();
};
}
}
var deck = new Deck(suits, face);
var player = new Player('PJ');
deck.shuffle();
var card = deck.deal;
player.takeCard(card);
console.log(deck.deck);
</script>
</head>
<body>
</body>
</html>