Contains the source code from the course work throughout my undergraduate Computer Engineering degree at Brigham Young University. There is a mixture of Go, Python, C, C++, Java, VHDL, Verilog, Matlab, Bash, Assembly, etc..
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

monopoly.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4. #include <vector>
  5. using namespace std;
  6. struct property{
  7. string name;
  8. int value;
  9. };
  10. void roll_the_dice() {
  11. srand(time(NULL));
  12. int roll = rand() % 6 +1;
  13. int roll2 = rand() % 6 +1;
  14. cout << roll << " + "<< roll2 << " equals " << roll +roll2 << endl;
  15. }
  16. void report_current_money(int & current_money) {
  17. cout << "this is your current money" << endl;
  18. cout << current_money << endl;
  19. }
  20. void add_money(int & current_money) {
  21. cout << "how much money would you like to add? " << endl;
  22. int add;
  23. cin >> add;
  24. if(add < 0){
  25. cout << "please enter a positive number" << endl;
  26. add_money(current_money);
  27. }
  28. else {
  29. current_money += add;
  30. cout << current_money << endl;
  31. }
  32. }
  33. void spend_money(int & current_money) {
  34. cout << "how much money would you like to spend? " << endl;
  35. int minus;
  36. cin >> minus;
  37. if(current_money - minus < 0) {
  38. cout << "This would result in a negative number!" << endl;
  39. }
  40. else {
  41. current_money -= minus;
  42. cout << current_money << endl;
  43. }
  44. }
  45. void pass_go(int & current_money) {
  46. cout << "you passed go, collect 200 dollars!" << endl;
  47. current_money += 200;
  48. }
  49. bool has_property(vector<property> & props, string name) {
  50. bool has_prop = false;
  51. for(unsigned int i = 0; i < props.size(); i++) {
  52. if(name == props[i].name) {
  53. has_prop = true;
  54. }
  55. }
  56. return has_prop;
  57. }
  58. void buy_property(vector<property> & properties, int & current_money) {
  59. bool success = false;
  60. int property_value = 0;
  61. string input_name;
  62. while(!success) {
  63. cout << "please enter property name: ";
  64. cin >> input_name;
  65. if (not has_property(properties, input_name) ){
  66. cout << "What is the value of your property?" << endl;
  67. cin >> property_value;
  68. while(property_value < 0) {
  69. cout << "incorrect property value. Please try again." << endl;
  70. cin >> property_value;
  71. }
  72. if(current_money - property_value < 0) {
  73. cout << "you dont have enough money to purchase this." << endl;
  74. break;
  75. }
  76. current_money -= property_value;
  77. properties.push_back(property{input_name, property_value});
  78. success = true;
  79. }
  80. else {
  81. cerr << "you already have that property" << endl;
  82. break;
  83. }
  84. }
  85. }
  86. void sell_property(vector<property> & properties, int & current_money) {
  87. string property_to_sell;
  88. cout << "please enter the property name you would like to sell: " << endl;
  89. cin >> property_to_sell;
  90. if(has_property(properties, property_to_sell)) {
  91. for(unsigned int i = 0; i < properties.size(); i++) {
  92. if(properties[i].name == property_to_sell) {
  93. current_money += properties[i].value;
  94. properties.erase(properties.begin()+i);
  95. }
  96. }
  97. }
  98. else {
  99. cout << "you dont own that property to be able to sell it" << endl;
  100. }
  101. }
  102. void quit_game() {
  103. cout << "thanks for playing!" << endl;
  104. exit (0);
  105. }
  106. ostream & operator<<(ostream & os, const property & prop) {
  107. os << "{name: '" << prop.name
  108. << "', value: " << prop.value
  109. << "}";
  110. return os;
  111. }
  112. void display_properties(vector<property> & properties) {
  113. cout << "found the following "
  114. << properties.size() << " properties:" << endl;
  115. for(unsigned int i = 0; i < properties.size(); i++) {
  116. cout << properties[i] << endl;
  117. }
  118. }
  119. void play_game(vector<property> & properties, int & current_money) {
  120. bool keep_playing = true;
  121. while(keep_playing){
  122. int choice;
  123. cout << "Welcome to Monopoly" << endl;
  124. cout << "please enter a number correspoding to what function you would like to run" << endl;
  125. cout << "1) Roll the dice\n2) Report the player's current money\n3) Add money\n4) Spend money" << endl;
  126. cout << "5) Pass GO\n6) Buy Property\n7)Sell Property\n8)Display owned properties\n9) Quit the program" << endl;
  127. cin >> choice;
  128. if(choice == 1)
  129. roll_the_dice();
  130. else if(choice == 2)
  131. report_current_money(current_money);
  132. else if(choice == 3)
  133. add_money(current_money);
  134. else if(choice == 4)
  135. spend_money(current_money);
  136. else if(choice == 5)
  137. pass_go(current_money);
  138. else if(choice == 6)
  139. buy_property(properties, current_money);
  140. else if(choice == 7)
  141. sell_property(properties, current_money);
  142. else if(choice == 8)
  143. display_properties(properties);
  144. else if(choice == 9)
  145. quit_game();
  146. }
  147. }
  148. int main() {
  149. int current_money = 1500;
  150. vector<property> properties;
  151. play_game(properties, current_money);
  152. return 0;
  153. }