【问题标题】:String representation of an object - deck cards对象的字符串表示 - 套牌
【发布时间】:2021-01-26 12:40:03
【问题描述】:

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

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

【问题讨论】:

  • { suit: 'HEARTS', value: 1 } - 这甚至不是一个合适的功能

标签: javascript card deck.js


【解决方案1】:

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

const suitsMap = {
  'HEARTS' : '♥',
  'SPADES' : '♠'
  // etc
}

const valuesMap = {
  2 : '2',
  10: 'T',
  11: 'J'
  // etc
}

function playCard({value,suit}){
  return valuesMap[value] + suitsMap[suit];
}

console.log(playCard({ suit: 'HEARTS', value: 2 }))
console.log(playCard({ suit: 'SPADES', value: 10 }))
console.log(playCard({ suit: 'SPADES', value: 11 }))

playCard 也可以这样写:

function playCard(card){
  return valuesMap[card.value] + suitsMap[card.suit];
}

在我上面的示例中,它只是使用了对象解构赋值:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

【讨论】:

  • 查找值只需为 1,10,11,12,13 ...然后您使用values[value]||value.toString()
  • @JaromandaX 你可以这样做,是的。我个人更喜欢只有 13 个值的查找表的简单性和透明度。
  • 好点 - 我是老派 - 最小化代码而不会失去太多清晰度(并且值“map”的好变量名称将使意图清晰):p
  • 谢谢。很好地解释了这一点!非常感谢
  • @learnjspsde 只有 1 个参数。它是您的对象{ suit: 'HEARTS', value: 2 } 它只是解构对象属性。请参阅developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… - 查看更新的答案。
【解决方案2】:

写了一款卡牌游戏,分享给疫情前曾经亲自玩过的朋友。它使用 firebase 来获取玩家之间的实时状态,并使用您欢迎的 Card 和 Deck 类。

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

有时我们有太多的玩家玩大型浪费纸牌的游戏,所以我在 Card 对象中添加了一个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
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 2016-05-19
    相关资源
    最近更新 更多