javascript 确定函数是否有两对

cgvd09ve  于 2023-01-29  发布在  Java
关注(0)|答案(3)|浏览(118)

试图建立一个视频扑克应用程序,我已经得到了大部分的获胜逻辑做,但我不能把我的头周围的两对。

function isTwoPair() {
  const tempHand = [...playerHand];
  let reduceHand = tempHand.reduce((acc, curVal) => {
    if (curVal.rank in acc) {
      acc[curVal.rank]++;
    } else {
      acc[curVal.rank] = 1;
    }
    return acc;
  }, {});
  const sortedHand = Object.fromEntries(Object.entries(reduceHand).sort());
  for (const [key, value] of Object.entries(reduceHand)) {
    let pairs = 0;
    if (value === 2) {
      pairs++;
      if (pairs === 2) {
        return true;
      }
    }
  }
}

我的想法是使用reduce来确定每个键所具有的值的数量,然后对其进行排序(升序)并循环,如果任何值等于2,则它将pairs变量更新为1,一旦pairs达到2,则它将返回true。
什么是更好的方法,或者说正确的方法,因为这不起作用,在给定的对象数组中找到两个对。
甲板是一个对象阵列,如下所示:

[
 {
    card: "Ah",
    rank: 14,
    suit: "hearts",
    img: "./images/hearts/hearts-A.svg",
    isHold: false,
 },
]
nle07wnf

nle07wnf1#

function isTwoPair() {
  const reduceHand = reduceHandRank();
  let pair = 0;
  for (const [key, value] of Object.entries(reduceHand)) {
    if (value === 2) {
      pair++;
    }
  }
  if (pair === 2) {
    return true;
  }
}

决心会让你到达那里!热dawg!!

niwlg2el

niwlg2el2#

我会计算每张牌的点数。如果点数大于2,则返回false。计算后,过滤值为2的值,如果其长度等于2,则返回

function isTwoPair() {
  let count={}
  for(let i=0;i<playerHand.length;i++){
    let card=playerHand[i]
    if(card.rank in count){
      count[card.rank]+=1
      if(count[card.rank]>2){
        return false
      }
    }else{
      count[card.rank]=1
    }
  }
  return Object.values(count).filter(c=>c==2).length == 2
}

完整示例:

let playerHand = [{
    rank: 14,
    suit: "spades",
    img: "./images/hearts/hearts-A.svg",
    isHold: false,
  },
  {
    rank: 14,
    suit: "hearts",
    img: "./images/hearts/hearts-A.svg",
    isHold: false,
  },
  {
    rank: 14,
    suit: "diamonds",
    img: "./images/hearts/diamonds-A.svg",
    isHold: false,
  },
  {
    rank: 14,
    suit: "clubs",
    img: "./images/hearts/diamonds-A.svg",
    isHold: false,
  },
  {
    rank: 8,
    suit: "hearts",
    img: "./images/hearts/hearts-8.svg",
    isHold: false,
  },
]

function isTwoPair() {
  let count={}
  for(let i=0;i<playerHand.length;i++){
    let card=playerHand[i]
    if(card.rank in count){
      count[card.rank]+=1
      if(count[card.rank]>2){
        return false
      }
    }else{
      count[card.rank]=1
    }
  }
  return Object.values(count).filter(c=>c==2).length == 2
}

console.log(isTwoPair())
playerHand = [{
    rank: 14,
    suit: "hearts",
    img: "./images/hearts/hearts-A.svg",
    isHold: false,
  },
  {
    rank: 14,
    suit: "spades",
    img: "./images/hearts/hearts-A.svg",
    isHold: false,
  },
  {
    rank: 13,
    suit: "clubs",
    img: "./images/hearts/diamonds-A.svg",
    isHold: false,
  },
  {
    rank: 13,
    suit: "diamonds",
    img: "./images/hearts/diamonds-A.svg",
    isHold: false,
  },
  {
    rank: 8,
    suit: "hearts",
    img: "./images/hearts/hearts-8.svg",
    isHold: false,
  },
]
console.log(isTwoPair())
tp5buhyn

tp5buhyn3#

我建议传入playerHand,这样你的函数就可以被多手使用,如果你不熟悉,~~有一个副作用,那就是把不真实的值变成0,而整数保持不变。

function isTwoPair(playerHand) {
  let counts={};
  playerHand.forEach(hand => counts[hand.rank] = ~~counts[hand.rank]+1);
  let sorted=Object.values(counts).sort();
  return sorted[sorted.length-1]==2 && sorted[sorted.length-2]==2
}

相关问题