我正在构建一个函数PokerHand(),该函数应使用5张牌作为字符串,并根据Texas Holdem规则对其进行评分。我已经编写了代码,以便首先根据等级对其进行排序。这样,手const handOne = ('AC 4S 5S 8C AH')变为let sortedHandOne = ["4S", "5S", "8C", "AC", "AH"](有效),然后拆分为一组等级和相应的花色(无效)。当我运行suitsAndRank函数时,我期望使用rankArray = ["4", "5", "8", "A", "A"]suitArray = ["C", "C", "H", "S", "S"],但是会得到空数组。我不知道为什么。

这是代码:

function PokerHand() {
    //get ranks of hands

    const handOne = ('AC 4S 5S 8C AH');
    //const handTwo = ('4S 5S 8C AS AD');

    let rankArray = [];
    let suitArray = [];

    // let rankArrayTwo = [];
    // let suitArrayTwo = [];

    const suits = ["C", "D", "H", "S"]
    const ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]

    let arrayHandOne = handOne.split(" ");
    //let arrayHandTwo = handTwo.split(" ");

    function sorted() {
    let sortedHand = [];
    for (let i = 0; i < ranks.length; i++) {
        for (let j = 0; j < arrayHandOne.length; j++ ) {
            if (ranks[i] === arrayHandOne[j].charAt(0)) {
                sortedHand.push(arrayHandOne[j])
            }
        }
    }
        return sortedHand;
    }

    console.log(sorted())

    let sortedHandOne = sorted(arrayHandOne);
    //let sortedHandTwo = sortedHand(arrayHandTwo);
    console.log(sortedHandOne)
    function suitAndRank(sortedHandOne) {
        console.log(sorted)
        for (i = 0; i< sortedHandOne.length; i++) {
            let sted = sortedHandOne;
            rankArray.push(sted[i].charAt(0))
            suitArray.push(sted[i].charAt(1))
        }
    }

    console.log(rankArray, suitArray)

    function countSuites(suitArray) {
        let suitCount = {};
        suitArray.forEach(function(x) { suitCount[x] = (suitCount[x] || 0)+1; });
            return suitCount;
    }

    function countRanks(rankArray) {
        let rankCount = {};
        rankArray.forEach(function(x) { rankCount[x] = (rankCount[x] || 0)+1; });
            return rankCount;
    }

    function isFlush() {
        let cS = countSuites(suitArray);
        if(Object.keys(cS).find(key => cS[key] === 5)) {
            return true;
        } else {
            return false;
        }
    }

    function isStraight() {
        let index = ranks.indexOf(rankArray[0])
        let ref = ranks.slice(index, index + 5).join("")
        let section = rankArray.slice(0).join("")
            if (section === "10JQKA" && section === ref) {
                return "ROYALSTRAIGHT";
            } else if (section === "A2345" || section === ref) {
                return "STRAIGHT";
            }else {
                return "FALSE";
            }
    }

    function pairs() {
        let rS = countRanks(rankArray)
        return Object.keys(rS).filter((key) => rS[key] === 2).length
    }


    function whichHand() {
        let rS = countRanks(rankArray)
        if (isFlush() === true && isStraight() === "ROYALSTRAIGHT") {
            console.log('Royal Flush')
        } else if (isFlush() === true && isStraight() === "STRAIGHT") {
            console.log("Straight Flush")
        } else if (Object.keys(rS).find(key => rS[key] === 4)) {
            console.log("Four of a Kind")
        } else if (Object.keys(rS).find(key => rS[key] === 3) && pairs() === 2) {
            console.log("Full House")
        } else if (isFlush === true) {
            console.log("Flush")
        } else if (isStraight === "STRAIGHT") {
            console.log("Straight")
        } else if (Object.keys(rS).find(key => rS[key] === 3)) {
            console.log("Three of a Kind")
        } else if (pairs() === 2) {
            console.log("Two Pair")
        }else if(pairs() === 1) {
            console.log("Pair")
        }else {
            console.log('High Card', rankArray[rankArray.length-1])
        }
    }

    return whichHand();
}

// const hands = ['Royal flush', 'Straight flush', 'Four of a kind', 'Full house',
//     'Flush', 'Straight', 'Three of a kind', 'Two pairs', 'Pair', 'High card']
//     //compare ranks of hands and return results

PokerHand();

最佳答案

我终于找到了你的问题。更改了代码,以提供rankArraysuitArray。有两个问题:


您没有调用函数suitAndRank(sortedHandOne);
最后的whichHand()函数中的代码有两个问题,您没有在其中加上括号。


请参见下面带有输出的完整代码:



function PokerHand() {
  //get ranks of hands

  const handOne = "AC 4S 5S 8C AH";
  //const handTwo = ('4S 5S 8C AS AD');

  let rankArray = [];
  let suitArray = [];

  // let rankArrayTwo = [];
  // let suitArrayTwo = [];

  const suits = ["C", "D", "H", "S"];
  const ranks = [
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "10",
    "J",
    "Q",
    "K",
    "A"
  ];

  let arrayHandOne = handOne.split(" ");
  //let arrayHandTwo = handTwo.split(" ");

  function sorted() {
    let sortedHand = [];
    for (let i = 0; i < ranks.length; i++) {
      for (let j = 0; j < arrayHandOne.length; j++) {
        if (ranks[i] === arrayHandOne[j].charAt(0)) {
          sortedHand.push(arrayHandOne[j]);
        }
      }
    }
    return sortedHand;
  }

  console.log(sorted());

  let sortedHandOne = sorted(arrayHandOne);
  //let sortedHandTwo = sortedHand(arrayHandTwo);
  console.log(sortedHandOne);
  function suitAndRank(sortedHandOne) {
    console.log(sorted);
    for (let i = 0; i < sortedHandOne.length; i++) {
      let sted = sortedHandOne;
      rankArray.push(sted[i].charAt(0));
      suitArray.push(sted[i].charAt(1));
    }
  }

  suitAndRank(sortedHandOne);

  console.log(rankArray, suitArray);

  function countSuites(suitArray) {
    let suitCount = {};
    suitArray.forEach(function(x) {
      suitCount[x] = (suitCount[x] || 0) + 1;
    });
    return suitCount;
  }

  function countRanks(rankArray) {
    let rankCount = {};
    rankArray.forEach(function(x) {
      rankCount[x] = (rankCount[x] || 0) + 1;
    });
    return rankCount;
  }

  function isFlush() {
    let cS = countSuites(suitArray);
    if (Object.keys(cS).find(key => cS[key] === 5)) {
      return true;
    } else {
      return false;
    }
  }

  function isStraight() {
    let index = ranks.indexOf(rankArray[0]);
    let ref = ranks.slice(index, index + 5).join("");
    let section = rankArray.slice(0).join("");
    if (section === "10JQKA" && section === ref) {
      return "ROYALSTRAIGHT";
    } else if (section === "A2345" || section === ref) {
      return "STRAIGHT";
    } else {
      return "FALSE";
    }
  }

  function pairs() {
    let rS = countRanks(rankArray);
    return Object.keys(rS).filter(key => rS[key] === 2).length;
  }

  function whichHand() {
    let rS = countRanks(rankArray);
    if (isFlush() === true && isStraight() === "ROYALSTRAIGHT") {
      console.log("Royal Flush");
    } else if (isFlush() === true && isStraight() === "STRAIGHT") {
      console.log("Straight Flush");
    } else if (Object.keys(rS).find(key => rS[key] === 4)) {
      console.log("Four of a Kind");
    } else if (Object.keys(rS).find(key => rS[key] === 3) && pairs() === 1) {
      console.log("Full House");
    } else if (isFlush() /*First issue*/ === true) {
      console.log("Flush");
    } else if (isStraight() /*Second issue*/ === "STRAIGHT") {
      console.log("Straight");
    } else if (Object.keys(rS).find(key => rS[key] === 3)) {
      console.log("Three of a Kind");
    } else if (pairs() === 2) {
      console.log("Two Pair");
    } else if (pairs() === 1) {
      console.log("Pair");
    } else {
      console.log("High Card", rankArray[rankArray.length - 1]);
    }
  }

  return whichHand();
}

// const hands = ['Royal flush', 'Straight flush', 'Four of a kind', 'Full house',
//     'Flush', 'Straight', 'Three of a kind', 'Two pairs', 'Pair', 'High card']
//     //compare ranks of hands and return results

PokerHand();

08-17 08:52