UNPKG

the-game-domain

Version:

Complete domain layer for 'The Game' card game with rich business entities, automatic persistence, action tracking, and advanced game mechanics. Features Game, Player, Card, Deck, Pile entities with repository pattern, turn management, victory/defeat dete

101 lines 2.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Deck = void 0; const Card_1 = require("./Card"); class Deck { constructor(cards, _id) { this._id = _id; this._cards = [...cards]; } get cards() { return this._cards; } get id() { return this._id; } get size() { return this._cards.length; } get isEmpty() { return this._cards.length === 0; } shuffle() { for (let i = this._cards.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); const temp = this._cards[i]; this._cards[i] = this._cards[j]; this._cards[j] = temp; } } drawCard() { if (this.isEmpty) { return undefined; } return this._cards.pop(); } drawCards(count) { const drawnCards = []; for (let i = 0; i < count && !this.isEmpty; i++) { const card = this.drawCard(); if (card !== undefined) { drawnCards.push(card); } } return drawnCards; } addCard(card) { this._cards.push(card); } addCards(cards) { this._cards.push(...cards); } contains(card) { return this._cards.some(c => c.equals(card)); } peekCards(count = 1) { return this._cards.slice(-count); } equals(other) { if (this._cards.length !== other._cards.length) { return false; } for (let i = 0; i < this._cards.length; i++) { const thisCard = this._cards[i]; const otherCard = other._cards[i]; if (!thisCard || !otherCard || !thisCard.equals(otherCard)) { return false; } } return true; } toString() { return `Deck(${this._cards.length} cards)`; } toJSON() { const result = { cards: this._cards.map(card => card.toJSON()) }; if (this._id !== undefined) { result.id = this._id; } return result; } static fromJSON(data) { const cards = data.cards.map(cardData => Card_1.Card.fromJSON(cardData)); return new Deck(cards, data.id); } static createInitialDeck() { const cards = []; for (let value = 2; value <= 99; value++) { cards.push(new Card_1.Card(value)); } const deck = new Deck(cards); deck.shuffle(); return deck; } static createWithCards(cards, id) { return new Deck(cards, id); } } exports.Deck = Deck; //# sourceMappingURL=Deck.js.map