2016-04-07 16 views
0

her şeyden önce, çirkin olmak için kodumu bağışla. Bu kodu düzeltmeye çalışmak için verdiğim fikirlerin tonu, işe yaramayan tüm potansiyel çözümlerin peşinden koştu. Temel olarak, kart bilgisiyle iki .txt dosyasında okuyan ve onları hangi oyuncuların kazandığını görmek için savaşan bir Hearthstone rip-off yazdım. Sorun şu ki, oyuncunun adını (dosyalardaki ilk satır) kaydetmeye çalıştığımda, tüm dosyayı yalnızca ilk satırın yerine kaydeder. Bunu düzeltmeyi başardığımda, kart nesnesi için bilgileri kaydetmek için kullanılan for döngüsü (format: kart adı, kart gücü, kart sağlığı) bir sebepten dolayı doğru şekilde kaydedilmez. Herhangi bir yardım takdir edilecektir, bunu iki gün boyunca düzeltmeye çalışıyorum ve hiçbir şey problemi çözmedi. Önce okunan dosyaları koddan önce ekleyeceğim.C++ .txt sorunlarda okuyabilirsiniz. getline readline

Yasal Uyarı: Çok fazla satır var ve bunun için üzgünüm. Ayrıca sanırım sorun Mac'imin .txt'i doğru satır sonlarına sahip bir biçimde kaydetmemesi olabilir. XCode'u IDE'im olarak kullanıyorum. Yardım etmek isteyen herkese çok teşekkür ederim!

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 

Ana:

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

Oyuncu Başlık:

#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 */ 

Kart Başlık:

#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 */ 

Oyuncu Kaynak:

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

Kart Kaynak:

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

cevap

0

Kodunuz neredeyse haklı. Bu Oyuncunun adı ve kart numaralarını okuyabilir, ancak kodları aşağıda gösterdi:

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

ilk başta, bu kartın num okuyup numCards alanına kaydetmek, doğrudur. sonra, numCard'ların değerini temizlersiniz, daha sonra Kartın numerini kaybettiniz, bu yüzden kodlar takip edildiğinde numCards == 0 ile çalıştırılabilir. numCards = 0 hattına yorum yapabilirsiniz ve kodunuz doğru olarak çalıştırılabilir.

+0

Teşekkürler! aptal hata –

İlgili konular