2016-04-07 10 views
0

まず、醜いの私のコードを許してください。私がこのコードを修正しようとしてきたたくさんのアイデアは、うまくいかない潜在的な解決策の後にそれを混乱させました。基本的には、カード情報を持つ2つの.txtファイルを読み込み、どのプレイヤーが勝つかを見るために戦うHearthstone rip-offをコーディングしています。問題は、プレイヤーの名前(ファイルの最初の行)を保存しようとすると、最初の行の代わりにファイル全体が保存されるということです。私がそれを修正することができたとき、何らかの理由でカードオブジェクト(フォーマット:カード名、カードパワー、カードヘルス)の情報を保存するために使用されるforループが正しく保存されません。私は2日間これを修正しようとしていますが、何も問題を完全に解決していません。私はコードの前に最初にファイルに読み込みを添付します。C++ .txtの問題があります。 getlineのフルファイルを読む

免責事項:それはたくさんの行であり、私はそれについて残念です。また、問題は、私のMacが正しい行末を持つ形式で.txtを保存していない可能性があると思います。私は自分のIDEとしてXCodeを使用しています。誰でも手伝ってくれてありがとう!

はFile1:

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

File2の:

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

ありがとうございます!ダムミスハハ –

関連する問題