<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><font color="#FF0000" size="6">&nbsp;<b>Simple Monopoly</b></font>    
</p>

<p align="left"><b><font color="#FF0000" size="4">1。The problem of human</font></b>              
</p>
<p align="left">This is a simple C++ assignment to simulate the monopoly game 
but in extremely simple mode.　              
</p>

<p align="left">　           
</p>

<p align="left"><font size="4" color="#FF0000"><b>2. This is head file</b></font>      
</p>
 
<pre>////////////////////////////////////////////////////
//Program Title: Monopoly
//Purpose: To practice structures
//Name: Qingzhe Huang
//ID: 5037735
//Section: X
////////////////////////////////////////////////////
</pre>
<pre>#include &lt;iostream&gt;
#include &lt;ctime&gt;</pre>
<pre>using namespace std;</pre>
<pre>struct Property
{
	char name;
	int price;
	int owner;   //the index of player
};</pre>
<pre>struct Player
{
	int money;
	int position;
	bool eliminated;
};</pre>
<pre>const int PlayerNum = 3;
const int PropertyNum = 6;
const int InitMoney = 1500;</pre>
<pre>Property properties[PropertyNum];</pre>
<pre>Player	players[PlayerNum];</pre>
<pre>void initialize();</pre>
<pre>void display();</pre>
<pre>void transact(int playerIndex);</pre>
<pre>void playGame();</pre>
<pre>bool nextPlayer();</pre>
<pre>int roll1Dice();</pre>
<pre>int roll2Dice();</pre>
<pre>bool canPlay();</pre>
 
<p align="left"><font size="4" color="#FF0000"><b>3. This is my cpp file</b></font>       
</p>
 
<p align="left">　    
</p>
 
<pre>#include &quot;Monopoly.h&quot;
</pre>
<pre>int main()
{
	char choice;
	cout&lt;&lt;&quot;want to use fixed seed like 248?&quot;;
	cin&gt;&gt;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();
	}
	</pre>
<pre>	display();</pre>
<pre>	return 0;
}</pre>
<pre>//check all player to see if there is player to play
bool hasPlayer()
{
	for (int i=0; i&lt; PlayerNum; i++)
	{
		if (!players[i].eliminated)
		{
			return true;
		}
	}
	return false;
}
</pre>
<pre>//check all property to see if there is property that is not owned by bank
bool hasProperty()
{
	for (int j=0; j&lt;PropertyNum; j++)
	{
		if (properties[j].owner == -1)
		{
			return true;
		}
	}
	return false;
}</pre>
<pre>//only when there is player and there is property left that you can play
bool canPlay()
{
	bool hasPlayer();
	bool hasProperty();</pre>
<pre>	return (hasPlayer()&amp;&amp;hasProperty());
}</pre>
<pre>	</pre>
<pre>//input data 
void initialize()
{
	cout&lt;&lt;&quot;Enter the name and price of the 6 properties:&quot;&lt;&lt;endl;
	for (int i =0; i&lt; PropertyNum; i++)
	{
		cin&gt;&gt;properties[i].name&gt;&gt;properties[i].price;
		properties[i].owner = -1; //-1 means the bank owns it
	}
	for (int j=0; j&lt; PlayerNum; j++)
	{
		players[j].position = -1;//starting pos
		players[j].money = InitMoney;
	}
}</pre>
<pre>void display()
{
	for (int j =0; j&lt; PlayerNum; j++)
	{
		//THE POSITION = index + 1
		cout&lt;&lt;&quot;player &quot;&lt;&lt;j+1&lt;&lt;&quot; has &quot;&lt;&lt;players[j].money&lt;&lt;&quot;$, is at position &quot;
			&lt;&lt;(players[j].position==-1?-1:players[j].position+1)&lt;&lt;&quot; and is &quot;
			&lt;&lt;(players[j].eliminated?&quot; &quot;:&quot; not &quot;)&lt;&lt;&quot;elimininated&quot;&lt;&lt;endl;
	}
	for (int i=0; i&lt; PropertyNum; i++)
	{
		cout&lt;&lt;&quot;property &quot;&lt;&lt;properties[i].name&lt;&lt;&quot; costs &quot;&lt;&lt;properties[i].price
			&lt;&lt;&quot;$, its owner is&quot;;
		if (properties[i].owner==-1)
			cout&lt;&lt;&quot; bank&quot;;
		else
			cout&lt;&lt;&quot; player &quot;&lt;&lt;properties[i].owner + 1;
		cout&lt;&lt;endl;
	}
}</pre>
<pre>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 &lt; properties[pos].price)
		{
			players[playerIndex].eliminated = true;
			cout&lt;&lt;&quot; player &quot;&lt;&lt;playerIndex + 1&lt;&lt;&quot; is eliminated&quot;&lt;&lt;endl;			
		}
		else
		{
			//buy it
			players[playerIndex].money -= properties[pos].price;
			properties[pos].owner = playerIndex;
			cout&lt;&lt;&quot; buys &quot;&lt;&lt;properties[pos].name&lt;&lt;&quot; and now has &quot;
				&lt;&lt;players[playerIndex].money&lt;&lt;&quot;$&quot;&lt;&lt;endl;</pre>
<pre>			//if the money is 0, still eliminate it.
			if (players[playerIndex].money==0)  
			{
				cout&lt;&lt;&quot;player &quot;&lt;&lt;playerIndex + 1&lt;&lt;&quot; is eliminated&quot;&lt;&lt;endl;
				players[playerIndex].eliminated = true;
			}
		}
	}
	else
	{
		//if it is not bank property, you are safe to play one more round
		cout&lt;&lt;&quot; player &quot;&lt;&lt;playerIndex + 1&lt;&lt;&quot; cannot buy &quot;&lt;&lt;properties[pos].name
			&lt;&lt;&quot; because it belongs to player &quot;&lt;&lt;properties[pos].owner + 1&lt;&lt;endl;
	}
}</pre>
<pre>void playGame()
{
	int temp =0;
	for (int i =0; i&lt; PlayerNum; i++)
	{
		if (!players[i].eliminated)
		{
			temp = roll2Dice();
			cout&lt;&lt;&quot;player &quot;&lt;&lt;i + 1&lt;&lt;&quot; moves &quot;&lt;&lt;temp&lt;&lt;&quot; steps, stops on &quot;;
			players[i].position +=  temp; 
			//modus gives you the round effect
			players[i].position %= PropertyNum;
			cout&lt;&lt;properties[players[i].position].name&lt;&lt;&quot;,&quot;;
			transact(i);			
		}
	}
}
	</pre>
<pre>			
//mod it and shift by one to get the step, otherwist it is index
int roll1Dice()
{
	return rand()%6 + 1;
}</pre>
<pre>int roll2Dice()
{
	return roll1Dice() + roll1Dice();
}</pre>
 
<pre></pre>

<p>　</p>

<p><b><font color="#FF0000">The running result is like following:</font></b></p>

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

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="population1.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>