What's the best way to approach this function?

Started by
3 comments, last by Zahlman 15 years, 4 months ago
Need a little help with this assignment.. more over just not sure how to go about doing it.. have completed the rest by now am suppose to find the value of all the treasures and rank them by value then have the monster choose the treasure that is one better than their current so long as he does not already posses one that is in the top half of available treasures... here's my source not to long at all

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

class Treasure {
private:              // Private variables.. can only be used internally in the class
string t_name;
int value;        
        
public:
Treasure()  {};      // empty constructor
Treasure(string tempname, int tempval) {       // Treasure function that sets values
t_name = tempname;
value = tempval;
}


string getName() {return t_name;}
int appraise() {return value;}

};

class Monster {
private:
string name, treasure, TempT;
int health, strength, defense;

public:
Monster() {};

Monster(string nm, int hlth, int str, int def) {
name = nm;
health = hlth;
strength = str;
defense = def;
}

int status() {return health;}

int attack() {return strength;}

string getName() {return name;}

void defend(Monster enemy) {
int attack = enemy.attack() - defense;
if (attack < 0) attack = 0;
health -= attack;
if (health < 0) health = 0;

cout << enemy.getName();
if (!health) cout << " decisively";
cout << " hits " << name << " for " << attack << " points." << endl;
if (!health)
cout << name << " enters a state of suspended animation." << endl;
}

string getTreasure() {return treasure;}     // Return the monsters treasure

string horde (string newTreasure) {
TempT = treasure;
treasure = newTreasure;
return TempT;
}

string covet () {         // Get tres aval.. rank.. make change

}

};

int main() {
srand(0);

Monster ranch[] = {
Monster("Gorgolla", 100, 5, 5), // name, health (or status), attack, defense
Monster("Taboo", 100, 6, 4),
Monster("Dragoom", 100, 6, 0),
Monster("Gargantus", 100, 8, 1),
Monster("Mechano", 100, 10, 3),
Monster("Zzutak", 100, 7, 3),
Monster("Orrgo", 100, 4, 2),
Monster("Monsteroso", 100, 6, 6),
Monster("Krogarr", 100, 6, 4)
};

Treasure treasures[] = {
Treasure("Acorn of Wo Mai", 745669),
Treasure("Bracers of Invulnerability", 1693179),
Treasure("Cup of Al-Akbar", 3228374),
Treasure("Ankh of Life", 3293752),
Treasure("Golden Circlet", 4803458),
Treasure("Hammer of Kharas", 5848660),
Treasure("Stone Scepter of Shih", 6505675),
Treasure("Fist of Delzoun", 6949098),
Treasure("Hand of Vecna", 8375126),
};

//Shuffle the treasures.
for (int i = 0; i < 50; i++) {
int a = rand() % 9;
int b = rand() % 9;
Treasure tmp = treasures[a];
treasures[a] = treasures;
treasures = tmp;
}
//Dole them out.
for (int i = 0; i < 9; i++) {
ranch.horde(treasures);
}

//The market opens. The closing bell will toll for all but one.
int menStanding = 9;
while(menStanding > 1) {
//a random active monster will get to act this round
int chosen = rand() % 9;
while(!ranch[chosen].status()) chosen = rand() % 9;

//which treasures are still available?
int treasuresInPlay = 0;
for (int i = 0; i < 9; i++) {
if (ranch.status())
treasures[treasuresInPlay++] = ranch.getTreasure();
}

//does the monster want a treasure? If so, let him attempt to steal it
string coveted = ranch[chosen].covet(treasures, treasuresInPlay);
if (coveted != ranch[chosen].getTreasure().getName()) {
int target = 0;
for (int i = 0; i < 9; i++)
if (coveted == ranch.getTreasure().getName())
target = i;
ranch[chosen].defend(ranch[target]);
if (ranch[chosen].status()) {
ranch[target].horde(ranch[chosen].horde(ranch[target].getTreasure()));
cout << ranch[chosen].getName() << " steals the " << ranch
[chosen].getTreasure().getName() << "." << endl;
}
}

//how many monsters are left?
menStanding = 0;
for (int i = 0; i < 9; i++)
if (ranch.status()) menStanding++;
}

cout << endl << "Fight results:" << endl;
int order[]= {7,0,8,6,3,5,4,2,1}; //win (pc)
//int order[]= {7,4,0,2,3,6,1,5,8}; //mac
for (int i = 0; i < 9; i++) {
cout << "\t" << i+1 << ": ";
cout << ranch[order].getName();
if (ranch[order].status()) cout << "*";
cout << " has the " << ranch[order].getTreasure().getName();
cout << " (" << ranch[order].getTreasure().appraise() << ")" << endl;
}

return 0;
}




[Edited by - JMOmandown on December 3, 2008 5:14:27 PM]
Advertisement
We do not do homework here. If you want answers to specific questions, you can ask, but you need to explain what you've tried, why that didn't work, and what you expect. Posting your code and your assignment is not sufficient and will simply lead to your thread being closed.
I'm not asking you to do it... infact... this is a small piece of a large assignment... I'm just asking how you would approach doing it... specifically getting and ranking the current treasures.. not asking for the code.. asking what the best way to go about doing it is
I would sort them (after all, you already have a structure representing a "treasure" and an array of them), using one of many common sort algorithms which you can find around Google or (more likely) have been introduced to in your class. I would specifically sort them by their value property.

We cannot provide you an answer more specific than that until you ask a question more specific than that, one that demonstrates the steps you've already taken to solve the problem yourself.
Quote:Original post by JMOmandown
I'm not asking you to do it... infact... this is a small piece of a large assignment... I'm just asking how you would approach doing it... specifically getting and ranking the current treasures.. not asking for the code.. asking what the best way to go about doing it is


The current treasures are represented by the 'treasures[]' array and 'treasuresInPlay' count. Their values are given by the .appraise() member function. "ranking" things is equivalent, more or less, to sorting them. C++ provides a built-in sorting function in the standard library, called std::sort; to get it, #include <algorithm>. You will need to specify how to "compare" two treasures for the purpose of the sort (in your case, by comparing the results of .appraise()). It looks like

#include <algorithm>// ...bool compareTreasure(const Treasure& a, const Treasure& b) {  return a.appraise() < b.appraise();}// This will sort the treasures from least valuable to most valuable.sort(treasures, treasures + treasuresInPlay, compareTreasure);


It looks like you have a lot of other bugs to worry about, though. In particular, you are trying to .horde() Treasures, but .horde() is declared to accept a string, and the Monster's "treasure" is also in fact just a string. You should think carefully about what you want to actually happen. You should also consider that C++ has value semantics by default: i.e. if you say something like

class Treasure {};class Monster {  Treasure t;  void setTreasure(const Treasure& t) { this->t = t; }};Monster m;Treasure jewel;m.setTreasure(jewel);


then the Monster's Treasure is equal to the 'jewel', but it is not identical - it is a copy. Thus if you later change 'jewel', the Monster's Treasure is unchanged.

This topic is closed to new replies.

Advertisement