ARRAYCOL is not needed because you aren't using a 2D array, just a 1D array of structs with 4 entries. Also, ARRAYROW seems to have the same meaning as SIZE. I would just use SIZE.
Also, the structs are essentially identical, so you may as well just have one called Card. And I guess you don't need the type if you are only reading in type 1 cards.
We would normally use a vector instead of an array, which would mean that you don't need SIZE at all. And the struct could benefit from a constructor.
If the files actually have a header as you showed it then you need to skip that. You show a header that says "type name attack defense", but maybe that's not part of the files.
The following compiles but I didn't actually run it.
It assumes you want to go through each deck and compare card-for-card (as opposed to the double loop in your original code where you compare each card in one deck to every card in the other).
If you would like to shuffle the deck I can show you how to do that, but this code doesn't.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Card {
string name;
int attk;
int def;
Card(string n, int a, int d)
: name(n), attk(a), def(d) {}
};
int main() {
ifstream fin("sorceress.txt");
if (!fin) {
cerr << "Unable to open sorceress.txt\n";
return 0;
}
int typ, att, def;
string nam;
vector<Card> sorDeck;
// getline(fin, nam); // skip header (?)
while (fin >> typ >> nam >> att >> def)
if (typ == 1)
sorDeck.push_back(Card(nam, att, def));
fin.close();
fin.open("wizard.txt");
if (!fin) {
cerr << "Unable to open wizard.txt\n";
return 0;
}
vector<Card> wizDeck;
// getline(fin, nam); // skip header (?)
while (fin >> typ >> nam >> att >> def)
if (typ == 1)
wizDeck.push_back(Card(nam, att, def));
fin.close();
// See how many cards you read in.
cout << "sorDeck: " << sorDeck.size() << "\n";
cout << "wizDeck: " << wizDeck.size() << "\n\n";
cout << "Welcome To MAGIMANIA!\n\nCreated by M Gibson-Storey\n\n";
int sorceressHealth = 30, wizardHealth = 30;
size_t i = 0;
while (i < 30 && i < sorDeck.size() && i < wizDeck.size()
&& sorceressHealth > 0 && wizardHealth > 0) {
cout << "\nRound " << i + 1 << " fight!\n\n";
system("pause");
cout << "Sorceress draws " << sorDeck[i].name << "\n\n";
cout << sorDeck[i].name << " deals "
<< sorDeck[i].attk << " damage to Wizard!\n\n";
cout << "Wizard draws " << wizDeck[i].name << "\n\n";
cout << wizDeck[i].name << " deals "
<< wizDeck[i].attk << " damage to Sorceress!\n\n";
wizardHealth -= sorDeck[i].attk;
sorceressHealth -= wizDeck[i].attk;
cout << "\nSorceress has:" << sorceressHealth << " Health\n\n";
cout << "Wizard has:" << wizardHealth << " Health\n";
system("pause");
}
if (wizardHealth <= 0)
cout << "Sorceress Wins!\n";
else if (sorceressHealth <= 0)
cout << "Wizard Wins!\n";
else
cout << "\nIts a tie!\n";
system("pause");
}
| |