如何在同一时间弹出2堆栈

How to pop off 2 stacks at the same time

本文关键字:2堆栈 同一时间      更新时间:2023-10-16

我目前正在开发一个程序,该程序模拟具有2个团队的游戏,两个团队都包含许多玩家。每个团队被表示为一个堆栈(alienStack1和alienStack2);这两个堆栈各包含若干玩家。为了让2个玩家进行战斗(每个堆栈中有一个),我必须从堆栈中弹出相应的外星人以对抗另一个,但我不知道如何同时弹出两个堆栈。然后我们应该将弹出的项目发送到队列中,但在我尝试之前,我想先弄清楚这个。如果有人能帮我解决这个问题,我会很感激的。

下面是我到目前为止的代码:在我的battlefield()函数中,我遇到了2堆栈弹出的问题。
#include <iostream>
#include <string>
#include <stack>
using namespace std;
class Alien 
{
    public: 
        Alien();                    
        Alien(int h, int w, char g);  //set height to h,                                      
        void setHeight(int h);        //set height to h 
        void setWeight(int w);        //set weight to w 
        void setGender(char g);       //sets the gender to g 
        int getHeight();              //return the height
        int getWeight();              //return the weight 
        char getGender();             //return the gender 
        bool operator==(const Alien& alien) const; 
        bool operator!=(const Alien& alien) const; 
        bool operator<=(const Alien& alien) const; 
        bool operator<(const Alien& alien) const; 
        bool operator>=(const Alien& alien) const; 
        bool operator>(const Alien& alien) const;
        void putPlayersInStack(Alien alien, Alien alien2, Alien alien3, Alien alien4);
        void battlefield();
private: 
    int height;  //inches 
    int weight;  //pounds 
    char gender;  //the gender.  Either 'M' or 'F'
    stack <Alien> alienStack1;
    stack <Alien> alienStack2;
};
Alien::Alien()
{
    height = 60;
    weight = 100;
    gender = 'M';
    int statusPoints = 0;
}
Alien::Alien(int h, int w, char g)
{
    height = h;
    weight = w;
    gender = g;
    int statusPoints = 0;
}
void Alien::setHeight(int h)
{
    if (height > 0)
    {
        height = h;
    }
    else
    {
        cout << "Invalid height. Must be greater than zero. " << endl;
    }
}
void Alien::setWeight(int w)
{
    if (weight > 0)
    {
        weight = w;
    }
    else
    {
        cout << "Invalid weight. Must be greater than zero. " << endl;
    }
}
void Alien::setGender(char g)
{
    if (gender == 'M' && gender == 'F')
    {
        gender = g;
    }
    else
    {
        cout << "Invalid gender. Must be either M or F. " << endl;
    }
}
int Alien::getHeight()
{
    return height;
}
int Alien::getWeight()
{
    return weight;
}
char Alien::getGender()
{
    return gender;
}
static int getGenderValue(char g)
{
    int genderValue = 0;
    int statusPoints = 0;
    if (g == 'F')
    {
        genderValue = 3;
    }
    else
        genderValue = 2;
    return genderValue;
}
static int getStatusPoint(Alien alien)
{
    int genderValue = getGenderValue(alien.getGender());
    return (alien.getHeight() * alien.getWeight() * genderValue);
}
bool Alien::operator==(const Alien& alien) const
{
    return getStatusPoint(*this) == getStatusPoint(alien);
}
bool Alien::operator!=(const Alien& alien) const
{
    return getStatusPoint(*this) != getStatusPoint(alien);
}
bool Alien::operator<=(const Alien& alien) const
{
    return getStatusPoint(*this) <= getStatusPoint(alien);
}
bool Alien::operator>=(const Alien& alien) const
{
    return getStatusPoint(*this) >= getStatusPoint(alien);
}
bool Alien::operator<(const Alien& alien) const
{
    return getStatusPoint(*this) < getStatusPoint(alien);
}
bool Alien::operator>(const Alien& alien) const
{
    return getStatusPoint(*this) > getStatusPoint(alien);
}
void Alien::putPlayersInStack(Alien alien, Alien alien2, Alien alien3, Alien alien4)
{
    stack <Alien> alienStack1;
    stack <Alien> alienStack2;
    // Team 1
    alienStack1.push(alien);
    alienStack1.push(alien2);
    //Team 2
    alienStack2.push(alien3);
    alienStack2.push(alien4);
}
void Alien::battlefield()
{
    cout << "Prepare for battle " << endl;
    while (!alienStack1.empty())
    {
        alienStack1.top();
        alienStack1.pop();
    }
    while (!alienStack2.empty())
    {
        alienStack2.top();
        alienStack2.pop();
    }
}
int main()
{
    // Driver to test all 6 operators 
        Alien alien1(40, 120, 'M');
        Alien alien2(50, 130, 'F');
        Alien alien3(60, 140, 'M');
        Alien alien4(70, 150, 'F');
        Alien sendToStack;
        sendToStack.putPlayersInStack(alien1, alien2, alien3, alien4);

        /*if (player1 == player2)
        {
            cout << "Same score, it's a tie! " << endl;
            cout << endl;
        }
        if (player1 != player2)
        {
            cout << "players are NOT equal " << endl;
            cout << endl;
        }
        if (player1 <= player2)
        {
            cout << "It's a tie or the player 2 wins!" << endl;
            cout << endl;
        }
        if (player1 < player2)
        {
            cout << "Player 2 wins!" << endl;
            cout << endl;
        }
        if (player1 >= player2)
        {
            cout << "It's a tie or player 1 wins!" << endl;
            cout << endl;
        }
        if (player1 > player2)
        {
            cout << "Player 1 wins!" << endl;
            cout << endl;
        }*/
    system("pause");
    return 0;
}

除了"Cheers and hth"指出的语法错误…

(1)异形类有两个栈作为成员。我认为你需要将玩家插入到这两个堆栈中。相反,Alien::putPlayersInStack函数将玩家插入两个临时堆栈中,这些堆栈超出了作用域并在函数结束时被销毁。

(2)我不知道游戏规则是什么,但我猜你是想做这样的事情:

void Alien::battlefield()
{
    cout << "Prepare for battle " << endl;
    Alien p1,p2;        
    while (1)
    {
        if (alienStack1.empty()) { /*battle ended*/ return; }
        if (alienStack2.empty()) { /*battle ended*/ return; }
        p1=alienStack1.top(); alienStack1.pop();
        p2=alienStack1.top(); alienStack2.pop();      
        /* Now compare players p1, p2 */
    }    
}