Example #1
0
void Market::Buy(void){
	Hero* h;
	int itemchoice,c=0,choice,i,tlvl,tmn;
	
	h=heroes->chooseHero("use for buying");                                     //choose Hero
	tlvl=h->get_level();
	tmn=h->get_money();

	cout<<"Type 1 to see available weapons\n     2 to see available armors\n     3 to see available potions\n     4 to see available spells.\n(More items will appear while you gain level and money)\nInput : ";
	cin>>choice;
	while(choice<1||choice>4){
		cout<<"Invalid Input.Type again : ";
		cin>>choice;
	}

	list<Weapon*>::iterator it;
	list<Armor*>::iterator ita;
	list<Potion*>::iterator itb;
	list<Spell*>::iterator itc;

	switch(choice){
		case 1:                                                                 //buy weapons
  			for ( i=0,it=weapon_list.begin() ; it!= weapon_list.end(); ++it ){  //print only the items which the hero is able to buy
				if( (*(*it)).able(tlvl,tmn) ){
                	cout<<++i<<" : ";
					(*(*it)).printItem();
				}
			}

			if(i==0){cout<<"No weapons can be bought with your money and level\nincrease them and return."<<endl;break;}

			cout<<"Type the number of the weapon you want to buy or 0 to exit : "<<endl;
			cin>>itemchoice;

			while(itemchoice<0 || itemchoice>i){
				cout<<"Wrong Input.Type again : ";
				cin>>itemchoice;
			}

  			for ( it=weapon_list.begin() ; it!= weapon_list.end(); ++it ){      //search list for the item choosen by input from those 
				if((*(*it)).able(tlvl,tmn) ){                               	//that the player can buy
                	c++;
				if(c==itemchoice)
				    break;
				}
			}

			if(h->add_to_bag( (*it) )){

				h->pay_for_item( (*it)->getprice() );
			}

			break;

		case 2:                                                                 //buy armor
  			for ( i=0,ita=armor_list.begin() ; ita != armor_list.end(); ++ita ){
				if( (*(*ita)).able(tlvl,tmn) ){
                cout<<++i<<" : ";
				(*(*ita)).printItem();
				}
			}
			
			if(i==0){cout<<"No armors can be bought with your money and level\nincrease them and return."<<endl;break;}

			cout<<"Type the number of the armor you want to buy or 0 to exit : "<<endl;
			cin>>itemchoice;
			while(itemchoice<0 || itemchoice>i){
				cout<<"Wrong Input.Type again : ";
				cin>>itemchoice;
			}
			
				for ( ita=armor_list.begin() ; ita!= armor_list.end(); ++ita ){
				if((*(*ita)).able(tlvl,tmn) ){
                	c++;
				if(c==itemchoice)
				    break;
				}
			}

			if(h->add_to_bag( (*ita) )){
				h->pay_for_item( (*ita)->getprice() );
            }
            
			break;
			
		case 3:                                                                 //buy potions
  			for ( i=0,itb=potion_list.begin() ; itb != potion_list.end(); ++itb ){

				if( (*(*itb)).able(tlvl,tmn) ){
                cout<<++i<<" : ";
				(*(*itb)).printItem();

				}
			}
			
			if(i==0){cout<<"No armors can be bought with your money and level\nincrease them and return."<<endl;break;}

			cout<<"Type the number of the armor you want to buy or 0 to exit : "<<endl;
			cin>>itemchoice;
			while(itemchoice<0 || itemchoice>i){
				cout<<"Wrong Input.Type again : ";
				cin>>itemchoice;
			}
			
				for ( itb=potion_list.begin() ; itb!= potion_list.end(); ++itb ){
				if((*(*itb)).able(tlvl,tmn) ){
                	c++;
				if(c==itemchoice)
				    break;
				}
			}

			if(h->add_to_bag( (*itb) )){

				h->pay_for_item( (*itb)->getprice() );
			}
			
			break;
			
        case 4:                                                                 //buy spells
			for ( i=0,itc=spell_list.begin() ; itc != spell_list.end(); ++itc ){
				if( (*(*itc)).able(tlvl,tmn) ){
                cout<<++i<<" : ";
				(*(*itc)).printSpell();

				}
			}
			
			if(i==0){cout<<"No spells can be bought with your money and level\nincrease them and return."<<endl;break;}

			cout<<"Type the number of the spell you want to buy or 0 to exit : "<<endl;
			cin>>itemchoice;
			while(itemchoice<0 || itemchoice>i){
				cout<<"Wrong Input.Type again : ";
				cin>>itemchoice;
			}

			for ( itc=spell_list.begin() ; itc!= spell_list.end(); ++itc ){
				if((*(*itc)).able(tlvl,tmn) ){
                	c++;
				if(c==itemchoice)
				    break;
				}
			}
			
			if( h->add_to_bag_spell( *itc )  ){

				h->pay_for_item( (*itc)->getprice() );
			}
			
			break;
 	}

}