2016-08-17 62 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 */ 
    }  
}