2016-08-17 59 views
-2

我目前正在开发一个程序,模拟一个包含两个团队的游戏,这两个团队都包含多个玩家。每个团队被表示为一个堆栈(alienStack1和alienStack2);这两个堆栈包含大量的玩家。为了让两名玩家对抗(每个玩家一个),我必须从堆叠中弹出相应的外星人才能相互对抗,但我不知道如何同时弹出两个堆叠。然后,我们应该将弹出的项目发送到队列中,但在尝试尝试之前,我想首先解决这个问题。如果有人能够帮助我解决这个问题,我将不胜感激。如何同时弹出两个堆栈

以下是我的代码到目前为止: 我在弹出2栈的问题在我的battlefield()函数中。

#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; 
} 
+0

注:在C++ 03你不能有一个标准的'X'集合直接作为一个'X'类数据成员(因为'X'在这一点上是不完整的)。我不确定,但我不认为在C++ 11中有所改变。另外它有一种设计气味,每个'Alien'都有两个'Alien'。 –

+1

请将您的代码降低到最低限度,可验证和完整示例:http://stackoverflow.com/help/mcve –

+0

您现在的做法有什么问题,即一个接一个地弹出两个堆栈? – Praveen

回答

0

除了语法错误所指出的“欢呼和心连心” ......

(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 */ 
    }  
}