Can anyone help me with this C++ program? Overview In this assignment you will s
ID: 3684336 • Letter: C
Question
Can anyone help me with this C++ program?
Overview
In this assignment you will simulate an online collectible card game where two players duel against each other to see who has the strongest cards. In this particular card game, cards have several attributes: a Name (this can be a word or a phrase that contains spaces), Power (a positive integer), and Health (a positive integer).
Sample input
Your program will read in data from 2 text files: 1 for each player. Each file contains information about that player and the cards they will be using for this duel. The user of the program will enter the filenames from a prompt from your program.
The first line of each file will contain the player’s name. The second line will contain an integer n which represents the number of cards that follow. Each subsequent line are the card attributes ordered in this format: Name, Power, and Health. It can be assumed that each player has the same number of cards in each file. Here is an example:
(file 1 contents) (file 2 contents)
The Innkeeper Malfurion
3 3
Tunnel Trogg Leper Gnome
1 2
3 1
Neptulon Aviana
7 5
7 5
Fire Elemental Cenarius
6 5
5 8
Player duels
After reading in both files of player information into 2 dynamic arrays of type Card, prompt the user if they would like the duel to begin.
Duels occur in the following format:
Duels happen between opposing cards in each player’s deck. If each player’s deck has 5 cards, there will be 5 duels.
Whichever player wins the most duels, wins the match. If both players win the same amount of duels, it is a tie.
To score a duel, the Power from player A’s card is subtracted from the Health of player B’s card and vice versa.
If a card in the duel is at or below 0 health, that card is defeated. If both cards are defeated, the duel is a draw.
For example, if it was Tunnel Trogg vs. Neptulon, Tunnel Trogg would end the duel with -4 Health, and Neptulon would end the duel with 6 health. In this duel, Neptulon wins.
Sample output
The first line of output will contain the concise results of the competition between players. The following lines will be the detailed output of each duel and which cards were involved.
The Innkeeper wins over Malfurion, 2 wins to 1 wins and no ties.
Here are the detailed results:
Tunnel Trogg (1, 3) vs. Leper Gnome (2, 1) – Tunnel Trogg wins for The Innkeeper
Neptulon (7, 7) vs. Aviana (5, 5) – Neptulon wins for The Innkeeper
Fire Elemental (6, 5) vs. Cenarius (5, 8) – Cenarius wins for Malfurion
Coding requirements and assumptions
Your program must read in 2 files—the names of each file will be determined by the user
Each file will contain the same number of cards for each player
The name of players and cards may contain spaces
The Power and Health of each card will always be positive integers
Explanation / Answer
Hi consider this example code for your reference,
#include <algorithm> // min, find
#include <string>
// board is incomplete, game is draw, p1 won, p2 won
enum class Status { incomplete, draw, p1, p2};
// types of cards
enum class Battle_Type { physical, magical, flexible, assault };
// type of block: free means you can put down a card there, a block is random, p1/p2 is ownership of cards.
enum class CType { free, block, p1, p2 };
class Values {
public:
/* E.g. 9P3F means
9 attack
P physical type
3 defense
F=15 magic defense*/
int atk, def, mdef;
Battle_Type type;
/* Note: Values constructor is not safe. Must check the string for proper format before using */
Values(std::string& s) {
static const char hex[] = "0123456789ABCDEF";
atk = std::find(hex, hex + sizeof hex, s[0]) - hex;
def = std::find(hex, hex + sizeof hex, s[2]) - hex;
mdef = std::find(hex, hex + sizeof hex, s[3]) - hex;
switch (s[1]) {
case 'P':
type = Battle_Type::physical;
break;
case 'M':
type = Battle_Type::magical;
break;
case 'X':
type = Battle_Type::flexible;
break;
case 'A':
type = Battle_Type::assault;
break;
}
};
};
class Card {
public:
Card(int id = 0) : id(id) { };
int id; // card id
bool arrows[3][3]; // card arrows for attack, what should [1][1] be?
Values values("0P00");
};
class Board {
public:
Board() {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
t[i][j] = CType::free;
// place random blocks here
};
~Board();
Status status() const;
Card m[4][4];
CType t[4][4];
};
// return 0 if card is placed, 1 otherwise
bool Place(Board& b, Card& c, int x, int y) {
if (b.m[x][y].id == 0) {
b.m[x][y] = c; // now check the arrows and fight
for (int i = -1; i < 2; i++)
for (int j = -1; j < 2; j++) {
if (x + i > 0 && y + j > 0) { // not out of bounds ; need to add more (currently for only one corner)
if (b.m[x][y].arrows[i + 1][j + 1]) {
if (b.m[x + i][y + j].arrows[1 - i][1 - j])
battle(b.m[x][y], b.m[x + i][y + j]);
else 0; // win the card by default
}
}
}
}
else return 1;
}
// return 0 if challenging card won, 1 if resting card won
bool battle(Card& chg , Card& rtg) {
int cHP[3], rHP[3];
HP(chg, cHP);
HP(rtg, rHP);
switch (chg.values.type) {
case Battle_Type::physical:
return cHP[0] > rHP[1];
break;
case Battle_Type::magical:
cHP[0] > rHP[2];
break;
case Battle_Type::flexible:
cHP[0] > std::min(rHP[1], rHP[2]);
break;
case Battle_Type::assault:
cHP[0] > std::min(rHP[0], rHP[1], rHP[2]);
break;
}
}
// return a random number between 0 and range-1
int random(int range) {
return 0; // for now
}
// roll random HP
void HP(Card& c, int *HP) {
// random(16) rolls between 0 and 15
HP[0] = c.values.atk * 16 + random(16);
HP[1] = c.values.def * 16 + random(16);
HP[2] = c.values.mdef * 16 + random(16);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.