Simple Monopoly

1。The problem of human

This is a simple C++ assignment to simulate the monopoly game but in extremely simple mode. 

 

2. This is head file

////////////////////////////////////////////////////
//Program Title: Monopoly
//Purpose: To practice structures
//Name: Qingzhe Huang
//ID: 5037735
//Section: X
////////////////////////////////////////////////////
#include <iostream>
#include <ctime>
using namespace std;
struct Property
{
	char name;
	int price;
	int owner;   //the index of player
};
struct Player
{
	int money;
	int position;
	bool eliminated;
};
const int PlayerNum = 3;
const int PropertyNum = 6;
const int InitMoney = 1500;
Property properties[PropertyNum];
Player	players[PlayerNum];
void initialize();
void display();
void transact(int playerIndex);
void playGame();
bool nextPlayer();
int roll1Dice();
int roll2Dice();
bool canPlay();

3. This is my cpp file

 

#include "Monopoly.h"
int main()
{
	char choice;
	cout<<"want to use fixed seed like 248?";
	cin>>choice;
	//thru this simple choice you can set up seed
	if (choice=='y')
	{
		srand(248);
	}
	else
	{
		srand(time(NULL));
	}
	//input data
	initialize();
	display();
	while (canPlay())
	{
		playGame();
	}
	
	display();
	return 0;
}
//check all player to see if there is player to play
bool hasPlayer()
{
	for (int i=0; i< PlayerNum; i++)
	{
		if (!players[i].eliminated)
		{
			return true;
		}
	}
	return false;
}
//check all property to see if there is property that is not owned by bank
bool hasProperty()
{
	for (int j=0; j<PropertyNum; j++)
	{
		if (properties[j].owner == -1)
		{
			return true;
		}
	}
	return false;
}
//only when there is player and there is property left that you can play
bool canPlay()
{
	bool hasPlayer();
	bool hasProperty();
	return (hasPlayer()&&hasProperty());
}
	
//input data 
void initialize()
{
	cout<<"Enter the name and price of the 6 properties:"<<endl;
	for (int i =0; i< PropertyNum; i++)
	{
		cin>>properties[i].name>>properties[i].price;
		properties[i].owner = -1; //-1 means the bank owns it
	}
	for (int j=0; j< PlayerNum; j++)
	{
		players[j].position = -1;//starting pos
		players[j].money = InitMoney;
	}
}
void display()
{
	for (int j =0; j< PlayerNum; j++)
	{
		//THE POSITION = index + 1
		cout<<"player "<<j+1<<" has "<<players[j].money<<"$, is at position "
			<<(players[j].position==-1?-1:players[j].position+1)<<" and is "
			<<(players[j].eliminated?" ":" not ")<<"elimininated"<<endl;
	}
	for (int i=0; i< PropertyNum; i++)
	{
		cout<<"property "<<properties[i].name<<" costs "<<properties[i].price
			<<"$, its owner is";
		if (properties[i].owner==-1)
			cout<<" bank";
		else
			cout<<" player "<<properties[i].owner + 1;
		cout<<endl;
	}
}
void transact(int playerIndex)
{
	int pos=0;
	//as this variable is used with more than one time
	pos = players[playerIndex].position;.
	if (properties[pos].owner == -1) //if it is bank property
	{
		//if money is less then definitely eliminated
		if (players[playerIndex].money < properties[pos].price)
		{
			players[playerIndex].eliminated = true;
			cout<<" player "<<playerIndex + 1<<" is eliminated"<<endl;			
		}
		else
		{
			//buy it
			players[playerIndex].money -= properties[pos].price;
			properties[pos].owner = playerIndex;
			cout<<" buys "<<properties[pos].name<<" and now has "
				<<players[playerIndex].money<<"$"<<endl;
			//if the money is 0, still eliminate it.
			if (players[playerIndex].money==0)  
			{
				cout<<"player "<<playerIndex + 1<<" is eliminated"<<endl;
				players[playerIndex].eliminated = true;
			}
		}
	}
	else
	{
		//if it is not bank property, you are safe to play one more round
		cout<<" player "<<playerIndex + 1<<" cannot buy "<<properties[pos].name
			<<" because it belongs to player "<<properties[pos].owner + 1<<endl;
	}
}
void playGame()
{
	int temp =0;
	for (int i =0; i< PlayerNum; i++)
	{
		if (!players[i].eliminated)
		{
			temp = roll2Dice();
			cout<<"player "<<i + 1<<" moves "<<temp<<" steps, stops on ";
			players[i].position +=  temp; 
			//modus gives you the round effect
			players[i].position %= PropertyNum;
			cout<<properties[players[i].position].name<<",";
			transact(i);			
		}
	}
}
	
			
//mod it and shift by one to get the step, otherwist it is index
int roll1Dice()
{
	return rand()%6 + 1;
}
int roll2Dice()
{
	return roll1Dice() + roll1Dice();
}


 

                                                        back.gif (341 bytes)       up.gif (335 bytes)         next.gif (337 bytes)

Hosted by www.Geocities.ws

1