2016-04-07 56 views
0

首先,请原谅我的丑陋代码。尽管所有可能的解决方案都没有奏效,但我已经尝试解决这些代码的大量想法已经混乱起来。基本上,我正在编写一个Hearthstone rip-off,用卡信息读取两个.txt文件,并与他们对战以查看哪个玩家获胜。问题是,当我试图保存播放器的名称(文件中的第一行)时,它会保存整个文件而不是第一行。当我设法解决这个问题时,出于某种原因,用于保存卡对象信息的for循环(格式:卡名称,卡电源,卡健康状况)无法正确保存。任何帮助将不胜感激,我一直在试图解决这个问题两天,没有任何东西完全解决了这个问题。我将在代码之前先将附加的文件附加到文件中。C++ .txt读入问题。 getline阅读完整文件

声明:这是很多线条,我很抱歉。此外,我认为问题可能是我的Mac没有以具有正确行结尾的格式保存.txt。我使用XCode作为我的IDE。非常感谢你愿意帮助的人!

文件1:

The Innkeeper 
3 
Tunnel Trogg 
1 
3 
Neptulon 
7 
7 
Fire Elemental 
6 
5 

文件2:

Malfurion 
3 
Leper Gnome 
2 
1 
Aviana 
5 
5 
Cenarius 
5 
8 

主营:

#include "Player.h" 

using namespace std; 

int main() 
{ 
cout << "Please enter file name of the first player: " << endl; 
string inFile = ""; 
getline(cin, inFile); 

Player* p1 = new Player(inFile); 

cout << "Now enter the file name of the second player: " << endl; 
getline(cin, inFile); 
Player* p2 = new Player(inFile); 

p1->battle(*p2); 

delete p1; 
delete p2; 

return 0; 
} 

播放器标题:

#include "Card.h" 
#include <fstream> 

#ifndef Player_h 
#define Player_h 
using namespace std; 
class Player 
{ 
private: 
    string playerName; 
    int numCards; 
    Card ** cards; 
    int wins = 0; 
public: 
    Player(std::string inFile); 
    void battle(Player p2); 
    Card* getCard(int counter); 
    ~Player(); 
}; 
#endif /* Player_h */ 

卡头:

#include <string> 
#include <iostream> 

#ifndef Card_h 
#define Card_h 

using namespace std; 

class Card 
{ 
public: 
    Card(); 
    string getName(); 
    int getPower(); 
    int getHealth(); 
    void setName(string newName); 
    void setPower(int newPower); 
    void setHealth(int newHealth); 

    Card* duel(Card&); 

    friend ostream& operator<<(ostream& o, Card& c); 
    friend bool operator==(Card& p1Card, Card& p2Card); 
private: 
    string name; 
    int power; 
    int health; 
}; 
#endif /* Card_h */ 

球员来源:

#include "Player.h" 

using namespace std; 

Player::Player(string inFile) 
{ 
    ifstream in(inFile, ios::in);\ 
    if (!in) 
    { 
     cerr << "There was a problem opening the file. Sorry, try again!" << endl; 
     return; 
    } 

    getline(in, playerName); 
    cout << playerName << endl; 
    in>>numCards; 

    playerName = ""; 
    numCards = 0; 
    cards = new Card* [numCards]; 

    string tempName = ""; 
    int tempPower = 0; 
    int tempHealth = 0; 

    for (int i = 0; i<numCards; i++) 
    { 
     in.ignore(); 
     cards[i] = new Card(); 
     getline(in, tempName); 
     cout << "in for loop: " << endl; 
     cout << tempName << ","; 
     cards[i]->setName(tempName); 
     in >> tempPower; 
     in.ignore(); 
     cout << tempPower << ","; 
     cards[i]->setPower(tempPower); 
     in >> tempHealth; 
     cout << tempHealth << "    done"<< endl; 
     cards[i]->setHealth(tempHealth); 
    } 
} 

void Player::battle(Player p2) 
{ 
    int draws = 0; 

    cout << "Let the battle begin!" << endl; 
    cout << numCards << endl; 

    if (wins > p2.wins) 
    { 
     cout << playerName << " wins over " << p2.playerName << ", " << wins << " to " << p2.wins; 

     if (draws == 0) 
     { 
      cout << " and no ties." << endl; 
     } 

     else 
     { 
      cout << " and " << draws << " ties." << endl; 
     } 
    } 
    else if (p2.wins > wins) 
    { 
     cout << p2.playerName << " wins over " << playerName << ", " << p2.wins << " to " << wins; 

     if (draws == 0) 
     { 
      cout << " and no ties." << endl; 
     } 

     else 
     { 
      cout << " and " << draws << " ties." << endl; 
     } 
    } 
    else if (p2.wins == wins) 
    { 
     cout << "It is a draw between " << playerName << " and " << p2.playerName << ", with " << wins << " for each and "; 
     if (draws == 0) 
     { 
      cout << "no ties." << endl; 
     } 

     else 
     { 
      cout << draws << " ties." << endl; 
     } 
    } 

    cout << "Here are the detailed results:" << endl; 
    for (int i = 0; i < numCards; i++) 
    { 
     cout << *cards[i] << " vs. " << *p2.cards[i] << " - "; 

     if (*cards[i] == *p2.cards[i]) 
     { 
      cout << "It is a draw." << endl; 
     } 

     else if (cards[i]->duel(*p2.cards[i]) == NULL) 
     { 
      cout << "It is a draw." << endl; 
     } 

     else if (*cards[i]->duel(*p2.cards[i]) == *p2.cards[i]) 
     { 
      cout << p2.cards[i]->getName() << "wins for " << p2.playerName << "." << endl; 
     } 

     else if (*cards[i]->duel(*p2.cards[i]) == *cards[i]) 
     { 
      cout << cards[i]->getName() << "wins for " << playerName << "." << endl; 
     } 
    } 
} 


Player::~Player() 
{ 
    if (cards != NULL) 
    { 
     for (int i = 0; i < numCards; i++) 
     { 
      if (cards[i] != nullptr) 
      { 
       delete cards[i]; 
       cards[i] = NULL; 
      } 
     }; 
    } 
} 

卡来源:

#include "Card.h" 
using namespace std; 

Card::Card() 
{ 
    name = ""; 
    power = 0; 
    health = 0; 
} 

string Card::getName() 
{ 
    return name; 
} 

int Card::getPower() 
{ 
    return power; 
} 

int Card::getHealth() 
{ 
    return health; 
} 

void Card::setName(string newName) 
{ 
    name = newName; 
} 

void Card::setPower(int newPower) 
{ 
    power = newPower; 
} 

void Card::setHealth(int newHealth) 
{ 
    health = newHealth; 
} 

Card* Card::duel(Card& otherCard) 
{ 
    if ((otherCard.getHealth() - this->getPower() <=0) && (getHealth() - otherCard.getPower() <= 0)) 
    { 
     return NULL; 
    } 

    else if ((otherCard.getHealth() - this->getPower() >0) && (getHealth() - otherCard.getPower() >0)) 
    { 
     return NULL; 
    } 

    else if (otherCard.getHealth() - this->getPower() <=0) 
    { 
     return this; 
    } 

    else if (this->getHealth() - otherCard.getPower() <=0) 
    { 
     return &otherCard; 
    } 

    return NULL; 
} 


ostream& operator<<(ostream& o, Card& c) 
{ 
    o << c.getName() << " (" << c.power << ", " << c.health << ") " << endl; 

    return o; 
} 

bool operator==(Card& p1Card, Card& p2Card) 
{ 
    if (p1Card.health == p2Card.health && 
     p1Card.power == p2Card.power && 
     p1Card.name == p2Card.name) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

回答

0

你的代码是差不多吧。它可以读取玩家的姓名和身份证号码,但你的代码,显示如下:

in>>numCards; 
playerName = ""; 
numCards = 0; 
cards = new Card* [numCards]; 

首先,它读取卡的NUM并将其存储到numCards,这是正确的。 接下来,您清除了numCards的值,然后,您丢失了该卡的编号,因此所执行的代码将以numCards == 0 执行。您可以对该行进行注释numCards = 0,并且您的代码正确执行。

+0

谢谢!愚蠢的错误哈哈 –