Question: Exercise 2 . ( 5 0 points ) Monopoly Inthisexercise,wewill implementasimplifiedMonopolygame.Wewill seewhetherandwhenpure luck cancreateextremeinequalityinwealth. OurMonopolygamehastwomajorcomponents: anarrayofplayersandanarrayofproperties.Thenumber ofplayersismandthenumberofpropertiesisn. Thenpropertiesarearrangedinacircular fashion. The indexof thepropertiesstarts from 0 andgoesto

Exercise2.(50points)Monopoly
Inthisexercise,wewill implementasimplifiedMonopolygame.Wewill seewhetherandwhenpure luck
cancreateextremeinequalityinwealth.
OurMonopolygamehastwomajorcomponents: anarrayofplayersandanarrayofproperties.Thenumber
ofplayersismandthenumberofpropertiesisn.
Thenpropertiesarearrangedinacircular fashion. The indexof thepropertiesstarts from0andgoesto
n1inaclockwisedirection.Thentheindexgoesbackto0, thestartingpoint.
Atthebeginningof thegame,all theplayershavendollarsastheir initialbalance.All theplayerstartat
property0,andplayerstaketurnsaccordingtotheincreasingorderof their id.Whenit isaplayersturn,
theplayertossestwodicetodeterminethenumberofstepstheyneedtomoveinaclockwisedirection.The
facevalueofthetwo6-sideddicearesummedtofindthenumberofsteps.Theplayermovesthisnumberof
stepsandlandsonthecorrespondingproperty. Ifthispropertyisownedbysomeoneotherthanthisplayer,
thisplayerneedstopayrenttotheowner.Otherwise, ifthispropertyhasnoowneryet, itwillbeownedby
thisplayer.Rent increaselinearlyfrom1tonbetweenthefirstandfinalproperties.
Wheneveraplayergoesbacktoorpassesoverproperty0,ndollarswillbeawardedtotheplayer.
Thegameendswhenaplayer is requiredtopaymoremoneythantheyown. Whenthishappens, that
playerpaysallof theirmoneytotheplayerwhoownsthepropertytheyjust landedon.Attheendof the
game,wewillestimatetheinequalityoftheplayersasthehighestwealthdividedbytheaveragewealth.
Todescribeaplayer,weneedtousethestructure
typedefstructPlayer
{
intid;
intloc;
longbalance;
}TPlayer;
Here, id is inthe range0...m1, loc is inthe range0...n1, andbalancerepresents thewealth
accumulatedbytheuser.
Similarly, todescribeaproperty,weusethefollowingstructure
3
typedef struct Property
{
int id;
int owner_id;
int rent;
} TProperty;
Here, id is in the range 0...n1, owner
id is in the range 0...m1, and rent is the rent of the property.
Below is the main() function of the program; note how we use the command line arguments. Here m is
the number of players, n is the number of properties, and rounds is the maximum rounds of game in case
the game does not end.
int main(int argc, char *argv[])
{
if(argc !=4)
{
printf("Usage: %s m n rounds
", argv[0]);
return-1;
}
int m = atoi(argv[1]);
int n = atoi(argv[2]);
int rounds = atoi(argv[3]);
TPlayer p[m];
TProperty prop[n];
for(int i =0; i < n; i++)
{
prop[i].id = i;
prop[i].owner_id =-1;
prop[i].rent = i +1;
}
for(int j =0; j < m; j++)
{
p[j].id = j;
p[j].loc =0;
p[j].balance = n;
}
monopoly(m, n, p, prop, rounds);
return 0;
}
Note how we initialize the player array p[m] and the property array prop[n]. The owner id for each property
is set to 1 since, at the beginning, it is not owned by any player. We use 1 instead of 0 because id 0 is
the id for the first player.
We need to implement the following function.
//Implement the following function
void monopoly(int m, int n, TPlayer p[], TProperty prop[], int max_rounds)
{
4
srand(12345);
introunds=1;
//The followinglineofcodeisneededforcorrectoutput
print_result(m,p);
printf("after%drounds
",rounds);
}
Belowareoutputsfromafewsampleruns.Wecanusetheseoutputstocheckourcode.
$./monopoly15100100000
id balance
067
195
2204
3244
4102
5228
60
772
833
90
1054
1127
1276
1318
14280
average:100.000000max:280,max/average=2.800000
after5rounds
$./monopoly20100100000
id balance
0340
1200
2203
3260
474
5137
6199
768
840
90
1039
1146
1216
13137
1447
1556
1628
5
1715
1857
1938
average:100.000000max:340,max/average=3.400000
after5rounds

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!