-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex2y.html
346 lines (337 loc) · 12 KB
/
index2y.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!DOCTYPE html>
<!-- FreeCell using svg for the cards - all in one HTML page -->
<html lang=en>
<head>
<link rel="icon" type="image/png" href="jc.png"/>
<link rel="manifest" href="/manifest.json">
<link rel="apple-touch-icon" href="jc192.png">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="green">
<meta name="apple-mobile-web-app-title" content="FreeCell">
<link rel="canonical" href="https://jcoonrod.github.io" />
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<link rel='shortcut icon' href='/jc.png'>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="green"/>
<meta name="description" content="One-Click FreeCell Game by John Coonrod"/>
<title>SVG Cards</title>
<style>
body {background:green;padding:0;margin:0;font-family:sans-serif;font-weight:bold;}
p, p>a {color:white;}
div.row {display:flex; flex-wrap:nowrap; flex-direction:row; justify-content:space-evenly;}
div.a {position:absolute;background:red;color:white;top:40%;left:40%;padding:1vw;font-size:3vw;}
div.s {width:11%;height:14vw; background:lightgreen;}
div.c {height:80vw;width:11%;position:relative;}
div.r {line-height:85%; letter-spacing:80%; font-size:14vw; color:red; background:white;}
div.b {line-height:85%; letter-spacing:80%; font-size:14vw; background:white;}
</style>
</head>
<body>
<p>One-Click FreeCell 0.2y 💂
<button onclick="shuffle(); deal()">New Game</button>
<button onclick="deal()">Replay</button>
</p>
<div class=row>
<div id=s0 class=s onclick="dropFree(0)"></div>
<div id=s1 class=s onclick="dropFree(1)"></div>
<div id=s2 class=s onclick="dropFree(2)"></div>
<div id=s3 class=s onclick="dropFree(3)"></div>
<div id=a0 class=s></div>
<div id=a1 class=s></div>
<div id=a2 class=s></div>
<div id=a3 class=s></div>
</div>
<div class=row> </div>
<div class=row>
<div id=c0 class=c onclick="popStack(0)"></div>
<div id=c1 class=c onclick="popStack(1)"></div>
<div id=c2 class=c onclick="popStack(2)"></div>
<div id=c3 class=c onclick="popStack(3)"></div>
<div id=c4 class=c onclick="popStack(4)"></div>
<div id=c5 class=c onclick="popStack(5)"></div>
<div id=c6 class=c onclick="popStack(6)"></div>
<div id=c7 class=c onclick="popStack(7)"></div>
</div>
<script>
window.onload = () => { 'use strict'; // register service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js');
}
}
// Set up a 1-d array of the unicode values for the cards 0-51
// Note - Unicode sets up 16 cards per suit, including two queens
// Unlike the old game, I want to only use the DOM
cards = []; // array of card div svg objects
tomove = []; // array of cards to move
deck = []; // sort order for the cards
freecells = [-1,-1,-1,-1]; // holds the cardNo if filled
nopen = 0; // computed # open freecells
nempty = 0; // computed # empty cascades
aces = [-1,-1,-1,-1]; // holds the card value if filled
suits = ["♠","♥","♦","♣"];
faces = ["💂","👸","🤴"]; // emojis for facecards
vals = ['A','2','3','4','5','6','7','8','9','10','J','Q','K'];
var nodes; // this gets defined by stackable
var first; // index within the nodes for the first that could be moved
var last; // " the top card
createCards();
// FUNCTIONS from top down // make numbers bigger for phones
function createCards(){
var size=( screen.width<600 ? 70 : 45);
for (n=0;n<52;n++) { // create 52 svg cards as strings in this array - innerHTML for divs
deck[n]=n; // initialize the deck pre-shuffle
var suit=Math.floor(n/13);
var f=''; if(suit==1 || suit==2) f=' fill="red"'; // optionally paint the red suits red
var val = n % 13;
var ctr = (val<10) ? suits[suit] : faces[val-10];
cards[n]='<svg viewBox="0 0 200 280">'
+ '<rect x="2" y="2" rx="20" ry="20" width="194" height="274" style="fill:white;stroke:black;stroke-width:4;" />'
+ '<text font-size="120" x="97" y="137" text-anchor="middle" alignment-baseline="central"'+f+'>'
+ ctr+'</text><text x="15" y="'+size+'" font-size="'+size+'"'+f+'>'+vals[val]+'</text>'
+ '<text x="'+(190-size)+'" y="'+size+'" font-size="'+size+'"'+f+'>'+suits[suit]+'</text></svg>';
}
}
// a function to shuffle the deck;
function shuffle(){
for(i=0; i<100; i++) { // do 100 random interchanges
j=Math.floor(52*Math.random());
k=Math.floor(52*Math.random());
m=deck[j]; deck[j]=deck[k]; deck[k]=m;
}
}
function deal(){
clearBoard();
var i=0;
var m = setInterval(frame,50);
function frame() { // use interval to deal the cards slowly
if(i==52) {
clearInterval(m);
tryAce();
}else{
j=i%8;
appendCard(deck[i],j);
i++;
}
}
}
function clearBoard(){
for(j=0;j<8;j++) { // clear cascades
const cascade=document.getElementById("c"+j);
while (cascade.firstChild) cascade.removeChild(cascade.firstChild);
}
for(j=0;j<4;j++) { // clear freecells and aces piles
aces[j]=-1;
document.getElementById("a"+j).innerHTML='';
freecells[j]=-1;
document.getElementById("s"+j).innerHTML='';
}
}
function popStack(j) { // a cascade has been clicked
stackable(j);
if (!tryMove(j)) {
if(!tryEmpty(j)) tryFreeCells(j);} // try either moving a cascade or a car to a freecell
tryAce();
}
function tryEmpty(j) { // attempt to move cascade to an open column
nmove=0; i=1;
while(!nmove && i<8) {
k=(j+i) % 8;
if(cascadeEmpty(k)) {
nmove++;
removeStack(j,first); // unlink these cards into the tomove array
appendStack(k,first);
}
i++;
}
return nmove;
}
function removeStack(j,m) {
tomove.length=0; // clear it off
cascade=document.getElementById("c"+j);
for(n=m;n<=last;n++) {
l=nodes[n].id;
tomove.push(l.substring(1)); // cardNo
}
for(n=m;n<=last;n++) cascade.removeChild(cascade.lastElementChild);
}
function appendStack(k,m) {
for(n=m;n<=last;n++) appendCard(tomove[n-m],k);
}
function tryMove(j) { // attempt to move a cascade to another
tomove = []; // array to hold cards that will move
computeFree();
if(nmax<(last-first+1)) first=last-nmax+1; // limit range
nmove=0; m=first; // we'll march down the cascade until something moves
cascade=document.getElementById("c"+j);
while(!nmove && m<=last) { // loop from the first of the stackables to whatever will move
srcId=nodes[m].id; val=getVal(srcId); color=getColor(srcId);
i=1; // cascades beyond k
while(i<8 && !nmove) {
k=(j+i) % 8;
destId=topCardId(k); val2=getVal(destId); color2=getColor(destId);
if((color!==color2 && val+1==val2)) {
nmove++;
removeStack(j,m)
appendStack(k,m);
}
i++;
}
m++;
}
return nmove;
}
function computeFree() { // What is the biggest move we can make
nopen=0;
for(k=0;k<4;k++) if(freecells[k]==-1) nopen++;
nempty=0;
for(j=0;j<8;j++) if(cascadeEmpty(j)) nempty++;
nmax=1+(nopen+nempty);
}
function stackable(j) { // return the global values of first and last
nodes=document.getElementById("c"+j).childNodes;
last=nodes.length-1; // index of top node in node list
if(last<1) {
first=last;
}else{
first=last-1; // index on how many can be stacked
match=1; // test to see whether it can be stacked
while(first>-1 && match) {
id1=nodes[first].id; id2=nodes[first+1].id;
val1=getVal(id1); val2=getVal(id2);
color1=getColor(id1); color2=getColor(id2); // returns 'r' or 'b'
match=(val1==(val2+1) && color1!==color2); // true or false
first--;
// console.log("F "+first+" L "+last+" M "+match)
}
first=(first==-1 && match) ? 0 : first+2;
}
}
function cascadeEmpty(j) {
return (document.getElementById("c"+j).childElementCount==0)
}
function appendCard(cardNo,j) { // add a card to the end of cascade j
const cascade=document.getElementById("c"+j);
z=cascade.childElementCount+1;
var card=document.createElement("div");
card.innerHTML=cards[cardNo];
card.id="v"+cardNo;
card.style.zindex=z.toString();
card.style.position='absolute';
card.style.width='100%';
y=(screen.width < 600 ? (z-1)*4 : (z-1)*3);
card.style.top=y.toString()+"vw";
cascade.appendChild(card);
}
function dropFree(k){ // Drop the card from freecell k to a cascade
var cardNo=freecells[k];
if(cardNo>-1) {
suit=Math.floor(cardNo/13); val=cardNo % 13; color=(suit==0 || suit==3) ? 'b' : 'r';
// run through the top cards to see if it can drop down to them
j=0;nmove=0;
while(j<8 && nmove==0){
destId=topCardId(j); destVal=getVal(destId); destSuit=getSuit(destId);
destColor=(destSuit==0 || destSuit==3) ? 'b' : 'r';
if((val==destVal-1) && (color !== destColor)) {
appendCard(cardNo,j);
freecells[k]=-1;
document.getElementById("s"+k).innerHTML="";
nmove=1;
}
j++;
}
j=0; // if nothing moved to a full cascade, try empty cascades!
while(j<8 && nmove==0) {
if(cascadeEmpty(j)) {
appendCard(cardNo,j);
freecells[k]=-1;
document.getElementById("s"+k).innerHTML="";
nmove=1;
}
j++;
}
}
tryAce(); // after making a move, see if any cards can jump up to foundation
}
// Run through to see if any top cards can jump to the ace pile
function topCardId(j){
cascade=document.getElementById("c"+j);
topCard = cascade.lastChild;
return (topCard) ? topCard.id : ''; // what card is it? v0...
}
// simple functions to convert id to values
const getSuit = s => Math.floor(parseInt(s.substring(1),10)/13);
const getVal = s => parseInt(s.substring(1),10) % 13;
const getColor = s => (getSuit(s)==0 || getSuit(s)==3) ? 'b' : 'r';
// check if anything can jump to the aces piles automatically
function tryAce() { // this will repeat as long as it moves something
var nmove=1; // This gets incremented and returned
var m=setInterval(frame2,100);
function frame2() {
if (nmove==0) {
clearInterval(m);
showFoundations();
} else {
nmove=0;
for(j=0;j<8;j++) { // try pop from the cascades
topID=topCardId(j);
if(topID) { // is there a top card in this cascade?
suit=getSuit(topID);
val=getVal(topID);
if(aces[suit]==(val-1)) {
nmove++;
aces[suit]++;
cascade=document.getElementById("c"+j);
cascade.removeChild(cascade.lastChild);
}
}
}
for(j=0;j<4;j++) { // try pop from freecells
cardNo=freecells[j];
if(cardNo>-1) {
val=cardNo % 13; suit=Math.floor(cardNo/13);
if(aces[suit]==(val-1)) {
nmove++;
aces[suit]=val;
freecells[j]=-1;
document.getElementById("s"+j).innerHTML="";
}
}
}
}
} // end frame
} // end try aces
function showFoundations(){
for(i=0;i<4;i++) {
if(aces[i]>-1) {
var card=cards[i*13+aces[i]];
document.getElementById("a"+i).innerHTML=card;
}
}
}
function tryFreeCells(j){
free=-1;
for(k=0;k<4;k++) if(freecells[k]==-1) free=k;
if(free>-1) {
t=topCardId(j);
freecells[free]=parseInt(t.substring(1)); // place the cardno in there for easy move later
popCard(j,t,"s"+free);
return 1;
}else{
return 0;
}
}
// Move a card from one place to another
function popCard(j,srcId,destId){
cascade=document.getElementById("c"+j);
src=document.getElementById(srcId);
dest=document.getElementById(destId);
if(dest) dest.innerHTML=src.innerHTML;
cascade.removeChild(cascade.lastChild);
return 1;
}
</script>
</body>
</html>