0

我正在做一个发牌游戏。我无法将字符串解析为组件,以便我跟踪正在绘制的花色和值。

const string rank[] = { "Ace", "2", "3", "4", "5", "6", "7",
                    "8", "9", "10", "Jack", "Queen", "King" };
const string suit[] = { "Clubs", "Diamonds", "Hearts", "Spades" };

string random_card(bool verbose=false) {
  string card;
  card = rank[ rand()%13 ];
  card += " of ";
  card += suit[ rand()%4 ];
  if (verbose)
    cout << card << "\n";
  return card;
}

如何将卡片分解为组件以跟踪表格中的花色和价值?

编辑:我必须为此分配使用一个字符串。我无法更改卡片的生成方式。我必须将字符串解析为组件并跟踪表格中每件西装的值,然后我需要对其进行格式化并打印到标准输出中。

4

2 回答 2

0

不要用字符串来表示卡片,只要定义一个简单的struct

struct Card
{
    int suit;
    int rank;
};
于 2013-09-14T21:29:35.997 回答
0

尝试这样的事情:

string delimiter = " of ";
int delimiter_length = delimiter.size();

string card = "Ace of Clubs";
int del_begin = card.find_first_of(delimiter);
int del_end = del_begin + delimiter_length; 

string rank = card.substr(0, del_begin); 
string suit = card.substr(del_end, card.size() - del_end);
于 2013-09-14T21:52:27.050 回答