adding cs142 code

This commit is contained in:
dm
2016-04-06 20:45:34 -07:00
parent 552d456d53
commit 8e52ce1982
174 changed files with 14064 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
CXXFLAGS=-Wall -g -std=c++0x
all: monopoly
clean:
rm -vf monopoly
test: all
./monopoly
debug: all
gdb monopoly
+10
View File
@@ -0,0 +1,10 @@
CXXFLAGS=-Wall -g -std=c++0x
all: monopoly
clean:
rm -vf monopoly
test: all
./monopoly
debug: all
gdb monopoly
+18
View File
@@ -0,0 +1,18 @@
#include <iostream>
#include <vector>
using namespace std;
struct property {
string name;
int value;
}
void create_board(vector<property> & board) {
board.push_back(property{"mediterranean Avenue", 60}
}
int main() {
vector<property> board;
cout << board.name << endl;
}
+175
View File
@@ -0,0 +1,175 @@
#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;
}
@@ -0,0 +1,40 @@
Mediterranean Avenue -60
Community Chest
Baltic Avenue - 60
Income Tax
Reading Railroad - 200
Oriental Avenue - 100
Chance
Vermont Avenue - 100
Connecticut Avenue - 120
Jail
St. Charles Place - 140
Electric Company - 150
States Avenue - 140
Virginia Avenue - 160
Pennsylvania Railroad - 200
St. James Place - 180
Community Chest
Tennessee Avenue - 180
New York Avenue - 200
Free parking
Kentucky Avenue - 220
Chance
Indiana Avenue - 220
Illinois Avenue - 240
B&O Railroad - 200
Atlantic Avenue - 260
Ventnor Avenue - 260
Water Works - 150
Marvin Gardens - 280
Go to jail
Pacific avenue - 300
North Carolina Avenue - 300
Community chest
Pennsylvania Avenue - 320
Short line - 200
Chance
Park Place - 350
Luxury tax
Boardwalk - 400
Go
+182
View File
@@ -0,0 +1,182 @@
#include <iostream>
#include <cstdlib>
#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) {
cout << "enter value for sale" << endl;
int sale;
cin >> sale;
if(sale < 0){
cout << "can't be nagative" << endl;
break;
}
properties[i].value = sale;
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;
}