在3-4次运行程序后出现std :: bad_alloc()错误,不知道为什么

时间:2016-07-21 23:03:32

标签: c++ bad-alloc

这是一张战争卡片游戏的代码。运行多次后(每次都不一样),它会抛出std::bad_alloc()错误,我不知道为什么。 Card.h和deck.h很好,但如果这段代码看起来不错,我会在需要时发布它们。

#ifndef WAR_H
#define WAR_H
/*
  This runWar function runs a game of war between 2 automated players
  and returns the number of rounds (both put a card down
  once per round) until someone won. An integer is given
  as an upper limit to how many rounds a game can go for.
*/

#include <iostream>
#include <stdlib.h>
#include <stdexcept>
#include <stack>
#include <queue>

#include "deck.h"

using namespace std;

int runWar(unsigned int limit)
{
    // fill me in!
    Card aCards[52];
    Deck d; // starting deck
    int roundcounter = 0;
    for (int i = 0; i < 1000; i ++)
    {
        cout << i << endl;
        int numCards = 0;
        // create cards
        for (int i = 1; i <= 4; i ++)
        {
            for (int j = 1; j <= 13; j++)
            {
                Card tC;
                tC.setValues(j,i);
                //d.push(*tC);
                // add to array for shuffling
                aCards[numCards] = tC;
                numCards = numCards + 1;
            }
        }
    }






    // shuffling

    for (int n = 0; n < 52; n++)
    {

        int index = rand() % 51;
        Card holder = aCards[index];
        aCards[index] = aCards[n];
        aCards[n] = holder;
    }

    for (int m = 0; m <= 51; m++)
    {
        d.addCard(aCards[m]);
    }

    // SUCCESSFULLY CREATES 52 CARD DECK

    // DECKS SUCCESSFULLY RANDOM


    // D - starting deck

    // deal out cards to each player

    queue<Card> p1; // player 1 deck
    queue<Card> p2; // player 2 deck

    while (d.size() != 0)
    {
        p1.push(d.getTopCard());
        if (d.size() != 0)
        {
            p2.push(d.getTopCard());
        }
    }

    // SUCCESSFULLY DEALS 26 cards to each player

    bool winner = false;

    // Play game
    while (winner == false)
    {
        Card one = p1.front();
        Card two = p2.front();

        if(!p1.empty())
        {
            p1.pop();
        }
        if(!p2.empty())
        {
            p2.pop();
        }

        if(one.getRank() > two.getRank())
        {
            // player 1 wins
            roundcounter++;
            p1.push(one);
            p1.push(two);
        }
        else if(one.getRank() < two.getRank())
        {
            // player 2 wins
            roundcounter++;
            p2.push(one);
            p2.push(two);
        }
        else
        {
            // WAR
            stack<Card> cardpile;

            bool war_donechecker = false;
            while(war_donechecker == false)
            {
                for( int w=0; w<3; w++)
                {
                    cardpile.push(p1.front());
                    cardpile.push(p2.front());
                    if(!p1.empty())
                    {
                        p1.pop();
                    }
                    if(!p2.empty())
                    {
                        p2.pop();
                    }
                }
                Card p11 = p1.front();
                Card p22 = p2.front();

                cardpile.push(p11);
                cardpile.push(p22);
                if(!p1.empty())
                {
                    p1.pop();
                }
                if(!p2.empty())
                {
                    p2.pop();
                }
                if(p11.getRank() > p22.getRank())
                {
                    roundcounter++;
                    while(!(cardpile.empty()))
                    {
                        p1.push(cardpile.top());
                        cardpile.pop();
                    }
                    p1.push(one);
                    p1.push(two);
                    war_donechecker = true;
                }
                else if(p11.getRank() > p22.getRank())
                {
                    roundcounter++;
                    while(!(cardpile.empty()))
                    {
                        p2.push(cardpile.top());
                        cardpile.pop();
                    }
                    p2.push(one);
                    p2.push(two);
                    war_donechecker = true;
                }
            }
            if (p1.empty() || p2.empty())
            {
                winner = true;
            }
        }
        cout << roundcounter << endl;
    }
    return roundcounter;
}

#endif

0 个答案:

没有答案