Question
Please code in C language starter code is: #include #include #include //There will be m players in an array typedef struct Player { int id;
Please code in C language starter code is:
#include
#include
#include
//There will be m players in an array
typedef struct Player
{
int id;
int loc;
long balance;
} TPlayer;
//There will be n properties in an array
typedef struct Property
{
int id;
int owner_id;
int rent;
} TProperty;
//TODO
//Implement the following function
//The player p1 needs to pay the player p2 'amount' of dollars
//If p1 has enough balance, the transaction will be successful
//Otherwise, p1 will pay all of their balance to p2, and the transaction
//is not successful
//return 1 if the transaction is successful
//Otherwise, return 0
int transaction(TPlayer *p1, TPlayer *p2, int amount)
{
}
//TODO
//Finish the following function
//If one player cannot pay rent to another player, this function should return 0.
//The rest of the players will not get chances to play when this happens
int one_round(int m, int n, TPlayer p[], TProperty prop[])
{
for(int i = 0; i
{
int steps = rand() % 6 + 1 + rand() % 6 + 1;
//fill in the code below
}
return 1;
}
//used for printing out results
void print_result(int m, TPlayer p[])
{
printf(" id balance ");
for(int i = 0; i
{
printf("%8d %8ld ", p[i].id, p[i].balance);
}
long sum = 0;
long max = 0;
for(int i = 0; i
{
sum += p[i].balance;
if(p[i].balance > max) max = p[i].balance;
}
printf("average: %f max: %ld, max/average = %lf ", (double)sum/m, max, (double)max*m/sum);
}
//max_rounds is needed because the game may never finish
void monopoly(int m, int n, TPlayer p[], TProperty prop[], int max_rounds)
{
srand(12345);
int rounds = 1;
while(one_round(m, n, p, prop) && rounds
{
rounds ++;
}
print_result(m, p);
printf("after %d rounds ", rounds);
}
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]);
assert(n >= 13);
assert(m >= 1);
assert(rounds >= 1);
int rounds = atoi(argv[3]);
TPlayer p[m];
TProperty prop[n];
for(int i = 0; i
{
prop[i].id = i;
prop[i].owner_id = -1;
prop[i].rent = i + 1;
}
for(int j = 0; j
{
p[j].id = j;
p[j].loc = 0;
p[j].balance = n;
}
monopoly(m, n, p, prop, rounds);
return 0;
}
Exercise 2. (50 points) Monopoly In this exercise, we will implement a simplified Monopoly game. We will see whether and when pure luck can create extreme inequality in wealth. Our Monopoly game has two major components: an array of players and an array of properties. The number of players is m and the number of properties is n. The n properties are arranged in a circular fashion. The index of the properties starts from 0 and goes to n1 in a clockwise direction. Then the index goes back to 0 , the starting point. At the beginning of the game, all the players have n dollars as their initial balance. All the player start at property 0 , and players take turns according to the increasing order of their id. When it is a player's turn, the player tosses two dice to determine the number of steps they need to move in a clockwise direction. The face value of the two 6 -sided dice are summed to find the number of steps. The player moves this number of steps and lands on the corresponding property. If this property is owned by someone other than this player, this player needs to pay rent to the owner. Otherwise, if this property has no owner yet, it will be owned by this player. Rent increase linearly from 1 to n between the first and final properties. Whenever a player goes back to or passes over property 0,n dollars will be awarded to the player. The game ends when a player is required to pay more money than they own. When this happens, that player pays all of their money to the player who owns the property they just landed on. At the end of the game, we will estimate the "inequality" of the players as the highest wealth divided by the average wealth. To describe a player, we need to use the structure typedef struct Player \{ int id; int loc; long balance; \} TPlayer; Here, id is in the range 0m1, loc is in the range 0n1, and balance represents the wealth accumulated by the user. Similarly, to describe a property, we use the following structure
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started