Population

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();
    }

¡¡

The running result is like following:

want to use fixed seed like 248?Enter the name and price of the 6 properties:
player 1 has 1500$, is at position -1 and is not elimininated
player 2 has 1500$, is at position -1 and is not elimininated
player 3 has 1500$, is at position -1 and is not elimininated
property A costs 1000$, its owner is bank
property B costs 800$, its owner is bank
property C costs 400$, its owner is bank
property D costs 300$, its owner is bank
property E costs 1200$, its owner is bank
property F costs 200$, its owner is bank
player 1 moves 4 steps, stops on D, buys D and now has 1200$
player 2 moves 6 steps, stops on F, buys F and now has 1300$
player 3 moves 5 steps, stops on E, buys E and now has 300$
player 1 moves 6 steps, stops on D, player 1 cannot buy D because it belongs to player 1
player 2 moves 3 steps, stops on C, buys C and now has 900$
player 3 moves 8 steps, stops on A, player 3 is eliminated
player 1 moves 4 steps, stops on B, buys B and now has 400$
player 2 moves 12 steps, stops on C, player 2 cannot buy C because it belongs to player 2
player 1 moves 3 steps, stops on E, player 1 cannot buy E because it belongs to player 3
player 2 moves 9 steps, stops on F, player 2 cannot buy F because it belongs to player 2
player 1 moves 10 steps, stops on C, player 1 cannot buy C because it belongs to player 2
player 2 moves 6 steps, stops on F, player 2 cannot buy F because it belongs to player 2
player 1 moves 7 steps, stops on D, player 1 cannot buy D because it belongs to player 1
player 2 moves 7 steps, stops on A, player 2 is eliminated
player 1 moves 8 steps, stops on F, player 1 cannot buy F because it belongs to player 2
player 1 moves 6 steps, stops on F, player 1 cannot buy F because it belongs to player 2
player 1 moves 4 steps, stops on D, player 1 cannot buy D because it belongs to player 1
player 1 moves 7 steps, stops on E, player 1 cannot buy E because it belongs to player 3
player 1 moves 10 steps, stops on C, player 1 cannot buy C because it belongs to player 2
player 1 moves 5 steps, stops on B, player 1 cannot buy B because it belongs to player 1
player 1 moves 5 steps, stops on A, player 1 is eliminated
player 1 has 400$, is at position 1 and is elimininated
player 2 has 900$, is at position 1 and is elimininated
player 3 has 300$, is at position 1 and is elimininated
property A costs 1000$, its owner is bank
property B costs 800$, its owner is player 1
property C costs 400$, its owner is player 2
property D costs 300$, its owner is player 1
property E costs 1200$, its owner is player 3
property F costs 200$, its owner is player 2