Question
(C++) Write a program with only C++ please. All of these problems relate to HeroineQuestV2.cpp. This is a rather large piece of code, so you
(C++) Write a program with only C++ please.
All of these problems relate to HeroineQuestV2.cpp. This is a rather large piece of code, so you should try to familiarize yourself with it before attempting to solve the problems. You do not need to understand every part of the code to solve the problems, just the general structure of the code. (Note: the left-hand side of geany (under Symbols) lists the functions (and variables), which can allow you to jump around to them in the code quickly.) When running the program, first you enter a name for the heroine: Then you are shown to a map. You move using wasd (type one then press enter).
Problem A: Randomization (20 points) Currently both the Heroine and monsters do fixed damage during battle. Modify the code so that a random number is generated between the current damage to twice the current damage (inclusive). For example, the Heroine currently does 1 damage. After this modification, the heroine should do randomly between 1 to 2 damage.
(Important)********Every integer should be possible and each value should have equal probability (i.e. a uniform distribution). So if the Heroines damage was originally 2, you should under this modification be able to do 2, 3 or 4 points of damage, each with probability 1/3.
code
========================================================
#includeusing namespace std; const int SIZE = 10; const string NOT_VALID = "INVALID"; class creature{ // things! public: creature(); creature(string n, int h, int d); string getName(); void attack(creature& other); // for hurting other creatures bool isDead(); // checks to see if you have enoug hp int getHP(); int getDMG(); void setLocation(int r, int c); // sets your location in the world bool move(char dir); // changes location based on "wasd" int getRowLocation(); int getColLocation(); void kill(); // no more creature... private: string name; int hp; int dmg; int worldRow; int worldCol; }; class world{ public: world(); void show(); // displaying the world (and everything on it) char getTile(int r, int c); //shows what is displayed in the world at a row/col creature& getCreature(int r, int c); // returning creature& as I don't want multiple copies of this creature creature& getCreature(string name); // ^^ (probably better to use pointers, but this homework isn't about them) void addCreature(creature &c);// keep track of another creature void processTile(); // figuring out the hero should do at a tile of the map private: void setRow(int r, string s); // used to help initialize map creature list[SIZE*SIZE+1]; // last creature is invalid int creatureCount; // how many creatures are in the list char map[SIZE][SIZE]; }; void clearScreen(); void processTile(world& w); void gotoTown(creature& you); void round(creature& attacker, creature& getHit); bool isFirstLetter(string s, string tests); int main() { string hname; // your name! (or something cool) do { cout << "Who art thou? (Don't start with 'R' or '~' or '.') "; getline(cin,hname); }while(hname.length() == 0 || isFirstLetter(hname, "r~.")); creature heroine = creature(hname, 10, 1); heroine.setLocation(6,5); world island; island.addCreature(heroine); // heroine is always first creature // make a rabbit creature rabby = creature("Rabbit", 2, 1); rabby.setLocation(8, 3); island.addCreature(rabby); // save rabbit in the world // ... and some more rabbits creature rabby2 = creature("Rabbit", 2, 1); rabby2.setLocation(4, 8); island.addCreature(rabby2); // save rabbit in the world creature rabby3 = creature("Rabbit", 2, 1); rabby3.setLocation(5, 8); island.addCreature(rabby3); // save rabbit in the world creature rabby4 = creature("Rabbit's Revenge", 2, 9); rabby4.setLocation(4, 8); island.addCreature(rabby4); // save rabbit in the world while(!island.getCreature(hname).isDead()) // keep going until you drop { // show where you are: clearScreen(); island.show(); // show how healthy you are: cout << "HP: " << island.getCreature(hname).getHP() << endl; // ask what you want to do: cout << "Which direction do you want to move ('w'=up, 'a'=left, 's'=down, 'd'=right)? "; string dir; getline(cin, dir); if(dir.length()>0) // if they didn't just hit enter { // process your movement, then interact with that tile island.getCreature(hname).move(dir[0]); island.processTile(); } } cout << "Game over... "; } // hack solution void clearScreen() { cout << " "; } // interacting with town void gotoTown(creature& you) { clearScreen(); cout << " +" << endl; cout << " / \\" << endl; cout << " ______ ___________/ o \\" << endl; cout << "________ | | | | /^\\" << endl; cout << "| * * *| |: ::| | | //^\\\\" << endl; cout << "| * * | |:: | | [] [] [] []| ((|))" << endl; cout << "|* ** | |: :| | [] [] [] | ((|))" << endl; cout << "|_[]___| |_||_| |____________;;_| | " << endl; // not a whole lot to do here... cout << you.getName() << " walks into town, but there is nothing to do here and leaves. "; cout << "(press enter to continue...)"; string x; getline(cin, x); } // creature "a" and "b" fighting ("a" is the heroine) void battle(creature& a, creature& b) { clearScreen(); cout << " ___/________" < = 0; i--) // going backwards so hero is on top of other creatures { if(list[i].getRowLocation() >= 0 && list[i].getRowLocation() < SIZE && list[i].getColLocation() >= 0 && list[i].getColLocation() < SIZE && !list[i].isDead()) { currentWorld[list[i].getRowLocation()][list[i].getColLocation()] = tolower(list[i].getName().at(0)); } } // draw map (with creatures) for(int i=0; i < SIZE; i++) { for(int j=0; j < SIZE; j++) { cout << currentWorld[i][j]; } cout << endl; } } creature& world::getCreature(int r, int c) { for(int i=creatureCount-1; i >= 0; i--) // since heroine is always list[0], we want to check this last so we can tell if heroine is ontop of something else { if(list[i].getRowLocation() == r && list[i].getColLocation() == c && !list[i].isDead()) // only return alive creatures { return list[i]; } } return list[SIZE*SIZE]; // if we don't find anything, return a default creature } creature& world::getCreature(string name) { for(int i=creatureCount-1; i >= 0; i--) // since heroine is always list[0], we want to check this last so we can tell if heroine is ontop of something else { if(list[i].getName() == name && !list[i].isDead()) { return list[i]; } } return list[SIZE*SIZE]; // if we don't find anything, return a default creature } void world::addCreature(creature &c) { list[creatureCount] = c; // list is partiallyed filled array creatureCount++; } void world::processTile() { // find which tile the heroine is on int pRow = list[0].getRowLocation(); int pCol = list[0].getColLocation(); if(map[pRow][pCol] == '~') // if they on the ocean { cout << "The heroic " << list[0].getName() << " walks in to the ocean wearing heavy armor and joins the fishes... "; list[0].kill(); } else if(map[pRow][pCol] == 'T') // if heroine in town { gotoTown(list[0]); } else // see if the heroine is on top of a monster (to fight) { for(int i=1; i < creatureCount; i++) { if(list[0].getRowLocation() == list[i].getRowLocation() && list[0].getColLocation() == list[i].getColLocation() && !list[i].isDead()) // fight if they on same row/col and monster alive { battle(list[0], list[i]); break; // only fight a monster once per tile } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started