Guest User

Untitled

a guest
Jan 28th, 2025
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. interface Card {
  2.   imageURL: string;
  3.   value: string;
  4.   suit: string;
  5. }
  6.  
  7. class Hand {
  8.   cards: Card[];
  9.   totalPoints: number;
  10.  
  11.   constructor(cards: Card[] = []) {
  12.     this.cards = cards;
  13.     this.totalPoints = 0;
  14.     this.calculateTotalPoints();
  15.   }
  16.  
  17.   addCard(card: Card) {
  18.     this.cards.push(card);
  19.     this.calculateTotalPoints();
  20.   }
  21.  
  22.   addCards(newCards: Card[]) {
  23.     for (const card of newCards) {
  24.       this.addCard(card);
  25.     }
  26.   }
  27.  
  28.   calculateTotalPoints() {
  29.     this.totalPoints = this.cards.reduce((acc, card) => {
  30.       let value = card.value === 'ACE' ? 11 : (['JACK', 'QUEEN', 'KING'].includes(card.value) ? 10 : parseInt(card.value));
  31.       return acc + value;
  32.     }, 0);
  33.  
  34.     /* Adjust for Aces */
  35.     this.cards.filter(card => card.value === 'ACE').forEach(_ => {
  36.       if (this.totalPoints > 21) this.totalPoints -= 10;
  37.     });
  38.   }
  39.  
  40.   isBusted(): boolean {
  41.     return this.totalPoints > 21;
  42.   }
  43. }
  44.  
  45. export default Hand;
Advertisement
Add Comment
Please, Sign In to add comment