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 5.1KB

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