每次到达结束时重新调整阵列

时间:2017-09-28 05:31:57

标签: c++ arrays infinite-loop shuffle playing-cards

所以基本上我有一张10张牌,我在游戏开始时随机播放,每转一圈我都会选择一张卡片,没有重复,直到我出局。那时我会重新洗牌并重复这个过程,直到比赛结束。

void play(int &size, int &player1, int &player2, int cardPile[], int board[]);
    void displayRules();
    int takeTurn(int &size, int &player, int cardPile[], int board[], int &opposingPlayer);
    int shuffleDeck(int &size, int cardPile[]);
    int drawCard(int &size, int cardPile[]);



    int main()
    {
        int size;
        int board[] = {0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0};
        int cardPile[10] = {1, 1, 2, 2, 3, 3, 4, 4, 0, 5};
        int player1 = 0;
        int player2 = 0;

        play(size, player1, player2, cardPile, board);
        return 0;
    }

    //This is the function that plays the entire game
    void play(int &size, int &player1, int &player2, int cardPile[], int board[]){
        displayRules();
        shuffleDeck(size, cardPile);
            while(player1 < 25 && player2 < 25){
                cout << "\nPlayer 1's turn!" << endl;
                takeTurn(size, player1, cardPile, board, player2);
                    drawCard(size, cardPile);

                size++;
                showState(player1, player2);
                    if(player1 >= 25)
                        break;
                    else
                        cout << "\nPlayer 2's turn!" << endl,
                        takeTurn(size, player2, cardPile, board, player1),
                            drawCard(size, cardPile),

                        size++,
                        showState(player1, player2);
        }
        youWin(player1, player2);

    }

    //This function does a single turn for each player
    int takeTurn(int &size, int &player, int cardPile[], int board[],int &opposingPlayer){
        if(cardPile[size] == 0)
            cout << "You drew a Lose a turn card! You lose a turn!" << endl;

        else if(cardPile[size] == 5)
            cout << "You drew a Switch Places card! You must switch places with the other player!" << endl,
            switchPlaces(player, opposingPlayer);

        else
        cout << "You drew a " << cardPile[size] << "!";
            switch(cardPile[size]){
            case 1:
                cout << " Move forward " << cardPile[size] << " space on the board!" << endl;
                player += cardPile[size];
                obstacles(player, board);
                break;
            case 2:
                cout << " Move forward " << cardPile[size] << " spaces on the board!" << endl;
                player += cardPile[size];
                obstacles(player, board);
                break;
            case 3:
                cout << " Move forward " << cardPile[size] << " spaces on the board!" << endl;
                player += cardPile[size];
                obstacles(player, board);
                break;
            case 4:
                cout << " Move forward " << cardPile[size] << " spaces on the board!" << endl;
                player += cardPile[size];
                obstacles(player, board);
                break;

            }
    }

    //This function shuffles the deck of cards
    int shuffleDeck(int &size, int cardPile[]){

        srand(time(0));
        for(int i = 0; i < 10; i++){
                int size = rand() % 10;
                int temp = cardPile[i];
                cardPile[i] = cardPile[size];
                cardPile[size] = temp;
        }

    }



    int drawCard(int &size, int cardPile[]){
        size++;

        if(size == 10)
            shuffleDeck(size, cardPile);
        else
            ;

    }

我有一个名为size的变量,我在main和shuffleDeck函数中声明了在deck被洗牌之后我尝试将该元素的数量存储在size变量中并从那里将它放入takeTurn函数中以便每次都有一个玩家他们会从卡片中获取一张随机数。然后drawCard函数将size变量增加1,如果它达到第10个元素,那么它将调用shuffleDeck函数来重新组合套牌。

现在我试图在另一个程序中对此进行测试,并设法成功重组数组,最终成为一个无限循环。

#include <iostream>

using namespace std;

int shuffle(int &n, int deck[]);
void display(int &n, int deck[]);


int main()
{

    int deck[10] = {2, 4, 6, 8, 12, 14, 16, 18, 20, 22};
    int n;

    shuffle(n, deck);
    display(n, deck);

    return 0;
}

int shuffle(int &n, int deck[]){

    srand(time(0));
    for(int i = 0; i < 10; i++){
        int n = rand() % 10;
        int temp = deck[i];
        deck[i] = deck[n];
        deck[n] = temp;

    }
}

void display(int &n, int deck[]){
    while(n < 10){
        cout << n << " " << deck[n] << endl;
        n++;
    if(n == 10)
        shuffle(n, deck),
        n = 0;
    else
        ;
    }
}

当我编译并运行我的原始程序时,玩家会抽取成千上万的卡片,最终会以无限循环结束。

1 个答案:

答案 0 :(得分:0)

您可以创建一个类来处理套牌,例如:

template <typename T, typename Rnd>
class Deck
{
public:
    Deck(const std::vector<T>& cards, Rnd& rnd) : cards(cards), rnd(rnd)
    {
        shuffle();
    }

    T draw() {
        ++index;
        if (index == cards.size())
        {
            shuffle();
            index = 0;
        }
        return cards[index];
    }
private:
    void shuffle()
    {
        std::cout << "shuffle\n";
        std::shuffle(this->cards.begin(), this->cards.end(), rnd);
    }

private:
    std::vector<T> cards;
    std::size_t index = -1;
    Rnd& rnd;
};

用法:

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 rnd(seed);
Deck<int, std::mt19937> deck({1, 1, 2, 2, 3, 3, 4, 4, 0, 5}, rnd);

Demo