school/cs142/lab05/game/monopoly.cpp

176 lines
4.8 KiB
C++
Raw Normal View History

2016-04-06 20:45:34 -07:00
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>
using namespace std;
struct property{
string name;
int value;
};
void roll_the_dice() {
srand(time(NULL));
int roll = rand() % 6 +1;
int roll2 = rand() % 6 +1;
cout << roll << " + "<< roll2 << " equals " << roll +roll2 << endl;
}
void report_current_money(int & current_money) {
cout << "this is your current money" << endl;
cout << current_money << endl;
}
void add_money(int & current_money) {
cout << "how much money would you like to add? " << endl;
int add;
cin >> add;
if(add < 0){
cout << "please enter a positive number" << endl;
add_money(current_money);
}
else {
current_money += add;
cout << current_money << endl;
}
}
void spend_money(int & current_money) {
cout << "how much money would you like to spend? " << endl;
int minus;
cin >> minus;
if(current_money - minus < 0) {
cout << "This would result in a negative number!" << endl;
}
else {
current_money -= minus;
cout << current_money << endl;
}
}
void pass_go(int & current_money) {
cout << "you passed go, collect 200 dollars!" << endl;
current_money += 200;
}
bool has_property(vector<property> & props, string name) {
bool has_prop = false;
for(unsigned int i = 0; i < props.size(); i++) {
if(name == props[i].name) {
has_prop = true;
}
}
return has_prop;
}
void buy_property(vector<property> & properties, int & current_money) {
bool success = false;
int property_value = 0;
string input_name;
while(!success) {
cout << "please enter property name: ";
cin >> input_name;
if (not has_property(properties, input_name) ){
cout << "What is the value of your property?" << endl;
cin >> property_value;
while(property_value < 0) {
cout << "incorrect property value. Please try again." << endl;
cin >> property_value;
}
if(current_money - property_value < 0) {
cout << "you dont have enough money to purchase this." << endl;
break;
}
current_money -= property_value;
properties.push_back(property{input_name, property_value});
success = true;
}
else {
cerr << "you already have that property" << endl;
break;
}
}
}
void sell_property(vector<property> & properties, int & current_money) {
string property_to_sell;
cout << "please enter the property name you would like to sell: " << endl;
cin >> property_to_sell;
if(has_property(properties, property_to_sell)) {
for(unsigned int i = 0; i < properties.size(); i++) {
if(properties[i].name == property_to_sell) {
current_money += properties[i].value;
properties.erase(properties.begin()+i);
}
}
}
else {
cout << "you dont own that property to be able to sell it" << endl;
}
}
void quit_game() {
cout << "thanks for playing!" << endl;
exit (0);
}
ostream & operator<<(ostream & os, const property & prop) {
os << "{name: '" << prop.name
<< "', value: " << prop.value
<< "}";
return os;
}
void display_properties(vector<property> & properties) {
cout << "found the following "
<< properties.size() << " properties:" << endl;
for(unsigned int i = 0; i < properties.size(); i++) {
cout << properties[i] << endl;
}
}
void play_game(vector<property> & properties, int & current_money) {
bool keep_playing = true;
while(keep_playing){
int choice;
cout << "Welcome to Monopoly" << endl;
cout << "please enter a number correspoding to what function you would like to run" << endl;
cout << "1) Roll the dice\n2) Report the player's current money\n3) Add money\n4) Spend money" << endl;
cout << "5) Pass GO\n6) Buy Property\n7)Sell Property\n8)Display owned properties\n9) Quit the program" << endl;
cin >> choice;
if(choice == 1)
roll_the_dice();
else if(choice == 2)
report_current_money(current_money);
else if(choice == 3)
add_money(current_money);
else if(choice == 4)
spend_money(current_money);
else if(choice == 5)
pass_go(current_money);
else if(choice == 6)
buy_property(properties, current_money);
else if(choice == 7)
sell_property(properties, current_money);
else if(choice == 8)
display_properties(properties);
else if(choice == 9)
quit_game();
}
}
int main() {
int current_money = 1500;
vector<property> properties;
play_game(properties, current_money);
return 0;
}