随机随机查询

时间:2014-02-22 22:13:18

标签: c++ shuffle poker

制作扑克游戏,做基础知识现在创建了一个带有我的值的矢量结构,将其改组,然后打印出5张“卡片”。我的问题是,当我按“2”再次处理时,它给了我相同的卡片。我只是想让它从我的阵列中抓取另外5张牌,并继续这样做,直到没有足够的牌再次处理,然后让它重新洗牌并继续。这是我到目前为止所做的,它完成了我所说的,它随机播放然后打印出5张不同的卡片,但是如果我再次按下交易,我会得到相同的5张牌,除非我再次洗牌。

#include <iostream>
#include <vector>
#include <algorithm>
#include "stdio.h"
#include "stdlib.h"
#include "ctime"
#include "cstdlib"
using namespace std;

struct node
{
    int number;
    string suit;
    struct node *info;
};

vector<node> deckArray();
void displayDeal();
void Start();
vector<node> Deck(52);

int main()
{
    Start();
}

void Start()
{
    int choice;
    int choice2;

    deckArray();
    Deck.push_back(node());

    for (int s = 0; s < 2; ++s)
        Deck[0].number = s;
    while (1)
    {
        printf("1. Shuffle.\n");
        printf("2. Deal.\n");
        printf("2. Exit.");
        cin >> choice;

        switch (choice)
        {
            case 1:
                printf("\nAdding a hand!\n");
                deckArray();
                //Shuffle(Deck);
                break;
            case 2:
                for (int i = 0; i < 5; ++i)
                {
                    printf("\n%d", Deck[i].number);
                    printf("%s\n", Deck[i].suit.c_str());
                }
                cin.get();
                break;
            case 3:
                exit(1);
            default:
                printf("\n Invalid Choice. \n");
                break;
        }
    }
}

vector<node> deckArray()
{
    Deck[0].number = 2;
    Deck[0].suit = "S";
    Deck[1].number = 2;
    Deck[1].suit = "H";
    Deck[2].number = 2;
    Deck[2].suit = "D";
    Deck[3].number = 2;
    Deck[3].suit = "C";
    //2
    Deck[4].number = 3;
    Deck[4].suit = "S";
    Deck[5].number = 3;
    Deck[5].suit = "H";
    Deck[6].number = 3;
    Deck[6].suit = "D";
    Deck[7].number = 3;
    Deck[7].suit = "C";
    //3
    Deck[8].number = 4;
    Deck[8].suit = "S";
    Deck[9].number = 4;
    Deck[9].suit = "H";
    Deck[10].number = 4;
    Deck[10].suit = "D";
    Deck[11].number = 4;
    Deck[11].suit = "C";
    //4
    Deck[12].number = 5;
    Deck[12].suit = "D";
    Deck[13].number = 5;
    Deck[13].suit = "C";
    Deck[14].number = 5;
    Deck[14].suit = "D";
    Deck[15].number = 5;
    Deck[15].suit = "C";
    //5
    Deck[16].number = 6;
    Deck[16].suit = "D";
    Deck[17].number = 6;
    Deck[17].suit = "C";
    Deck[18].number = 6;
    Deck[18].suit = "D";
    Deck[19].number = 6;
    Deck[19].suit = "C";
    //6
    Deck[20].number = 7;
    Deck[20].suit = "D";
    Deck[21].number = 7;
    Deck[21].suit = "C";
    Deck[22].number = 7;
    Deck[22].suit = "D";
    Deck[23].number = 7;
    Deck[23].suit = "C";
    //7
    Deck[24].number = 8;
    Deck[24].suit = "D";
    Deck[25].number = 8;
    Deck[25].suit = "C";
    Deck[26].number = 8;
    Deck[26].suit = "D";
    Deck[27].number = 8;
    Deck[27].suit = "C";
    //8
    Deck[28].number = 9;
    Deck[28].suit = "D";
    Deck[29].number = 9;
    Deck[29].suit = "C";
    Deck[30].number = 9;
    Deck[30].suit = "D";
    Deck[31].number = 9;
    Deck[31].suit = "C";
    //9
    Deck[32].number = 10;
    Deck[32].suit = "D";
    Deck[33].number = 10;
    Deck[33].suit = "C";
    Deck[34].number = 10;
    Deck[34].suit = "D";
    Deck[35].number = 10;
    Deck[35].suit = "C";
    //10
    Deck[36].number = 11; //need to convert to string "J"
    Deck[36].suit = "D";
    Deck[37].number = 11;
    Deck[37].suit = "C";
    Deck[38].number = 11;
    Deck[38].suit = "D";
    Deck[39].number = 11;
    Deck[39].suit = "C";
    //11 (J)
    Deck[40].number = 12; //need to convert to string "Q"
    Deck[40].suit = "D";
    Deck[41].number = 12;
    Deck[41].suit = "C";
    Deck[42].number = 12;
    Deck[42].suit = "D";
    Deck[43].number = 12;
    Deck[43].suit = "C";
    //12 (Q)
    Deck[44].number = 13; //need to convert to string "K"
    Deck[44].suit = "D";
    Deck[45].number = 13;
    Deck[45].suit = "C";
    Deck[46].number = 13;
    Deck[46].suit = "D";
    Deck[47].number = 13;
    Deck[47].suit = "C";
    //13 (K)
    Deck[48].number = 14; //need to convert to string "A"
    Deck[48].suit = "D";
    Deck[49].number = 14;
    Deck[49].suit = "C";
    Deck[50].number = 14;
    Deck[50].suit = "D";
    Deck[51].number = 14;
    Deck[51].suit = "C";

    random_shuffle(Deck.begin(), Deck.end());
    return Deck;
}

解决我的问题的方法,但是不确定这是否是程序员禁止或不是

case 2:
    addToShuffle();
    break;

void addToShuffle()
{
for (int i = x; i < z; ++i)
{
    printf("\n%d", Deck[i].number);
    printf("%s\n", Deck[i].suit.c_str());
}
x += 5;
z += 5;
cin.get();
}

2 个答案:

答案 0 :(得分:1)

case 2只打印前5张牌,而不是从牌组中删除它们...... 所以处理一个新的时间(没有洗牌)仍然会返回相同的牌。

[OT] deckArray可以改写如下:

vector<node> deckArray()
{
    vector<node> cards(52);
    const char* colors[4] = {"S", "H", "D", "C"};

    int i = 0;
    for (int v = 2; v != 15; ++v) {
        for (int c = 0; c != 4; ++c, ++i) {
            cards[i].number = v;
            cards[i].suit = colors[c];
        }
    }
    random_shuffle(cards.begin(), cards.end());
    return cards;
}

答案 1 :(得分:0)

目前,你只能从矢量中处理0-4牌。如果您不更改交易之间的向量,那么这些元素将始终相同。有两种明显的方法可以解决这个问题。

可能最好的方法是跟踪你已经处理了多少张牌,并在下一笔交易中开始。例如,它第一次从0开始处理,并存储下一张可用卡位于索引5的某处。第二次,它将从5开始处理,依此类推。

另一种方法是在处理时从矢量中弹出每张卡片(并可选择将其添加到第二个“丢弃”矢量)。这样,每次交易时,套牌都会变小,直到没有卡片为止。这种方法在某些方面更容易,但这意味着每次洗牌时你都必须重新调整并重新填充套牌。