Question: C++ Code Please Problem A: Getting rich (20 points) Modify the code in the following way: Add gold to the game and keep track of
C++ Code Please
Problem A: Getting rich (20 points) Modify the code in the following way: Add gold to the game and keep track of how much you have. Show your gold amount under your HP (see example). Start the game with 10 gold. Killing a rabbit should increase your gold by 1. When you goto town, add an option to buy a sword that costs 13 gold. You should only be able to buy this if you actually have 13 or more gold (no IOUs). After buying the sword, you should do 3 damage instead of 1.
Problem B: Getting some sleep (20 points)
When going to town, you have the option to sleep under the tree. If you sleep, your health should go back to its original maximum values (10 in the default code). Also when you sleep, all the rabbits should come back to life.
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
Get step-by-step solutions from verified subject matter experts
