Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. class Weapon{
  7. public:
  8.     string weaponName;
  9.     int range;
  10.     int damage;
  11.     float weight;
  12.     float cost;
  13.  
  14.     Weapon(string n,int rang,int dam,float w,float c){
  15.         weaponName=n;
  16.         damage=dam;
  17.         range=rang;
  18.         weight = w;
  19.         cost=c;
  20.     }
  21. };
  22.  
  23. class hashTable{
  24.  
  25. public:
  26.     int tableLength;    // records the max size of the table
  27.     int numItems;       // records number of items in the list
  28.     Weapon **table; //hashtable itself
  29.  
  30.     hashTable(int size){
  31.         tableLength=size;
  32.         numItems=0;
  33.         table = new Weapon*[tableLength];
  34.         for (int i=0;i<tableLength;i++){
  35.             table[i]=NULL;
  36.         }
  37.     }
  38.  
  39.  
  40.     int hash( string key )
  41.     {
  42.         int value = 0;
  43.         for ( int i = 0; i < key.length(); i++ )
  44.             value = value + key[i];
  45.         return value % tableLength;
  46.     }
  47.  
  48.     void put(Weapon *item){
  49.         int location= hash(item->weaponName); //gets location in table based on name
  50.         while (table[location]!=NULL){
  51.             location=(location+1);      // look one down
  52.             location=location%tableLength; // to ensure wraparound at end of array
  53.         }
  54.         table[location]=item;
  55.         numItems++;
  56.     }
  57.  
  58.     Weapon* get(string key){
  59.         int location= hash(key); //gets location in table based on key
  60.         while (table[location]!=NULL && key.compare(table[location]->weaponName)!=0){  // not empty and not item
  61.             location=(location+1);      // look one down
  62.             location=location%tableLength; // to ensure wraparound at end of array
  63.         }
  64.         return table[location];
  65.     }
  66.  
  67.     void printTable(){
  68.         int count = 0;
  69.         for (int x=0;x<tableLength;x++){
  70.             if (table[x]!=NULL){
  71.                 cout<<"Name: "<<table[x]->weaponName<<"   Damage:"<<table[x]->damage<<"    Cost:"<<table[x]->cost<<endl;
  72.             }
  73.         }
  74.     }
  75. };
  76.  
  77. class Player{
  78. public:
  79.     string name;
  80.     Weapon ** backpack;
  81.     int numItems;
  82.     float money;
  83.  
  84.     Player (string n,float m){
  85.         name=n;
  86.         money =m;
  87.         numItems=0;
  88.         backpack=new Weapon*[10];
  89.     }
  90.  
  91.     void buy (Weapon * w){
  92.         cout<<w->weaponName<<" bought..."<<endl;
  93.         backpack[numItems]=w;
  94.         numItems++;
  95.         cout<<numItems;
  96.     }
  97.     void withdraw(float amt){
  98.         money=money-amt;
  99.     }
  100.  
  101.     bool inventoryFull(){
  102.         bool full= false;
  103.         if (numItems==10)full=true;
  104.         return full;
  105.     }
  106.  
  107.  
  108.     void printCharacter(){
  109.         cout<<" Name:"<<name<<"\n Money:"<<money<<endl;
  110.         printBackpack();
  111.     }
  112.  
  113.     void printBackpack(){
  114.         cout<<" "<<name<<", you own "<<numItems<<" Weapons:"<<endl;
  115.         for (int x=0;x<numItems;x++){
  116.             cout<<" "<<backpack[x]->weaponName<<endl;
  117.         }
  118.         cout<<endl;
  119.     }
  120.  
  121. };
  122.  
  123. void addWeapons(hashTable h){
  124.     cout<<"***********WELCOME TO THE WEAPON ADDING MENU*********"<<endl;
  125.     string weaponName;int weaponRange;int weaponDamage;float weaponWeight;float weaponCost;
  126.     cout<<"Please enter the NAME of the Weapon ('end' to quit):";cin>>weaponName;
  127.     while (weaponName.compare("end")!=0){
  128.         cout<<"Please enter the Range of the Weapon (0-10):";cin>>weaponRange;
  129.         cout<<"Please enter the Damage of the Weapon:";cin>>weaponDamage;
  130.         cout<<"Please enter the Weight of the Weapon (in pounds):";cin>>weaponWeight;
  131.         cout<<"Please enter the Cost of the Weapon:";cin>>weaponCost;
  132.         Weapon *w= new Weapon(weaponName,weaponRange,weaponDamage,weaponWeight,weaponCost);
  133.         h.put(w);
  134.         cout<<"Please enter the NAME of another Weapon ('end' to quit):";cin>>weaponName;
  135.     }
  136. }
  137.  
  138. void showRoom(hashTable ht,Player *p){
  139.     string choice;
  140.     cout<<"WELCOME TO THE SHOWROOM!!!!"<<endl;
  141.     ht.printTable();
  142.     cout<<" You have "<< p->money<<" money."<<endl;
  143.     cout<<"Please select a weapon to buy('end' to quit):";cin>>choice;
  144.     while (choice.compare("end")!=0&& !p->inventoryFull()){
  145.         Weapon *w= ht.get(choice);
  146.         if (w!=NULL){
  147.             if (w->cost > p->money){
  148.                 cout<<"Insufficient funds to buy "<<w->weaponName<<endl;
  149.             }else{
  150.                 p->buy(w);
  151.                 p->withdraw(w->cost);
  152.             }
  153.         }else{
  154.             cout<<" ** "<<choice<<" not found!! **"<<endl;
  155.         }
  156.         cout<<"Please select another weapon to buy('end' to quit):";cin>>choice;
  157.     }
  158.     cout<<endl;
  159. }
  160.  
  161.  
  162. int main (){
  163.     string pname;
  164.     cout<<"Please enter Player name:"<<endl;
  165.     cin>>pname;
  166.     Player pl (pname,45);
  167.     hashTable ht(101);
  168.     addWeapons(ht);
  169.     showRoom(ht,&pl);
  170.     pl.printCharacter();
  171.  
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement