-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck.js
141 lines (110 loc) · 3.62 KB
/
deck.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
// Deck functions on HTML
var deck, hand, discards;
var numDecks = 1; //updated in deck.js reset()
var numCards; //set in cards.js stackMakeDeck()
var numInDeck;
var numInDiscard = 0;
var numInHand = 0;
window.onload = init;
function init() {
deck = new Stack();
hand = new Stack();
discards = new Stack();
deck.makeDeck(numDecks); //set initial # of decks
displayNumCards(); //info.js
displayNumInDiscard(); //info.js
numInHand = 0;
displayNumInHand();
display();
}
function shuffle() {
if (deck == null) return;
deck.shuffle(3); //increase for better shuffling?
console.log("Deck shuffled");
display();
}
function deal() {
var e = document.getElementById("dealNum");
var numDealt = e.value; //updates
if (deck == null) return;
if (deck.cardCount() < numDealt) //run cardCount
alert("Not enough cards.");
else {
discard();
for (var i = 0; i < numDealt; i++)
hand.addCard(deck.deal()); //run addCard, deal from deck
numInHand = numDealt;// custom code
displayNumInHand();// custom code
}
// displayNumInHand();
display();
}
function discard() {
if (deck == null) return;
discards.combine(hand);
display();
}
function reset() {
if (deck == null) return;
//New code to rerun init() with # of decks
var e = document.getElementById("deckNum");
numDecks = e.value
init();
//Old code to combine other stacks
// discards.combine(hand);
// deck.combine(discards);
// display();
}
function display() {
var el, top, left;
var n;
// Note: only a fraction of the cards in the deck and discard pile are
// displayed, just enough to get an idea of the number of cards in each.
displayNumInDiscard(); //stack.js stackCardCount()
// deck
left = 0;
top = 0;
el = document.getElementById("deck");
while (el.firstChild != null)
el.removeChild(el.firstChild);
n = deck.cardCount();
for (var i = 0; i < Math.round(n / 5); i++) {
node = deck.cards[i].createNode();
node.firstChild.style.visibility = "hidden";
node.style.left = left + "em";
node.style.top = top + "em";
el.appendChild(node);
left += 0.10;
top += 0.05;
displayNumInDeck(); //info.js
}
//hand
left = 0;
top = 0;
el = document.getElementById("hand");
while (el.firstChild != null)
el.removeChild(el.firstChild);
for (var i = 0; i < hand.cardCount(); i++) {
node = hand.cards[i].createNode();
node.style.left = left + "em";
node.style.top = top + "em";
el.appendChild(node);
left += 1.00;
top += 0.25;
}
//discards
left = 0;
top = 0;
el = document.getElementById("discards");
while (el.firstChild != null)
el.removeChild(el.firstChild);
n = discards.cardCount();
for (var i = n - Math.round(n / 5); i < n; i++) {
node = discards.cards[i].createNode();
node.style.left = left + "em";
node.style.top = top + "em";
el.appendChild(node);
left += 0.10;
top += 0.05;
}
}