帮助小C ++游戏

时间:2011-02-09 16:58:19

标签: c++ vector

我正在写一个在终端上运行的纸牌游戏。截至目前,该程序编译并运行并向玩家提供四张牌,放置在四列中,并允许他们按0以获得更多牌,这些牌被添加到列上。随着列的增长,它的输出应该放在一个向量中(实际上是向量的向量)。

理想情况下,在必要时收集更多牌之后,玩家输入1-4之间的数字以选择他们想要与其他人比较的列。然后,它应该将该列的顶部卡与其他顶部卡进行比较,看看是否可以删除一个。这是我遇到麻烦的部分。首先,我不确定我是否正确地将卡片输入到矢量矢量中,我不确定如何将它们相互比较。我尝试过使用类似的东西:

column[2].back().getSuit()访问第二列顶部卡片的套装,然后给它一个数值并将其与另一个的套装进行比较。 我做了类似的事情来比较牌的排名,但我没有运气。

有人能用我或你自己的代码给我看一个例子吗? 我应该如何正确地比较每列中顶级牌的套装和等级?

到目前为止,这是我的代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

enum suits 
{
    diamond, club, heart, spade
};

class Card
{
private:
    int rank;
    suits suit;
public:
    Card();
    Card(suits, int);
    int getRank() { return rank; }
    suits getSuit() { return suit; }
    void setRank(int rankvalue) { rank = rankvalue; }
    void setSuit(suits suitvalue) { suit = suitvalue; }
};

ostream & operator<<(ostream &, Card);

Card::Card()
{
    rank = 1;
    suit = spade;
}

Card::Card(suits suitvalue, int rankvalue)
{
    rank = rankvalue;
    suit = suitvalue;
}

ostream & operator<<(ostream & out, Card aCard)
{
    switch (int rank = aCard.getRank())
    {
        case 14: out << "Ace"; break;
        case 11: out << "Jack"; break;
        case 12: out << "Queen"; break;
        case 13: out << "King"; break;
        default: out << rank;
    }

    switch (suits suit = aCard.getSuit())
    {
        case diamond: out << " of Diamonds"; break;
        case spade: out << " of Spades"; break;
        case heart: out << " of Hearts"; break;
        case club: out << " of Clubs"; break;
    }

    return out;
}

class RandomInteger 
{
public: 
    RandomInteger();
    unsigned int operator() (unsigned int max);
};

RandomInteger::RandomInteger()
{
    srand(time(0));
}

unsigned int RandomInteger::operator()(unsigned int max)

{
    unsigned int rval = rand();
    return rval % max;
}

RandomInteger randomizer;

class Deck
{
    Card cards[52];
    int topCard;
public:
    Deck();
    void shuffle();
    bool isEmpty() { return topCard <= 0; }
    Card draw();
};

extern RandomInteger randomizer;

Deck::Deck()
{
    topCard = 0;
    for (int i = 1; i <= 13; i++)
    {
        Card c1(diamond, i), c2(spade, i), c3(heart, i), c4(club, i);
        cards[topCard++] = c1;
        cards[topCard++] = c2;
        cards[topCard++] = c3;
        cards[topCard++] = c4;
    }
}

Card Deck::draw()
{
    if (!isEmpty())
        return cards[--topCard];
    else
    {
        Card spadeAce(spade, 1);
        return spadeAce;
    }
}

void Deck::shuffle()
{
    random_shuffle(cards, cards+52, randomizer);
}

class Player
{
public:
    Player();
    void print();
    Card draw(Deck &);
    typedef vector<Card> cards;
    vector<cards> column;
};

//ostream & operator<<(ostream &, Player&);

Player::Player()
{
    column.push_back(vector<Card>());
    column.push_back(vector<Card>());
    column.push_back(vector<Card>());
    column.push_back(vector<Card>());
}

Card Player::draw(Deck & aDeck)
{
    for (int i = 0; i < 4; i++)
        column[i].push_back(aDeck.draw());
}

void Player::print()
{
    cout << "Col 1 \t\t Col 2 \t\t Col 3 \t\t Col 4 \n";
    bool more = true;
    for (int j = 0; more; j++)
    {
        more = false;
        for (int i = 0; i < 4; i++)
            if (j < column[i].size())
        {
        cout << column[i][j] << "\t";
        more = true;
        }
            else
            cout << "\t\t";
    cout << endl;
    }
}

int main()
{
    Deck deck;
    deck.shuffle();

    Player player;
    player.draw(deck);
    //while (!deck.isEmpty())
    //{
    cout << "Enter a column number (0 to draw four new cards): " << endl;
    //}
    player.print();

    int input;
    int i;
    vector<vector<Card> > columns(4);
    while (cin >> input)
    if (input == 0 )
    {
        player.draw(deck);
        player.print();
        columns.push_back(vector<Card>());
        columns.push_back(vector<Card>());
    columns.push_back(vector<Card>());
        columns.push_back(vector<Card>());
    }
        else while (cin >> input)
            if (input == 1)
            {
            for ( i = 0; i > 4; i++)
                {
                        columns.push_back(vector<Card>());
            }
            for ( i = 0; i > 4; i++)
                    {
                    columns[0].back().getSuit();
                columns[1].back().getSuit();
                columns[2].back().getSuit();
                columns[3].back().getSuit();
            }

            }



}

任何建议,指示,提示都很棒,谢谢。

2 个答案:

答案 0 :(得分:2)

您无需将套装翻译为数值进行比较。您可以直接比较枚举类型。

尝试为Card s添加比较运算符:

bool operator==(const Card& a, const Card& b) {
  return a.getRank() == b.getRank() && a.getSuit() == b.getSuit();
}

要实现此目的,您必须使用getRank标记getSuitconst方法:

int getRank() const { return rank; }
suits getSuit() const { return suit; }

现在你应该能够简单地比较任何两张牌,包括两列牌“顶部”的牌。例如:

if (columns[1].back() == columns[2].back()) { ... }

请注意,如果向量为空,back将无效。

答案 1 :(得分:1)

Hia,你问了一些建议,所以我认为我提出了一些建议

首先,我喜欢保留枚举类型和将字符串一起返回的函数,以防我稍后添加到枚举类型(在这种情况下不太可能是admitadly)

class suits{
public:
  enum type{
    diamond, club, heart, spade,
  };

  static
  std::string get_string(const type& t)
  {
    switch (t) {
    case diamond: return"Diamonds";
    case spade: return"Spades"; 
    case heart: return"Hearts";
    case club: return"Clubs";
    }
    throw("invalid suit");  //this is a bit ugly - but you get the idea
  }
};

然后,您可以参考suits::spadesuits::get_string(suits::spade)等更清晰的内容。

然后我会在自己的班级中找到诉讼和号码,然后你可以在这里进行比较:

class CardSuit
{
private:
  suits::type m_suit;
protected:
  std::string get_string() const {return suits::get_string(m_suit);}
public:
  CardSuit(const suits::type& t) : m_suit(t) {}
  void setSuit(const suits::type& t)  { m_suit = t;}
  const suits::type& getSuit() const {return m_suit;}
  bool operator<(CardSuit cs)
  {
    return m_suit<cs.getSuit(); //You must have the enum in the suit order
  }
};

然后

  CardSuit club(suits::club);
  CardSuit spade(suits::spade);

  if (club<spade) 
    std::cout<<"true"<<std::endl;
  else
    std::cout<<"false"<<std::endl;

返回true

然后您可以继承CardSuit类

class Card : public CardSuit
{
private:
  int rank;
public:
  Card();
  Card(suits::type, int);
  int getRank() { return rank; }
  void setRank(int rankvalue) { rank = rankvalue; }
  std::string get_suit() const {return CardSuit::get_string();}

 bool operator<(const Card& c)
  {
    if (CardSuit::operator<(c) )
      return true; 
    if (rank<c.getRank()) 
      return true;
    else return false;
  }
};

这样

  Card c1(suits::club, 4);
  Card c2(suits::club, 5);
  Card c3(suits::diamond, 5);

  if (c2<c1) 
    std::cout<<"true"<<std::endl;
  else
    std::cout<<"false"<<std::endl;

  if (c3<c1) 
    std::cout<<"true"<<std::endl;
  else
    std::cout<<"false"<<std::endl;

分别返回false和true

最后 utstream运算符就像

ostream & operator<<(ostream & out, Card aCard)
{
  switch (int rank = aCard.getRank())
    {
    case 14: out << "Ace"; break;
    case 11: out << "Jack"; break;
    case 12: out << "Queen"; break;
    case 13: out << "King"; break;
    default: out << rank;
    }

  out << " of "<< aCard.get_suit();

  return out;
}

(我也会制作一个与CardSuit类相似的等级类 - 在这种情况下它甚至更整洁。)