-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
249 lines (232 loc) · 9.52 KB
/
index.html
File metadata and controls
249 lines (232 loc) · 9.52 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>AI Mind Reader</title>
<style>
body { font-family: system-ui, sans-serif; padding: 0.5rem; max-width: 32rem; margin: 0 auto; }
h1 { margin: 0.2rem 0 0.35rem 0; font-size: 1.4rem; }
p { margin: 0.35rem 0; }
.picker { margin-bottom: 0.5rem; }
.picker-section { margin-bottom: 0.4rem; }
.picker-section label { display: block; font-size: 0.85rem; color: #666; margin-bottom: 0.2rem; }
.rank-cards { display: flex; flex-direction: column; gap: 0.25rem; }
.rank-row { display: flex; gap: 0.25rem; flex-wrap: wrap; }
.suit-cards { display: flex; gap: 0.25rem; flex-wrap: wrap; }
.rank-card, .suit-card { width: 2.5rem; height: 3rem; border: 1px solid #ccc; border-radius: 4px; display: flex; align-items: center; justify-content: center; font-size: 1rem; background: #fff; cursor: pointer; user-select: none; }
.rank-card:hover, .suit-card:hover { border-color: #666; background: #f8f8f8; }
.rank-card.selected, .suit-card.selected { border-color: #1a4d6d; border-width: 2px; background: #e8f4fc; }
.suit-card.red { color: #c00; }
.suit-card { font-size: 1.25rem; }
.add-row { margin-top: 0.4rem; }
.cards-row { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; margin: 0.4rem 0; }
.reset-row { margin-top: 2rem; }
.reset-row button { padding: 0.35rem 0.6rem; font-size: 0.95rem; cursor: pointer; border-radius: 4px; border: 1px solid #d0d0d0; background: #e8e8e8; color: #777; }
.reset-row button:hover { background: #ddd; color: #555; }
.reset-row button.revealed { border-color: #1a4d6d; background: #e8f4fc; color: #1a4d6d; }
.reset-row button.revealed:hover { background: #d0e8f4; }
footer { margin-top: 1rem; padding-top: 0.5rem; font-size: 0.8rem; color: #666; }
footer a { color: #1a4d6d; }
.pile { display: flex; gap: 0.4rem; flex-wrap: wrap; min-height: 5rem; }
.card-slot { width: 4rem; height: 5.5rem; border: 1px solid #ccc; border-radius: 6px; display: flex; align-items: center; justify-content: center; gap: 0.25rem; font-size: 1.25rem; background: #fff; }
.card-slot.empty { background: #f0f0f0; color: #999; font-size: 0.9rem; }
.card-slot.red { color: #c00; }
.card-back { width: 4rem; height: 5.5rem; border: 2px solid #1a4d6d; border-radius: 6px; background: linear-gradient(135deg, #1a4d6d 0%, #2d6a8f 100%); cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.25rem; padding: 0.25rem; box-sizing: border-box; }
.card-back:hover { opacity: 0.95; transform: scale(1.02); }
.card-back.clickable { animation: pulse-border 1.5s ease-in-out infinite; }
.card-back.clickable .click-hint { font-size: 0.55rem; color: rgba(255,255,255,0.95); text-align: center; line-height: 1.2; text-transform: uppercase; }
@keyframes pulse-border { 0%, 100% { box-shadow: 0 0 0 0 rgba(26, 77, 109, 0.5); } 50% { box-shadow: 0 0 0 6px rgba(26, 77, 109, 0.2); } }
.card-back.revealed { background: #fff; cursor: default; font-size: 1.25rem; flex-direction: row; }
.card-back.revealed.red { color: #c00; }
.add-row button { padding: 0.4rem 0.75rem; font-size: 1rem; cursor: pointer; }
#add-btn:disabled { cursor: not-allowed; opacity: 0.6; }
</style>
<script src="algorithm.js"></script>
<script>
var pile = [];
var revealed = null;
var selectedRank = 2;
var selectedSuit = "c";
function selectedCardIsInPile() {
return pile.some(function (card) {
return card.rank === selectedRank && card.suit === selectedSuit;
});
}
function updateAddButtonState() {
var btn = document.getElementById("add-btn");
btn.disabled = pile.length >= 4 || selectedCardIsInPile();
}
function addToPile() {
if (pile.length >= 4 || selectedCardIsInPile()) return;
pile.push({ rank: selectedRank, suit: selectedSuit });
renderPile();
updateAddButtonState();
if (pile.length === 4) {
var el = document.getElementById("fifth-card");
el.classList.remove("revealed");
el.classList.add("clickable");
el.innerHTML = '<span class="click-hint">Click to reveal</span>';
el.onclick = revealFifth;
el.title = "Click to reveal your hidden card";
}
}
function reset() {
if (!revealed && !confirm("Clear all cards and start over?")) return;
pile = [];
revealed = null;
selectedRank = 2;
selectedSuit = "c";
var el = document.getElementById("fifth-card");
el.classList.remove("revealed", "clickable", "red");
el.onclick = null;
el.innerHTML = "";
el.title = "Add 4 cards first";
renderRankCards();
renderSuitCards();
renderPile();
updateAddButtonState();
document.getElementById("reset-btn").classList.remove("revealed");
}
function setRank(rank) {
selectedRank = rank;
document.querySelectorAll(".rank-card").forEach(function (el) {
el.classList.toggle("selected", parseInt(el.getAttribute("data-rank"), 10) === rank);
});
updateAddButtonState();
}
function setSuit(suit) {
selectedSuit = suit;
document.querySelectorAll(".suit-card").forEach(function (el) {
el.classList.toggle("selected", el.getAttribute("data-suit") === suit);
});
updateAddButtonState();
}
// Get the unicode character for a suit
function suitChar(suit) {
if (suit === "c") return "\u2663";
if (suit === "h") return "\u2665";
if (suit === "s") return "\u2660";
if (suit === "d") return "\u2666";
return "";
}
function renderRankCards() {
var numberRanks = [2, 3, 4, 5, 6, 7, 8, 9, 10];
var faceRanks = [11, 12, 13, 1]; // Ace is low (= 1)
var container = document.getElementById("rank-cards");
container.innerHTML = "";
function addRow(ranks) {
var row = document.createElement("div");
row.className = "rank-row";
ranks.forEach(function (rank) {
var card = document.createElement("div");
card.className = "rank-card" + (rank === selectedRank ? " selected" : "");
card.setAttribute("data-rank", rank);
card.textContent = rankToShort(rank);
card.onclick = function () { setRank(rank); };
row.appendChild(card);
});
container.appendChild(row);
}
addRow(numberRanks);
addRow(faceRanks);
}
function renderSuitCards() {
var suitOptions = [
{ suit: "c", red: false },
{ suit: "h", red: true },
{ suit: "s", red: false },
{ suit: "d", red: true }
];
var container = document.getElementById("suit-cards");
container.innerHTML = "";
suitOptions.forEach(function (option) {
var card = document.createElement("div");
card.className = "suit-card" + (option.red ? " red" : "") + (option.suit === selectedSuit ? " selected" : "");
card.setAttribute("data-suit", option.suit);
card.textContent = suitChar(option.suit);
card.onclick = function () { setSuit(option.suit); };
container.appendChild(card);
});
}
function renderPile() {
var container = document.getElementById("pile");
container.innerHTML = "";
for (var i = 0; i < 4; i++) {
var slot = document.createElement("div");
slot.className = "card-slot";
if (i < pile.length) {
var card = pile[i];
slot.textContent = rankToShort(card.rank) + " " + suitChar(card.suit);
if (card.suit === "h" || card.suit === "d") slot.classList.add("red");
} else {
slot.classList.add("empty");
slot.textContent = "—";
}
container.appendChild(slot);
}
}
// Face cards to short string (Ace = 1 low)
function rankToShort(rank) {
if (rank === 1) return "A";
if (rank <= 10) return rank;
if (rank === 11) return "J";
if (rank === 12) return "Q";
if (rank === 13) return "K";
return rank;
}
function revealFifth() {
if (pile.length !== 4 || revealed) return;
var rank1 = pile[0].rank, suit1 = pile[0].suit;
var rank2 = pile[1].rank, suit2 = pile[1].suit;
var rank3 = pile[2].rank, suit3 = pile[2].suit;
var rank4 = pile[3].rank, suit4 = pile[3].suit;
var result = computeHiddenCard(rank1, suit1, rank2, suit2, rank3, suit3, rank4, suit4);
revealed = { rank: result.rank5, suit: result.suit5 };
var el = document.getElementById("fifth-card");
el.classList.add("revealed");
el.classList.remove("clickable");
el.onclick = null;
el.textContent = rankToShort(revealed.rank) + " " + suitChar(revealed.suit);
if (revealed.suit === "h" || revealed.suit === "d") el.classList.add("red");
document.getElementById("reset-btn").classList.add("revealed");
}
</script>
</head>
<body>
<h1>AI Mind Reader</h1>
<p>Pick a rank and a suit, then add each of your four cards to the pile.</p>
<div class="picker">
<div class="picker-section">
<label>Rank</label>
<div class="rank-cards" id="rank-cards"></div>
</div>
<div class="picker-section">
<label>Suit</label>
<div class="suit-cards" id="suit-cards"></div>
</div>
<div class="add-row">
<button type="button" id="add-btn" onclick="addToPile()">Add to the pile</button>
</div>
</div>
<p>Your cards:</p>
<div class="cards-row">
<div id="pile" class="pile"></div>
<div id="fifth-card" class="card-back" title="Add 4 cards first"></div>
</div>
<div class="reset-row">
<button type="button" id="reset-btn" onclick="reset()">Start Over</button>
</div>
<footer>
Adapted from Tim Muller's <a href="https://youtu.be/rkrjo4IIb1I" target="_blank" rel="noopener noreferrer">JavaScript Card Trick video</a>
(his original version is <a href="http://magic.tinefamily.com/index_tim.html" target="_blank" rel="noopener noreferrer">here)</a>.<br>
My code is available on <a href="https://github.com/steventine/javascript-card-trick" target="_blank" rel="noopener noreferrer">GitHub</a>.
</footer>
<script>
renderRankCards();
renderSuitCards();
renderPile();
updateAddButtonState();
</script>
</body>
</html>