扑克手排名

时间:2018-12-05 08:36:33

标签: javascript function parameters poker

我正在构建一个函数PokerHand(),该函数应使用5张牌作为字符串并根据德州扑克规则对其进行评分。我已经编写了代码,以便首先根据等级对其进行排序。这样,手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();

2 个答案:

答案 0 :(得分:2)

我终于找到了你的问题。代码已更改为给您rankArraysuitArray。有两个问题:

  1. 您没有调用函数suitAndRank(sortedHandOne);
  2. 在最后的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();

答案 1 :(得分:0)

在下面找到更正的代码,问题在注释中描述

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(arrayHand) { // added the parameter required
    let sortedHand = [];
    for (let i = 0; i < ranks.length; i++) {
        for (let j = 0; j < arrayHand.length; j++ ) { 
            if (ranks[i] === arrayHand[j].charAt(0)) {
                sortedHand.push(arrayHand[j])
            }
        }   
    }
    return sortedHand;
}


let sortedHandOne = sorted(arrayHandOne); // sorted() did not take any parameters before
//let sortedHandTwo = sorted(arrayHandTwo);
function suitAndRank(sortedHand) { // changed the param name
    for (i = 0; i< sortedHand.length; i++) { // removed sted as is not really needed
        rankArray.push(sortedHand[i].charAt(0))
        suitArray.push(sortedHand[i].charAt(1)) 
    } 
}

suitAndRank(sortedHandOne); // was not being called before
// suitAndRank(sortedHandTwo);

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) { // problem here as isFlush is a function not a variable, otherwise it returns undefined
        console.log("Flush")
    } else if (isStraight() === "STRAIGHT") { // problem again, isStraight is a function too so it should be isStraight() instead of isStraight, otherwise it returns undefined
        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();