cardation
Version:
fundation of card games, card model
86 lines (85 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const CardError_1 = require("../../error/CardError");
const Card_1 = require("../card/Card");
const Collection_1 = require("./Collection");
/**
* A hand is a collection of cards.
* @todo 可以考慮再做一個IHand
*/
class Hand extends Collection_1.default {
/**
* Form a hand from a card array.
* @param hands
* @returns
*/
static from(...hands) {
const allCards = [];
hands.forEach((hand) => {
const cardArray = hand.getDuplicatedCardArray();
allCards.push(...cardArray);
});
return new Hand(allCards);
}
constructor(cardArray) {
super();
if (Array.isArray(cardArray)) {
this.pushCard(...cardArray);
}
if (cardArray instanceof Card_1.default) {
throw new CardError_1.default('[Hand][constructor]: Card is not acceptable as a parameter here!');
}
}
/**
* Get a baccarat based point of the hand.
* @returns {number} the point of the hand
*/
getPoint() {
const cards_array = this.getDuplicatedCardArray();
let result = 0;
for (const card of cards_array) {
result += card.getPoint();
}
return result % 10;
}
pushCard(...card) {
this.getCardArray().push(...card);
}
/**
* Get the first card in the array.
* @returns {Card[]} the first card of the hand
*/
getFirstCard() {
const cards_array = this.getDuplicatedCardArray();
if (cards_array.length) {
return cards_array[0];
}
return undefined;
}
/**
* Get the last card in the array.
* @returns {Card} the last card of the hand
*/
getLastCard() {
const cards_array = this.getDuplicatedCardArray();
if (cards_array.length) {
return cards_array[cards_array.length - 1];
}
return undefined;
}
// 或者deal
play() {
throw new Error('Method not implemented.');
}
// public recycle(collector: Collection): void {
// collector.pushCard(...this.getDuplicatedCardArray())
// this.clear()
// }
/**
* Clear the hand, all cards will be removed.
*/
clear() {
this.getCardArray().length = 0;
}
}
exports.default = Hand;