神武手游ios:排名扑克手(list of poker hands by rank)

我正在构建一个函数PokerHand(),它应该将 5 张牌手作为字符串,并根据 Texas Holdem Rules 对它们进行评分。我已经编写了代码,以便它首先根据 rank 进行排序。因此手const handOne = ('AC 4S 5S 8C AH')变为let sortedHandOne = ["4S", "5S", "8C", "AC", "AH"](有效),然后将其拆分为 rank 和相应的西装数组 (不起作用)。当我运行

下面是代码:

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();
1

我最终发现了你的问题。代码更改为给你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();
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();

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(111)
辐射四武器代码大全:大全景平移视频(panning gif)
上一篇
Chinese国产蓝摄gayvideomen:使用iPhoneX的远摄或双摄像头自动对焦图像模糊
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(81条)