Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <iostream>
#include <string>
#include<list>
using namespace std;
//Declaring functions and variables that an item will require
class Item
{
private:
string name1;
int value1;
int count1;
public:
//constructor
Item(string name, int value, int count);
void addItem();
void removeItem();
string getName() {
return name1;
}
int getCount() {
return count1;
}
void setCount(int count) {
count1 = count;
}
int getValue() {
return value1;
}
};
//increases the count of the item
void Item::addItem()
{
count1++;
}
//removes an item
void Item::removeItem()
{
if (count1 > 0) {
count1--;
}
}
Item::Item(string name, int value, int count)
{
name1 = name;
value1 = value;
count1 = count;
}
//Declaring the variables and functions the user will need
class Player
{
private:
string name1;
int money1;
list<Item> listofitems;
public:
Player();
void init(string name, int money);
void Inventory();
bool canAfford(string name, int money);
int getMoney() {
return money1;
}
void subtractMoney(int amount) {
money1 -= amount;
}
void addMoney(int money) {
money1 += money;
}
bool removeItem(string name, Item &newItem);
void addItem(Item newItem);
};
//initialize the player
void Player::init(string name, int money)
{
name1 = name;
money1 = money;
}
//prints out the users inventory
void Player::Inventory()
{
cout << name1 << "'s inventory **\n";
cout << "Money: " << money1 << " \n";
list<Item>::iterator items;
int i = 0;
for (items = listofitems.begin(); items != listofitems.end(); items++) {
cout << i << ". " << (*items).getName() << " x " << (*items).getCount() << " Price: " << (*items).getValue() << endl;
i++;
}
}
Player::Player()
{
}
//Removes the item from the users inventory when sold or used
bool Player::removeItem(string name, Item &newItem)
{
list<Item>::iterator ShopItem;
for (ShopItem = listofitems.begin(); ShopItem != listofitems.end(); ShopItem++) {
if ((*ShopItem).getName() == name) {
newItem = (*ShopItem);
(*ShopItem).removeItem();
if ((*ShopItem).getCount() == 0) {
listofitems.erase(ShopItem);
}
return true;
}
}
return false;
}
//Adds a new item to the users inventory
void Player::addItem(Item newItem)
{
list<Item>::iterator ShopItem;
for (ShopItem = listofitems.begin(); ShopItem != listofitems.end(); ShopItem++) {
if ((*ShopItem).getName() == newItem.getName()) {
(*ShopItem).addItem();
return;
}
}
listofitems.push_back(newItem);
listofitems.back().setCount(1);
}
//Checks to see how much is in the users account and compares to item value
bool Player::canAfford(string name, int money)
{
list<Item>::iterator ShopItem;
for (ShopItem = listofitems.begin(); ShopItem != listofitems.end(); ShopItem++) {
if ((*ShopItem).getName() == name) {
if ((*ShopItem).getValue() <= money) {
return true;
}
else {
return false;
}
}
}
return false;
}
//Declaring the variables and functions that the shop will need
class Shop
{
private:
int money1;
list<Item> items1;
string name1;
public:
Shop(string name, int money);
void addItem(Item newItem);
bool AffordItem(string name, int money);
bool purchaseItem(string name, Item &newItem);
void printShop();
string getName() {
return name1;
}
void addMoney(int money) {
money1 += money;
}
int getMoney() {
return money1;
}
};
Shop::Shop(string name, int money)
{
name1 = name;
money1 = money;
}
//Adds an new item to the shop if an existing one doesn't exist
void Shop::addItem(Item newItem)
{
list<Item>::iterator items;
for (items = items1.begin(); items != items1.end(); items++) {
if ((*items).getName() == newItem.getName()) {
(*items).addItem();
return;
}
}
items1.push_back(newItem);
items1.back().setCount(1);
}
//Checks to see if the item exists in the shop
bool Shop::purchaseItem(string name, Item &newItem)
{
list<Item>::iterator item;
for (item = items1.begin(); item != items1.end(); item++) {
if ((*item).getName() == name) {
newItem = (*item);
newItem.setCount(1);
(*item).removeItem();
if ((*item).getCount() == 0) {
items1.erase(item);
}
return true;
}
}
return false;
}
// Prints the items in the shop that are available
void Shop::printShop()
{
cout << " Welcome to " << name1 << " \n";
cout << "Money: " << money1 << " \n";
list<Item>::iterator item;
int i = 0;
for (item = items1.begin(); item != items1.end(); item++) {
cout << i << ". " << (*item).getName() << " x " << (*item).getCount() << " Price: " << (*item).getValue() << endl;
i++;
}
cout << endl;
}
//Checks to see if the user can afford to buy the item
bool Shop::AffordItem(string name, int money)
{
list<Item>::iterator item;
for (item = items1.begin(); item != items1.end(); item++) {
if ((*item).getName() == name) {
if ((*item).getValue() <= money) {
return true;
}
else {
return false;
}
}
}
return false;
}
void initializePlayer(Player &player);
void InitializeShops(list<Shop> &shops);
void AccessShop(Player &player, Shop &shop);
//As the game begins the user will be given pokeballs by default
void initializePlayer(Player &player)
{
cout << "Enter your name: ";
string name;
getline(cin, name);
player.init(name, 100);
player.addItem(Item("Pokeballs", 150, 3));
cout << endl;
}
//The shop will have some items by default in it for the user to buy
void InitializeShops(list<Shop> &shops)
{
shops.push_back(Shop("Essentials shop", 500));
shops.back().addItem(Item("Health potion", 50, 3));
shops.push_back(Shop("Exclusive items shop", 1200));
shops.back().addItem(Item("Armour", 1000, 1));
}
//Allows the player to buy or sell items from the shop
//If there are no items in the shop the user will be told
void AccessShop(Player &player, Shop &shop)
{
bool Ended = false;
char input;
string itemName;
Item newItem("NOITEM", 0, 1);
while (Ended == false) {
shop.printShop();
player.Inventory();
cout << "\nWould you like to buy or sell? Enter q to quit. (b/s): ";
cin >> input;
cin.ignore(64, '\n');
cin.clear();
if (input == 'Q' || input == 'q')
return;
if (input == 'B' || input == 'b') {
cout << "Enter the item you to buy: ";
getline(cin, itemName);
if (shop.AffordItem(itemName, player.getMoney())) {
if (shop.purchaseItem(itemName, newItem) == true) {
player.addMoney(-newItem.getValue());
player.addItem(newItem);
shop.addMoney(newItem.getValue());
}
else {
cout << "That is an invalid item!\n";
system("PAUSE");
}
}
else {
cout << "You don't have enough money!\n";
system("PAUSE");
}
}
else {
cout << "Enter the item you to sell: ";
getline(cin, itemName);
if (player.canAfford(itemName, shop.getMoney())) {
if (player.removeItem(itemName, newItem) == true) {
shop.addMoney(-newItem.getValue());
shop.addItem(newItem);
player.addMoney(newItem.getValue());
}
else {
cout << "That is not a valid item!\n";
system("PAUSE");
}
}
else {
cout << "You don't have enough money\n";
system("PAUSE");
}
}
}
}
int main()
{
Player player;
string shopName;
list<Shop> Listofshops;
list<Shop>::iterator items;
initializePlayer(player);
InitializeShops(Listofshops);
bool Ended = false;
while (Ended == false) {
cout << "Shops:\n";
int i = 1;
for (items = Listofshops.begin(); items != Listofshops.end(); items++) {
cout << i << ". " << (*items).getName() << endl;
i++;
}
cout << "\nWhat shop would you like to enter? Q to quit: ";
getline(cin, shopName);
if (shopName == "Q" || shopName == "q") {
Ended = true;
}
else {
cout << endl;
bool RealShops = false;
//check to see if the user entered a valid shop
for (items = Listofshops.begin(); items != Listofshops.end(); items++) {
if ((*items).getName() == shopName) {
AccessShop(player, (*items));
RealShops = true;
}
}
if (RealShops == false) {
cout << "Invalid Shop!\n";
system("PAUSE");
}
}
}
system("PAUSE");
return 0;
}