-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
333 lines (292 loc) · 8.63 KB
/
index.html
File metadata and controls
333 lines (292 loc) · 8.63 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
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
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Игра 21 (Блэкджек)</title>
<style>
body {
font-family: 'Arial Neue', sans-serif;
background-color: #111;
color: #eee;
margin: 0;
padding: 30px 20px;
display: flex;
flex-direction: column;
align-items: center;
}
h1 {
font-size: 2.5em;
margin-bottom: 30px;
color: #00ffff;
text-shadow: 0 0 15px #00ffff;
}
#game {
background: #222;
padding: 30px;
border-radius: 20px;
box-shadow: 0 0 30px rgba(0,255,255,0.3);
max-width: 700px;
width: 100%;
display: flex;
flex-direction: column;
align-items: stretch;
}
.section {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 25px;
}
h2 {
margin-bottom: 10px;
font-size: 1.4em;
color: #0ff;
text-shadow: 0 0 8px #0ff;
}
.cards {
display: flex;
gap: 15px;
flex-wrap: wrap;
justify-content: center;
}
.card {
width: 60px;
height: 90px;
background: #333;
border-radius: 10px;
border: 2px solid #555;
box-shadow: inset 0 0 8px #000, 0 4px 10px rgba(0, 0, 0, 0.5);
font-size: 1.5em;
font-weight: bold;
display: flex;
justify-content: center;
align-items: center;
color: #eee;
cursor: default;
transition: all 0.2s;
}
.card.back {
background: #000;
color: #222;
font-size: 1.8em;
font-weight: bold;
text-shadow: 0 0 4px #00ffff;
}
/* Акцентированные кнопки */
button {
background: #00ffff;
border: 3px solid #00cccc;
border-radius: 25px;
padding: 12px 30px;
margin: 10px 15px;
font-size: 1.2em;
font-weight: 600;
color: #111;
cursor: pointer;
box-shadow: 0 0 10px #00ffff66 inset, 0 4px 15px rgba(0,255,255,0.4);
transition: all 0.3s;
}
button:hover {
background: #00cccc;
transform: scale(1.05);
}
#result {
margin-top: 20px;
font-size: 1.4em;
font-weight: bold;
color: #ff0;
text-shadow: 0 0 8px #ff0;
}
@media(max-width: 700px) {
#game {
padding: 20px;
}
button {
padding: 10px 20px;
font-size: 1em;
}
.card {
width: 50px;
height: 70px;
font-size: 1.2em;
}
}
</style>
</head>
<body>
<h1>Игра 21 (Блэкджек)</h1>
<div id="game">
<div class="section" id="player-area">
<h2>Игрок</h2>
<div id="player-cards" class="cards"></div>
<p>Очки: <span id="player-score">0</span></p>
</div>
<div class="section" id="dealer-area">
<h2>Дилер</h2>
<div id="dealer-cards" class="cards"></div>
<p>Очки: <span id="dealer-score">0</span></p>
</div>
<div style="text-align: center; margin-top: 20px;">
<button id="btn-hit">Берём карту</button>
<button id="btn-stand">Остановка</button>
<button id="btn-new" style="display:none;">Новая игра</button>
</div>
<div id="result"></div>
</div>
<script>
const suits = ['♠', '♥', '♦', '♣'];
const ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
let deck = [];
let playerCards = [];
let dealerCards = [];
let playerScore = 0;
let dealerScore = 0;
let gameOver = false;
const playerCardsDiv = document.getElementById('player-cards');
const dealerCardsDiv = document.getElementById('dealer-cards');
const playerScoreSpan = document.getElementById('player-score');
const dealerScoreSpan = document.getElementById('dealer-score');
const resultDiv = document.getElementById('result');
const btnHit = document.getElementById('btn-hit');
const btnStand = document.getElementById('btn-stand');
const btnNew = document.getElementById('btn-new');
function createDeck() {
deck = [];
for (let suit of suits) {
for (let rank of ranks) {
deck.push({suit, rank});
}
}
}
function getCardValue(card) {
if (['J', 'Q', 'K'].includes(card.rank)) {
return 10;
} else if (card.rank === 'A') {
return 11;
} else {
return parseInt(card.rank);
}
}
function shuffleDeck() {
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[deck[i], deck[j]] = [deck[j], deck[i]];
}
}
function dealCard(hand) {
const card = deck.pop();
hand.push(card);
return card;
}
function calculateScore(hand) {
let score = 0;
let aces = 0;
for (let card of hand) {
score += getCardValue(card);
if (card.rank === 'A') aces++;
}
while (score > 21 && aces > 0) {
score -= 10;
aces--;
}
return score;
}
function renderCards(hand, container, hideSecondDealerCard = false) {
container.innerHTML = '';
if (hideSecondDealerCard && hand.length > 1) {
// Покажем первую карту дилера, вторую спрячем
let firstCard = hand[0];
const cardDiv = document.createElement('div');
cardDiv.className = 'card';
cardDiv.textContent = `${firstCard.rank}${firstCard.suit}`;
container.appendChild(cardDiv);
for (let i = 1; i < hand.length; i++) {
const backDiv = document.createElement('div');
backDiv.className = 'card back';
backDiv.textContent = '★';
container.appendChild(backDiv);
}
} else {
for (let card of hand) {
const cardDiv = document.createElement('div');
cardDiv.className = 'card';
cardDiv.textContent = `${card.rank}${card.suit}`;
container.appendChild(cardDiv);
}
}
}
function startGame() {
createDeck();
shuffleDeck();
playerCards = [];
dealerCards = [];
gameOver = false;
resultDiv.textContent = '';
dealCard(playerCards);
dealCard(dealerCards);
dealCard(playerCards);
dealCard(dealerCards);
playerScore = calculateScore(playerCards);
dealerScore = calculateScore(dealerCards);
renderCards(playerCards, playerCardsDiv);
renderCards(dealerCards, dealerCardsDiv, true);
playerScoreSpan.textContent = playerScore;
dealerScoreSpan.textContent = '?';
document.getElementById('btn-hit').style.display = 'inline-block';
document.getElementById('btn-stand').style.display = 'inline-block';
document.getElementById('btn-new').style.display = 'none';
}
btnHit.onclick = () => {
if (gameOver) return;
dealCard(playerCards);
playerScore = calculateScore(playerCards);
renderCards(playerCards, playerCardsDiv);
playerScoreSpan.textContent = playerScore;
if (playerScore > 21) {
endGame();
}
};
btnStand.onclick = () => {
if (gameOver) return;
renderCards(dealerCards, dealerCardsDiv);
dealerScore = calculateScore(dealerCards);
dealerScoreSpan.textContent = dealerScore;
while (dealerScore < 17) {
dealCard(dealerCards);
renderCards(dealerCards, dealerCardsDiv);
dealerScore = calculateScore(dealerCards);
dealerScoreSpan.textContent = dealerScore;
}
endGame();
};
function endGame() {
gameOver = true;
renderCards(dealerCards, dealerCardsDiv);
dealerScore = calculateScore(dealerCards);
dealerScoreSpan.textContent = dealerScore;
let message = '';
if (playerScore > 21) {
message = 'Вы проиграли!';
} else if (dealerScore > 21) {
message = 'Дилер превысил 21! Вы победили!';
} else if (playerScore > dealerScore) {
message = 'Вы победили!';
} else if (playerScore < dealerScore) {
message = 'Дилер победил!';
} else {
message = 'Ничья!';
}
resultDiv.textContent = message;
document.getElementById('btn-hit').style.display = 'none';
document.getElementById('btn-stand').style.display = 'none';
document.getElementById('btn-new').style.display = 'inline-block';
}
btnNew.onclick = () => {
startGame();
};
// Игра запускается сразу при загрузке
startGame();
</script>
</body>
</html>