Javascript 对象的字符串表示-卡片组

Javascript 对象的字符串表示-卡片组,javascript,card,deck.js,Javascript,Card,Deck.js,我希望构建一个包含两个字符串变量的函数,并将组合返回到卡片组的每一张卡片 playCard({ suit: 'HEARTS', value: 2 }) to return 2♥ playCard({ suit: 'SPADES', value: 10 }) to return T♠ playCard({ suit: 'SPADES', value: 11 }) to return J♠ 您可以使用2个简单的查找表或关联数组来完成此操作 const suitsMap={

我希望构建一个包含两个字符串变量的函数,并将组合返回到卡片组的每一张卡片

    playCard({ suit: 'HEARTS', value: 2 }) to return 2♥
    playCard({ suit: 'SPADES', value: 10 }) to return T♠
    playCard({ suit: 'SPADES', value: 11 }) to return J♠

您可以使用2个简单的查找表或关联数组来完成此操作

const suitsMap={
“红心”:♥',
“黑桃”:♠'
//等
}
常量值映射={
2 : '2',
10:‘T’,
11:‘J’
//等
}
功能游戏卡({value,suit}){
返回值map[value]+suitsMap[suit];
}
log(游戏卡({suit:'HEARTS',值:2}))
log(playCard({suit:'SPADES',value:10}))

log(playCard({suit:'SPADES',value:11}))
我写了一个纸牌游戏,与我的朋友们分享,他们在疫情爆发前经常亲自玩纸牌游戏。它使用firebase来实现玩家之间的实时状态,并使用这些欢迎您加入的卡片和甲板类

将卡片呈现为字符串(
asString()
)的答案与完全可以接受的已发布答案相匹配,在地图中查找西装和价值

我们有时会有太多玩家玩大型的浪费纸牌的游戏,所以我在纸牌对象中添加了一个
deckId
,并让Deck对象通过多个“鞋子”进行交易

class Card {
  constructor(suit, value, deckId) {
    this.suit = suit
    this.value = value
    this.deckId = deckId
  }

  static isEqual(card) {
    return this.value === card.value && this.suit === card.suit && this.deckId === card.deckId
  }

  asString() {
    const valueNames = { 1:'A', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 10:'19', 11:'J', 12:'Q', 13:'K' }
    // feel free to replace these with unicode equivalents (my UI uses images)
    const suitNames = { 'h': 'hearts', 'd': 'diamonds', 'c':'clubs', 's':'spades' }
    return `${valuesNames[this.value]} of ${suitNames[this.suit]}`
  }
}

class Deck {
  constructor() {
    this.deckId = this.randomId()
    this.cards = this.freshDeck()
  }

  static randomId () {
    let chars ='abcdefghijklmnopqrstuvwxyz0123456789'.split('')
    this.shuffle(chars)
    return chars.join('').substring(0,8)
  }

  freshDeck () {
    let cards = []
    let suits = ['h', 'c', 'd', 's']
    suits.forEach(suit => {
      for (let value = 1; value <= 13; value++) {
        cards.push(new Card(suit, value, this.deckId))
      }
    })
    return cards
  }

  // fischer-yates shuffle
  static shuffle (array) {
    let currentIndex = array.length, temp, randomIndex;
    while (0 !== currentIndex) {
      randomIndex = Math.floor(Math.random() * currentIndex)
      currentIndex -= 1
      temp = array[currentIndex]
      array[currentIndex] = array[randomIndex]
      array[randomIndex] = temp
    }
  }

  shuffle () {
    Deck.shuffle(this.cards)
    return this
  }

  dealOne () {
    if (this.cards.length === 0) {
      this.deckId = this.randomId()
      this.cards = this.freshDeck()
      this.shuffle()
    }
    let card = this.cards[0]
    this.cards = this.cards.slice(1)
    return card
  }
}
类卡{
建造师(西装、价值、身份证){
这套衣服
this.value=value
this.deckId=deckId
}
静态等质量(卡){
返回this.value===card.value&&this.suit===card.suit&&this.deckId===card.deckId
}
asString(){
const valueNames={1:'A',2:'2',3:'3',4:'4',5:'5',6:'6',7:'7',8:'8',9:'9',10:'19',11:'J',12:'Q',13:'K'}
//可以用unicode等价物替换它们(我的UI使用图像)
const suitNames={'h':'hearts','d':'diamonds','c':'clubs','s':'spades'}
返回${suitNames[this.suit]}的${valuesNames[this.value]}`
}
}
甲板{
构造函数(){
this.deckId=this.randomId()
this.cards=this.freshDeck()
}
静态随机ID(){
让chars='abcdefghijklmnopqrstuvxyz012456789'.拆分('')
这个。洗牌(chars)
返回字符连接(“”).substring(0,8)
}
freshDeck(){
让卡片=[]
让西装=['h','c','d','s']
套装。forEach(套装=>{

对于(让value=1;value
{suit:'HEARTS',value:1}
-这甚至不是一个正确的函数。值查找只需为1,10,11,12,13…然后使用
值[value]| value.toString()
@JaromandaX你可以这样做,是的。我个人更喜欢一个只有13个值的查找表的简单性和透明性。很好的一点-我是老派-在不失去太多清晰性的情况下最小化代码(值“map”的一个好的变量名会让意图变得清晰)当前位置谢谢。很好地解释了这一点!非常感谢appreciated@learnjspsde只有1个参数。它是您的对象
{suit:'HEARTS',value:2}
它只是解构对象属性。请参阅-请参阅更新的答案。