【问题标题】:Why are parameters changing from one function to another in a class?为什么类中的参数从一个函数更改为另一个函数?
【发布时间】:2019-10-23 14:55:32
【问题描述】:

我正在创建一个名为 3 card high card 的程序。我正在使用甲板、卡片和手牌课程。该程序的重​​点是比较玩家手和计算机手。拥有最高牌的人获胜。如果每手牌中最高的两张牌相同,则中间两张牌中最高的牌获胜,如果它们相同,则最低牌中最高的牌获胜。如果没有,那就是平局。我的问题是比较功能在比较两只手的地方。我的电脑高卡和电脑中卡(c.hc 和 c.mc)得到错误结果。排序函数中的结果是正确的,但是当它们在比较函数中被拉出来时,它不是正确的数字。但是,玩家 hc、mc 和 lc 以及计算机 lc 都是正确的。

忽略比较功能中的奇怪单词。我让他们在我的 if 语句中找出程序将获胜者结果拉到哪里。也忽略 hc、mc 和 lc 的结果出现在“获胜者是”这个词之后看起来多么奇怪。我这样做只是为了看看我的奇怪结果是否是由于这些参数中的数字错误造成的,是的,这就是问题所在。

Here is my main.cpp:
#include "3chc.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
  srand(time(0));  
  Deck d;
  d.shuffle();

  cout << "computer: " << endl;
  Hand comp(3);
  comp.dealFrom(d);
  comp.reveal();
  comp.sort();

  cout << "player: " << endl;
  Hand player(3);
  player.dealFrom(d);
  player.reveal();
  player.sort();

  cout << "Winner is " << player.compare(comp, player) << endl;

  return 0;
}



--------------------------------------------

here is my 3chc.h:

//3chc.h Deck, Card, and Hand classes
#include <iostream>
#include <string>


class Card { 
  friend class Deck;
  private:
    int card_index; //card number 0 to 51
    Card(int index) { card_index = index; } //made this private so user can't say card at index 100..can use it because of friend class
  public:
    Card() { card_index = 52; }
    char suit() const;
    char value() const;
    std::string str() const;
    int getValue(std::string c);
};

class Deck {
  private:
    Card cards[52];
    int pos;
  public:
    Deck();
    Card deal() { return cards[pos++]; };
    void shuffle();
    int size() const { return 52 - pos; };
};

class Hand {
  friend class Deck;
  friend class Card;
  private:
    int handSize;
    int hc; //high card
    int mc; //middle card
    int lc; //low card
    Card myCards[];

  public:   
    Hand() { handSize = 1; };
    Hand(int n) { handSize = n; };
    void dealFrom(Deck& d);
    void reveal(); 
    //int getHandValues();   
    int total();
    void sort();
    std::string compare(Hand& c, Hand&p);
};

std::ostream& operator<< (std::ostream& out, const Card& c);


----------------------------------------------

and here is my 3chc.cpp:


//3chc.cpp - Implementations of the Deck, Card, and Hand classes
#include "3chc.h"
#include <cstdlib>



char Card::suit() const {
  static char suits[] = { 'H', 'S', 'D', 'C' };
  //return card_index < 52 ? suits[card_index % 4] : 'X';
  return suits[card_index % 4];
}

char Card::value() const {
  static char values[] = 
    { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
    //return card_index < 52 ? values[card_index / 4] : 'X';
    return values[card_index / 4];
}

std::string Card::str() const {
  std::string s;
  s += value();
  s += suit();
  return s;
}

Deck::Deck() {
  for (int i = 0; i < 52; i ++) {
//    Card c(i);
//    cards[i] = c;
    cards[i] = Card(i);
  }
  pos = 0;
}

void Deck::shuffle() {
  for (int i = 0; i < 52; i++) {
    int j = rand() % 52;
    Card tmp = cards[i];
    cards[i] = cards[j];
    cards[j] = tmp;
  }
  pos = 0;
}

std::ostream& operator<< (std::ostream& out, const Card& c) {
  out << c.str();
  return out;
}

int Card::getValue(std::string c) {
  char v = c[0];
  int val = v - '0';
  if (val > 9) {
    switch(v){
      case 'T':
        val = 10;
        break;
      case 'J':
        val = 11;
        break;
      case 'Q':
        val = 12;
        break;
      case 'K':
        val = 13;
        break;
      case 'A':
        val = 14;
    }
  }
  return val;

}
void Hand::dealFrom(Deck& d) {
  for(int i = 0; i < handSize; i++)
      myCards[i] = d.deal();
}

void Hand::reveal(){ 
  for (int i = 0; i < handSize; i++) 
    std::cout << myCards[i] << " " << std::endl;
}

int Hand::total() {
  int total = 0;
  for (int i = 0; i < handSize; i ++) {
    total += myCards[i].getValue(myCards[i].str());
  }
  return total;

}

void Hand::sort() {

  hc = myCards[0].getValue(myCards[0].str());
  mc = myCards[1].getValue(myCards[1].str());
  lc = myCards[2].getValue(myCards[2].str());
  int temp = 0;

  if (mc > hc) {
    temp = mc;
    mc = hc;
    hc = temp;
  };

  if (lc > hc) {
    temp = lc;
    lc = hc;
    hc = temp;
  };

  if (lc > mc) {
    temp = lc;
    lc = mc;
    mc = temp;
  };



  std::cout << "hc = " << hc << ", mc = " << mc << ", lc = " << lc << std::endl;


}

std::string Hand::compare(Hand& c, Hand&p) {
  std::string winner;



  while (c.hc == p.hc) {
    if (c.mc == p.mc) {
      if (c.lc == p.lc){
        winner = "tie";
      } else if (c.lc > p.lc) {
        winner = "cat";
      } else if (c.lc < p.lc) {
        winner = "pig";
      }
    } else if (c.mc > p.mc) {
      winner = "cell";
    } else if (c.mc < p.mc) {
      winner = "penguin";
    }
  }

  if (c.hc > p.hc) {
    winner = "computer";
  } 
  if (c.hc < p.hc) {
    winner = "player";
  }

  std::cout << "c hc = " << c.hc << " mc= " << c.mc << " lc= " << c.lc << std::endl;

  std::cout << "p hc = " << p.hc << " mc= " << p.mc << " lc= " << p.lc << std::endl;

  return winner;


}

我希望 c.hc 和 c.mc 等于它们在排序函数中的值。如果我没有正确地写这篇文章,我深表歉意。我以前从来没有在这里发过帖子。这项作业将于今晚午夜到期,我无法在本网站的其他任何地方找到我的问题的答案。非常感谢您的帮助。

【问题讨论】:

  • 我无法一目了然地看到问题,但是在带有调试器的 IDE 中解决这个问题可能会让您通过单步执行快速发现问题。
  • Card myCards[]; 是一个指针,而不是一个数组。您不会为它分配内存,而是取消引用它。这是未定义的行为,可能是您问题的根源。你可以使用 std::vector 来修复它。
  • 我会做两件事。首先,我需要显示函数输出 hc、mc 和 lc 以确保其正常工作。其次,我会在 compare 方法中添加调试语句。这可能会帮助您在几分钟内找到问题。
  • 尝试用std::vector&lt;Card&gt; myCards; 代替Card myCards[];Hand() { handSize = 1; myCards.resize(1); }Hand(int n) { handSize = n; myCards.resize(n); }
  • 为什么手的大小是动态的?排序功能仅适用于 3 张卡片。您可以使用 3 的固定手大小。

标签: c++


【解决方案1】:

Card myCards[]; 是一个指针,而不是一个数组。您不会为它分配内存,而是取消引用它。这是未定义的行为,可能是您问题的根源。

使用std::vector&lt;Card&gt; myCards; 代替Card myCards[]; 并在构造函数中设置向量大小

Hand() { handSize = 1; myCards.resize(1); }

Hand(int n) { handSize = n; myCards.resize(n); }

排序功能仅适用于 3 张卡片。您可以使用固定的 3 手大小作为快速修复

Card myCards[3];

而不是使用std::vector 进行修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2018-11-13
    相关资源
    最近更新 更多