1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
#include <iostream>
using namespace std;
// We're going to have an object to represent the player and his/her attributes:
class player
{
public:
// The player will have potions and gold variables
int potions;
int gold;
int HamGranade;
// You can also have variables to describe the player's race, class, and name
// But for now, we'll keep it simple
public:
// Here is what's called a "constructor," this is called when the player is created
// We want the progammer (aka you) to specify the starting amounts of the player's
// gold and potion, thus the "int p" and "int g" parameters
player( int p, int g,int h )
{
// Set potions and gold
potions = p;
gold = g;
HamGranade = h;
// Notify the console:
cout << "Player was created with " << potions << " potions and " << gold << " gold." << endl;
}
// Here is what's called the "destructor," this is called when the player class is destroyed.
~player()
{
cout << "Player was destroyed :.(" << endl;
}
// Here we have a utility function to display the player's current gold and potions
void display_status()
{
cout << "You have " << gold << " gold and " << potions << " potions" << endl;
cout << "You have " << HamGranade <<"Ham Granades"<<endl;
}
}//;
// Now we're going to create a store class that handles the store operations and other such etc
class store
{
private:
// The members of a store object
// Note, this is listed under "private," which means that only the functions below can access this variable
int potions_in_stock;
int Ham_in_stock;
public:
// The constructor
store( int num_potions,int num_Ham )
{
// Stock up on potions!
potions_in_stock = num_potions;
Ham_in_stock = num_Ham;
cout << "A store was created with " << potions_in_stock << " potions in stock." << endl;
cout << Ham_in_stock <<"HamGranades"<< endl;
}
// The destructor
~store()
{
cout << "The store was destroyed :.(" << endl;
}
// The player parameter is to specify which player is entering this store
// The '&' stands for "reference to" which means that we'll directly be
// modifying the player, and not a copy of it.
// by changing it to "player the_player" we're going to be working with
// a copy of the player that is passed, which isn't what we want, we want
// to modify the object itself.
void player_enters( player &the_player )
{
// Welcome message
cout << endl;
cout << "Welcome to my store!" << endl;
// Check to see if we have some potions:
if ( potions_in_stock == 0 )
{
// We don't
cout << "Unfortunately, I'm out of potions!" << endl;
}
if ( Ham_in_stock == 0 )
{
// We don't
cout << "Unfortunately, I'm out of Ham's!" << endl;
}
else if ( the_player.gold < 5 ) // (We have potions and then) Check to see if the player has money
{
// He/she doesn't have money
cout << "Unfortunately, you requires more moneys" << endl;
}
else // (The player has money and we have potions)
{
// This is the main loop, which will run forever until this boolean turn false (or the player buys all the potions)
bool player_is_still_buying = true;
while ( player_is_still_buying && potions_in_stock > 0 ||player_is_still_buying && Ham_in_stock > 0) // Reads as while the player is still buying and we still have potions
{
// Display player's status:
cout << endl;
the_player.display_status();
if ( the_player.gold < 5 ) // The player is unable to buy any more potions
{
cout << "Looks like you're out of money lol" << endl;
player_is_still_buying = false;
break; // Force out of the "while" loop
}
// Display the menu
cout << endl;
cout << "What would you like to buy?" << endl;
cout << "1) Potion (" << potions_in_stock << ")" << endl;
cout << "2) Ham Granades (" << Ham_in_stock << ")" << endl;
cout << "3) Leave" << endl;
cout << endl;
cout << "Enter choice: ";
// Get a choice from the user
int choice;
cin >> choice;
if ( choice == 3 ) // Player decides to leave
player_is_still_buying = false; // When this is set to false, this "while" loop will stop running
else
{
// Player decides not to leave
if ( choice == 1 ) // Player decides to buy a potion
{
the_player.gold -= 5; // Subtract the money out of the player
the_player.potions += 1; // Add a potion to the player
potions_in_stock -= 1; // Subtract a potion out of the store's stock
if ( choice == 2 ) // Player decides to buy a potion
{
the_player.gold -= 5; // Subtract the money out of the player
the_player.HamGranade += 1; // Add a potion to the player
Ham_in_stock -= 1; // Subtract a potion out of the store's stock
if ( potions_in_stock == 0 ) // Player bought the last potion
{
cout << "You just bought my last potion! D:" << endl;
player_is_still_buying = false; // Nothing left to buy!
}
if ( Ham_in_stock == 0 ) // Player bought the last potion
{
cout << "You just bought my last Ham! D:" << endl;
player_is_still_buying = false; // Nothing left to buy!
}
else
cout << "Thank you for your business" << endl;
}
}
// (This is the end of the while loop, if all those '}' throw you off)
}
}
// Player is all done here.
cout << "Goodbye now!" << endl;
system("CLS");
}
}
int main( int, char ** )
{
// Temporary variables to get input from user
int g = 99, p = 6, ps = 6,h = 1,hs = 3;
// Self-explanatory:
cout << "Enter starting gold amount:" << endl;
cin >> g;
cout << "Enter starting potions amount:" << endl;
cin >> p;
cout << "Enter number of potions currently in stock (at the store)" << endl;
cin >> ps;
cout << "Enter number of Ham player1 has currently" << endl;
cin >> h;
cout << "Enter number of Ham player1 has currently in stock (at the store)" << endl;
cin >> hs;
// This code block here is for demonstration of the constructors and destructors, and isn't necessary
{
player hero( p, g ,h); // Create a player object named "hero" with "p" potions and "g" gold
store the_store( ps,hs ); // Create a store object named "the_store" with "ps" potions in stock
// Main game loop
bool is_running = true;
while ( is_running ) // will loop forever until "is_running" is false
{
// Display separator, so it is easier to read:
cout << endl << endl;
// Display current player status:
hero.display_status();
// Display available choices:
cout << "There's a store nearby, what must you do?" << endl;
cout << "1) Enter store" << endl;
cout << "2) Make gold" << endl;
cout << "3) Exit game" << endl;
cout << endl;
cout << "Enter choice: " << endl;
// Receive a choice from the user:
int choice;
cin >> choice;
if ( choice == 1 ) // Player enters the store
the_store.player_enters( hero );
else if ( choice == 2 ) // Player creates gold out of thin air
{
cout << "How much gold will you make: " << endl;
int moar_gold;
cin >> moar_gold;
hero.gold += moar_gold;
}
else if ( choice == 3 ) // Player decides he's had enough :)
is_running = false;
}
cout << "Thank you for playing!" << endl;
system("CLS");
}
system("pause");
cin.ignore();
cin.get();
return 0;
}
}
|