-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
102 lines (89 loc) · 2.64 KB
/
game.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
document.addEventListener('DOMContentLoaded', () => {
const rows = document.querySelectorAll('.row'); // Получаем все ряды
// Создаем игроков
const player1 = new Player('Player 1');
const player2 = new Player('Player 2');
// Создаем колоду и добавляем карты
const deck = new Deck();
const cardsData = [
{ name: 'Masik', value: 5 },
{ name: 'Tubik', value: 3 },
{ name: 'Skuf', value: 4 },
{ name: 'Altushka', value: 7 },
{ name: 'Sheikh', value: 6 },
{ name: 'Alkash', value: 4 },
{ name: 'ITishnik', value: 6 },
{ name: 'Fifa', value: 5 },
{ name: 'Vaper', value: 3 },
{ name: 'TikToker', value: 4 },
{ name: 'Khalif', value: 6 },
{ name: 'GameDev', value: 8 },
];
cardsData.forEach(card => deck.addCard(new Card(card.name, card.value)));
deck.shuffle();
// Игроки берут по 6 карт
for (let i = 0; i < 6; i++) {
player1.drawCard(deck);
player2.drawCard(deck);
}
// Рендеринг карт
const totalCards = 6;
for (let rowIndex = 0; rowIndex < 4; rowIndex++) {
const row = rows[rowIndex];
for (let i = 0; i < 3; i++) {
let card;
if (rowIndex < 2) {
card = player2.hand[rowIndex * 3 + i];
} else {
card = player1.hand[(rowIndex - 2) * 3 + i];
}
if (card) {
row.appendChild(card.render());
}
}
}
});
// Определение класса Card
class Card {
constructor(name, value) {
this.name = name;
this.value = value;
}
render() {
const cardElement = document.createElement('div');
cardElement.classList.add('card');
cardElement.textContent = `${this.name} (${this.value})`;
return cardElement;
}
}
// Определение класса Deck
class Deck {
constructor() {
this.cards = [];
}
addCard(card) {
this.cards.push(card);
}
shuffle() {
for (let i = this.cards.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]];
}
}
drawCard() {
return this.cards.pop();
}
}
// Определение класса Player
class Player {
constructor(name) {
this.name = name;
this.hand = [];
}
drawCard(deck) {
const card = deck.drawCard();
if (card) {
this.hand.push(card);
}
}
}