生成卡片组 [英] Generating a Deck of Cards

查看:104
本文介绍了生成卡片组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个简单的21点程序。很遗憾,我在生成一副卡牌时遇到问题。

  #include< iostream> 
#include< vector>

using namespace std;

int main(){
vector< char>甲板;
char suit [] = {'h','d','c','s'};
char card [] = {'2','3','4','5','6','7','8','9','10','J' 'Q','K','A'};
for(int j = 0; j <13; j ++){
for(int i = 0; i <4; i ++){
deck.push_back一世]);
}
}

return 0;
}



我知道我的问题是从我尝试分配值'10'一个炭。显然,我不能得到这个编译,但我敢肯定,当我尝试分配卡值到向量甲板我也会得到一个错误,因为我使用变量类型'char'。知道使用什么样的变量类型似乎是杀了我。此外,'deck.push_back(card [j] suit [i]);'是正确的代码来组合卡和西装,或者你必须把卡[j]和西装[i]之间的东西?我很感激,如果你能带给我正确的方向。还有一点小小的注释,这是一个家庭作业的一部分,所以请不要只给我整个代码块。非常感谢您的帮助。

解决方案

尝试创建一套牌套装和卡作为成员,并将其设置为一种类型向量。喜欢

  public class Card {
public:
Card(char suit,char card);
char suit,card;
};

int main(){
vector< Card>甲板;
char suit [] = {'h','d','c','s'};
char card [] = {'2','3','4','5','6','7','8','9','T','J' 'Q','K','A'};
for(int j = 0; j <13; j ++){
for(int i = 0; i <4; i ++){
deck.push_back ],suit [i]));
}
}
return 0;
}

也使用枚举而不是西装和卡片中的字符将使它更清晰。 / p>

I'm trying to make a simple blackjack program. Sadly, I'm having problems right off the bat with generating a deck of cards.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<char> deck;
    char suit[] = {'h','d','c','s'};
    char card[] = {'2','3','4','5','6','7','8','9','10','J','Q','K','A'};
    for (int j=0; j<13; j++) {
    	for (int i=0; i<4; i++) {
    		deck.push_back(card[j] suit[i]);
    	}		
    }

    return 0;
}

I know my problem begins with me trying to assign the value '10' to a char. Obviously I couldn't get this to compile but I'm sure when I try to assign the card values to the vector deck I'll also get an error since I used variable type 'char'. Knowing what kind of variable type to use seems to be killing me. Also, would 'deck.push_back(card[j] suit[i]);' be the correct code to combine the card and suit, or do you have to put something between card[j] and suit[i]? I'd appreciate it if any of you could lead me in the right direction. Also as a little side note, this is part of a homework assignment so please don't just give me entire blocks of code. Thanks for your help.

解决方案

Try to create class of Card with suit and card as a member and set it as a type of vector. Like

public class Card {
 public:
  Card(char suit, char card);
  char suit, card;
};

int main() {
    vector<Card> deck;
    char suit[] = {'h','d','c','s'};
    char card[] = {'2','3','4','5','6','7','8','9','T','J','Q','K','A'};
    for (int j=0; j<13; j++) {
        for (int i=0; i<4; i++) {
                deck.push_back(new Card(card[j],suit[i]));
        }               
    }
    return 0;
}

also using enums instead of chars in suit and card would make it clearer.

这篇关于生成卡片组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆