-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardSwap.html
286 lines (282 loc) · 6.44 KB
/
CardSwap.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<!DOCTYPE html>
<html>
<head>
<title>Card Swap</title>
<style>
h1, h2 {
text-align: center;
}
h1 {
margin-bottom: 0;
}
h2 {
color:gray;
font-size: 1.2em;
margin-top: 0;
}
button {
cursor: pointer;
}
.card {
width: 10%;
height: 10%;
cursor: pointer;
}
.button {
border: 0 none;
border-radius: 2px 2px 2px 2px;
color: #FFFFFF;
cursor: pointer;
display: block;
line-height: 20px;
margin-right: auto;
margin-left: auto;
margin-top: 0;
padding: 7px 10px;
text-transform: none;
width: 50px; /* auto */
text-align: center;
background-color: #0091ea;
}
.button:hover {
background-color: gray;
}
.active {
opacity: 0.4;
}
</style>
<script type="text/javascript">
//Random generator
function randomGen(n) {
return Math.floor((Math.random() * n) + 1);
}
//Card generator
function cardGen() {
//Suit generator
var sv = randomGen(4);
//Value generator
var vv = randomGen(13);
return [sv,vv];
}
//Cardname converter
function cardName() {
var card=cardGen();
var s = "";
switch(card[0]) {
case 1:
s="Spades";
break;
case 2:
s="Hearts";
break;
case 3:
s="Clubs";
break;
case 4:
s="Diamonds";
break;
}
var v = "";
switch(card[1]) {
case 1:
v="Ace";
break;
case 11:
v="Jack";
break;
case 12:
v="Queen";
break;
case 13:
v="King";
break;
default:
v=card[1].toString();
}
return v+"Of"+s;
}
//Array of cards without repeat
function cardList(n) {
var x = [];
for (i=0; i<n; i++){
var t = cardName();
if (x.indexOf(t)<0) x.push(t);
else i--;
}
listofCards=x;
return x;
}
//Image relative link formatter
function imageFormat(card) {
return "./cards/"+card+".svg";
}
//DOM create card in Cards div
function setCard(card) {
var x = document.createElement("img");
x.setAttribute("src",imageFormat(card));
x.setAttribute("alt",card);
x.setAttribute("class","card");
x.setAttribute("id",card);
x.setAttribute("onmousedown","imageToggle(this)");
x.setAttribute("opacity",1);
return x;
}
function createCard(card) {
var x=setCard(card);
document.getElementById("cards").appendChild(x);
}
//Reset cards
function clearCards() {
var element = document.getElementById("cards");
while (element.firstChild) {
element.removeChild(element.firstChild);
activeClick=0;
}
}
var listofCards=[];
//update clipboard
function updateCardlist() {
document.getElementById("listcards").innerHTML = "List of Cards: "+listofCards.toString();
copyText();
}
//DOM create array of cards in Cards div
function createCards(n) {
//reset cards
clearCards();
x=cardList(n);
for (i=0; i<x.length; i++){
createCard(x[i]);
}
updateCardlist();
}
//DOM create cards get X from textfield
function createXCards() {
var n = document.getElementById("number").value;
if (n>52) {
alert("Too many cards!");
} else if (n<0) {
alert("You owe me cards!");
} else {
createCards(n);
}
}
//Click
var activeClick = 0;
var totalSwap = 0;
//Update clicks on display
function updateActive() {
document.getElementById("activeclick").innerHTML = "Active click(s): "+activeClick;
}
//Update swaps on display
function updateTotal() {
document.getElementById("totalswap").innerHTML = "Total swap(s): "+totalSwap;
}
//Toggle image opacity
function imageToggle(img) {
img.style.display = "inline-block";
if (img.style.opacity==0.4){
img.className-=' active';
activeClick-=1;
} else if (activeClick>1) {
alert("You've chosen too many");
} else {
img.className+=' active';
activeClick+=1;
}
updateActive();
}
var undoCount=0;
//Undo
function undo() {
undoCount-=1;
if (undoCount=0) {
document.getElementById("undo").disabled;
}
/*var [a,b] = document.getElementsByClassName("active");
ano = document.getElementById(a.id);
bno = document.getElementById(b.id);
aid=a.id;
bid=b.id;
anew=setCard(aid);
anew.setAttribute("class","card");
bnew=setCard(bid);
anew.setAttribute("class","card");
//document.getElementById("activeclick").innerHTML = "a="+aid+"b="+bid;
document.getElementById("cards").replaceChild(anew,bno);
document.getElementById("cards").replaceChild(bnew,ano);
activeClick=0;
totalSwap++;
updateTotal();*/
}
//Redo
function redo() {
undoCount=1;
document.getElementById("undo").attributes.removeNamedItem("disabled");
}
//Swap
function swap() {
if (activeClick!=2) {
alert("Choose more.");
} else {
var [a,b] = document.getElementsByClassName("active");
ano = document.getElementById(a.id);
bno = document.getElementById(b.id);
aid=a.id;
bid=b.id;
anew=setCard(aid);
anew.setAttribute("class","card");
bnew=setCard(bid);
anew.setAttribute("class","card");
document.getElementById("cards").replaceChild(anew,bno);
document.getElementById("cards").replaceChild(bnew,ano);
activeClick=0;
totalSwap++;
updateActive();
updateTotal();
redo();
}
}
function copyText() {
var copyTextareaBtn = document.querySelector('.js-textareacopybtn');
copyTextareaBtn.addEventListener('click', function(event) {
var copyTextarea = document.querySelector('.js-copytextarea');
copyTextarea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
});
}
</script>
</head>
<body>
<h1>Card Swap</h1>
<h2>A deck of cards</h2>
<!--align left 2/3, buttons for card counts, sample sets generation-->
<div class="buttons">
<!--<button onclick="createCards(3)">3</button>
<button onclick="createCard('2_of_clubs')">2Club</button>-->
<p>Generate
<input type="number" id="number" name="number" value="3" min="0" max="52" size="5" maxlength="3">
cards
<button type="submit" onclick="createXCards()">Go</button>
<button class="js-textareacopybtn">Copy List of Cards</button>
<!--<button class="undo" id="undo" onclick="undo()" disabled>Undo</button>-->
</p>
<textarea id="listcards" class="js-copytextarea">List of Cards:</textarea>
<p class="button" id="swap" onclick="swap()">Swap</p>
</div>
<!--align right 1/3, counts of card swaps with history and reset button-->
<div class="count">
<!--display selected, swaps and -->
<p id="activeclick">Active click(s): 0</p>
<p id="totalswap">Total swap(s): 0</p>
</div>
<!--bottom 2/3 with cards-->
<div class="cards" id="cards">
</div>
</body>
</html>