Skip to content

Commit

Permalink
js files
Browse files Browse the repository at this point in the history
  • Loading branch information
spbRusty authored Aug 10, 2024
1 parent fb27677 commit 3838e96
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
13 changes: 13 additions & 0 deletions card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Card {
constructor(name, power) {
this.name = name;
this.power = power;
}

render() {
const cardElement = document.createElement('div');
cardElement.className = 'card';
cardElement.textContent = `${this.name} (${this.power})`;
return cardElement;
}
}
20 changes: 20 additions & 0 deletions deck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Deck {
constructor() {
this.cards = [];
}

addCard(card) {
this.cards.push(card);
}

drawCard() {
return this.cards.length ? this.cards.pop() : null;
}

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]];
}
}
}
50 changes: 50 additions & 0 deletions game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
document.addEventListener('DOMContentLoaded', () => {
const rows = document.querySelectorAll('.row'); // Получаем все ряды

// Создаем игроков
const player1 = new Player('Player 1');
const player2 = new Player('Player 2');

// Создаем колоду и добавляем карты (12 карт)
const deck = new Deck();
deck.addCard(new Card('Masik', 5));
deck.addCard(new Card('Tubik', 3));
deck.addCard(new Card('Skuf', 4));
deck.addCard(new Card('Altushka', 7));
deck.addCard(new Card('Sheikh', 6));
deck.addCard(new Card('Alkash', 4));
deck.addCard(new Card('ITishnik', 6));
deck.addCard(new Card('Fifa', 5));
deck.addCard(new Card('Vaper', 3));
deck.addCard(new Card('TikToker', 4));
deck.addCard(new Card('Khalif', 6));
deck.addCard(new Card('GameDev', 8));
deck.shuffle();

// Игроки берут по 6 карт
for (let i = 0; i < 6; i++) {
player1.drawCard(deck);
player2.drawCard(deck);
}

// Рендеринг карт
let cardIndex = 0;

rows.forEach((row, rowIndex) => {
console.log(`Рендеринг для ряда ${rowIndex + 1}`);
for (let i = 0; i < 3; i++) {
if (rowIndex < 2) {
if (player1.hand[cardIndex]) {
console.log(`Добавление карты ${player1.hand[cardIndex].name} для Player 1 в ряд ${rowIndex + 1}`);
row.appendChild(player1.hand[cardIndex].render());
}
} else {
if (player2.hand[cardIndex - 6]) {
console.log(`Добавление карты ${player2.hand[cardIndex - 6].name} для Player 2 в ряд ${rowIndex + 1}`);
row.appendChild(player2.hand[cardIndex - 6].render());
}
}
cardIndex++;
}
});
});
20 changes: 20 additions & 0 deletions player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Player {
constructor(name) {
this.name = name;
this.hand = [];
}

drawCard(deck) {
const card = deck.drawCard();
if (card) {
this.hand.push(card);
}
}

playCard(index) {
if (index < this.hand.length) {
return this.hand.splice(index, 1)[0];
}
return null;
}
}

0 comments on commit 3838e96

Please sign in to comment.