Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Card {
- imageURL: string;
- value: string;
- suit: string;
- }
- class Hand {
- cards: Card[];
- totalPoints: number;
- constructor(cards: Card[] = []) {
- this.cards = cards;
- this.totalPoints = 0;
- this.calculateTotalPoints();
- }
- addCard(card: Card) {
- this.cards.push(card);
- this.calculateTotalPoints();
- }
- addCards(newCards: Card[]) {
- for (const card of newCards) {
- this.addCard(card);
- }
- }
- calculateTotalPoints() {
- this.totalPoints = this.cards.reduce((acc, card) => {
- let value = card.value === 'ACE' ? 11 : (['JACK', 'QUEEN', 'KING'].includes(card.value) ? 10 : parseInt(card.value));
- return acc + value;
- }, 0);
- /* Adjust for Aces */
- this.cards.filter(card => card.value === 'ACE').forEach(_ => {
- if (this.totalPoints > 21) this.totalPoints -= 10;
- });
- }
- isBusted(): boolean {
- return this.totalPoints > 21;
- }
- }
- export default Hand;
Advertisement
Add Comment
Please, Sign In to add comment