用 C++ 编写扑克牌计算器

问题描述 投票:0回答:2

我已经用 C++ 为 5 张牌扑克牌计算器编写了几百行代码来练习使用类。这是我编写的成员函数的一部分,用于比较都是 TwoPairs 的两只手。我已经彻底测试了所有布尔函数,除了比较 TwoPairs、OnePairs 和 HighCard 手牌的函数外,它们都有效。

// If both hands are TwoPairs, the highest value pairs are compared.
else if (m_rank == TwoPair) {
  if (m_firstPairPoints > otherHand.m_firstPairPoints) {
    return 1;
  }
  else if (m_firstPairPoints < otherHand.m_firstPairPoints) {
    return -1;
  }
  // If the highest pairs are equal, the next lowest pairs are
  // compared.
  else if (m_firstPairPoints == otherHand.m_firstPairPoints) {
    if (m_secondPairPoints > otherHand.m_firstPairPoints) {
      return 1;
    }
    else if (m_secondPairPoints < otherHand.m_secondPairPoints) {
      return -1;
    }
    // If both pairs are equal, the kickers are compared.
    else if (m_secondPairPoints == otherHand.m_secondPairPoints) {
      if (m_lastCardPoints > otherHand.m_lastCardPoints) {
        return 1;
      } 
      else if (m_lastCardPoints < otherHand.m_lastCardPoints) {
        return -1;
      }
      else {
        return 0;
      }
    }
  }  
}

这是布尔函数本身,它设置我在上面输入的比较函数片段中使用的成员变量的值:

// Implementation of the isTwoPair bool function.
bool PokerHand::isTwoPair(){
  bool checkVar = true;

  // If the single card is the first element.
  if (m_cards[0].points() != m_cards[1].points() &&
      m_cards[0].points() != m_cards[3].points() &&
      m_cards[1].points() == m_cards[2].points() &&
      m_cards[3].points() == m_cards[4].points()) {
    m_rank = TwoPair;
    m_firstPairPoints = m_cards[3].points();
    m_secondPairPoints = m_cards[1].points();
    m_lastCardPoints = m_cards[0].points();
  }

  // If the single card is in the middle.
  else if (m_cards[0].points() == m_cards[1].points() &&
      m_cards[2].points() != m_cards[0].points() &&
      m_cards[2].points() != m_cards[3].points() &&
      m_cards[3].points() == m_cards[4].points()) {
    m_rank = TwoPair;
    m_firstPairPoints = m_cards[3].points();
    m_secondPairPoints = m_cards[0].points();
    m_lastCardPoints = m_cards[2].points();
  }

  //If the single card is the last element.
  else if (m_cards[0].points() == m_cards[1].points() &&
      m_cards[2].points() == m_cards[3].points() &&
      m_cards[4].points() != m_cards[0].points() &&
      m_cards[4].points() != m_cards[2].points()) {
    m_rank = TwoPair;
    m_firstPairPoints = m_cards[2].points();
    m_secondPairPoints = m_cards[0].points();
    m_lastCardPoints = m_cards[4].points();
  }
  else {
    checkVar = false;
  }
  return checkVar;
}

所以现在我的问题是,对于 TwoPair、OnePair 和 HighCard 手牌,无论我做什么,当它们属于这 3 个等级之一时,我的测试 cpp 文件总是输出同一手牌获胜(总是返回 1)。即使我可以这么说,公然堆砌甲板。几个小时以来我一直在尝试不同的东西,但我不太确定发生了什么。我什至尝试从头开始多次重写这三个等级的比较函数的 3 个片段。非常欢迎大家提出任何建议。另外,请记住,如果不是很明显,我仍然是一个新手,并且对我可以在这个项目中使用的内置函数有限制。

c++ poker
2个回答
1
投票

你让自己变得太难了。你错过了一个中间步骤:计算每个等级你有多少张牌,忽略花色。所以你会得到一个

std::array<int, 13> ranktotal
(或者
int [13]
如果你的老师/书已经过时了)。

现在,一旦您掌握了这一点,“2 对”手牌就是 2-2-1 的牌列模式,您可以轻松找到前 2 个。同样,葫芦是 2-3。顺子是一系列 5 个 1,中间没有 0。


0
投票

有一种更简单、更有效的评级方法。 请参阅https://codingbigdataalgorithm.com/

© www.soinside.com 2019 - 2024. All rights reserved.