Question: Attached is my code for monopoly, i want the program to do the following: first my program has dices that add to the players position.

Attached is my code for monopoly, i want the program to do the following:

first my program has dices that add to the players position. Then, what i want it to do is the following:

if player1 position = 1 then he is in block or square one, then doMath(),

the math function is supposed to subtract money from the player's money according to how much the the street cost.

for example if I am at position 2, that is vine street and the cost of it is $150

so my money after stoping at block 1 would be 1500-150 = 1350;

please I have been stuck for 3 hours already.

Line 160 is where it is not functioning well

"

cout << "You are at this spot " << storeValues[i].position << " which is also at the positon array at: " << positionArray[i].position << " Is it gotta print the name of the spot? " << positionArray[i].name << endl; "

positionArray[i].position prints a junk number instead of a number that is = to the player's positoin .

someone please help me out i need this code to work!

also: i beleive my code is a mess, if you know any way to make it look better or neater so i stop staring at for good 10 mins before starting to edit stuff, please let me know what to do.

//////////////////////////////////////////////////////////////////////////

#include

#include

#include /* srand, rand */

#include /* time */

#include

#include

using namespace std;

struct Position {

string name;

int price = 999;

int owner =-1;

string color;

int position;

//int numberOfPosition;

int house = 0;

int hotel = 0;

};

struct Player {

int money = 1500;

int position = 1;

int turn;

string name;

//Position positionn;

//int number; // number of player

bool eliminated = true;

int bid;

};

//*********************************************************************************

void startGame();

int dice1();

int dice2();

//int position(Player player1, int diceTotal, Position P[]);

//int position(Player player1, int diceTotal, Position &p);

void doMath(Player storeValues[],Position positionArray[], int i);

void assignment(Position positionArray[]);

void getOutOfJail(Position positionArray[], Player player);

void palyersAssignment(vector &player, int playersToAdd);//CHAD: Added playersToAdd variable for the for loop to reference in function definition

bool Isturn(Player storeValues[], int quantity);

//int POSITION(Player storeValues[], int diceTotal, Position p[], int i);

int POSITION(Player storeValues, int diceTotal, Position p[]);

void chance(Player storeValues[], int i);

//int POSITION(Player *&storeValues, int diceTotal, Position p[], int i); // try to pass in the function as int

//********************************************************************************************************

//GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG//

//********************************************************************************************************

//GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG

std::mt19937 rng(time(NULL));

int n = 6;

std::uniform_int_distribution distribution(1, n);

int main()

{

int roleIterator = 0;

int newPosition = 0;

int newPosition2 = 0;

Position positionArray[40];

//DECLARATIONS of the spots WITHOUT PRINTING

assignment(positionArray);

srand(time(NULL));

//Vector

cout << "Enter player quantity: ";

const int quantity = 2;

//const int newQuantity;

//cin >> quantity;

cout << endl;

vector players;

//newQuantity = quantity;

Player storeValues[quantity];

palyersAssignment(players, quantity);

if (storeValues[0].money == 0)

{

storeValues[0].eliminated = true; //then

}

if (storeValues[1].money == 0)

{

storeValues[1].eliminated = true; //then game over

}

for (int i = 0; i < quantity; i++)

{

storeValues[i].name = players.at(i).name;

storeValues[i].turn = players.at(i).turn;

}

cout << endl << endl;

for (int i = 0; i < 3; i++)

{

cout << "Start of loop" << endl;

cout << "It is Player " << storeValues[0].turn << " turn" << endl;

newPosition = POSITION(storeValues[0], dice1(), positionArray);

storeValues[0].position = newPosition;

cout << "Player 1 position is: (" << newPosition << ")"<< endl << endl;;

cout << "It is player " << storeValues[1].turn << " turn" << endl; // something wrong

newPosition2 = POSITION(storeValues[1], dice2(), positionArray);

storeValues[1].position = newPosition2;

cout << "Player 2 position is: (" << newPosition2 <<")"<< endl ;

//isert the assigning function here POSITION

//POSITION( storeValues, diceTotal, positionArray);

doMath(storeValues, positionArray, i);

cout << "End of Loop" << endl << endl << endl;

roleIterator++;

}

cout << "The new Player 1 position is: (" << newPosition2 << ")" << endl; // works

cout << "The new Player 2 position is: (" << newPosition2 << ")" << endl; // works

system("pause");

return 0;

}

void doMath(Player storeValues[], Position positionArray[], int i) // make the position of the struct assigned to a position spot

{

cout << "You are at this spot " << storeValues[i].position << " which is also at the positon array at: " << positionArray[i].position << " Is it gotta print the name of the spot? " << positionArray[i].name << endl;

cout << "position Array i+1 name is: " << positionArray[i + 1].name << endl;

cout << "position Array i name is: " << positionArray[i].name << endl;

cout << "position Array price is: " << positionArray[i+1].price << endl;

storeValues[i].money -= positionArray[i].price;

cout << "Your money is: " << storeValues[i].money << endl;

cout << "Would you like to buy a house? " << endl;

char userAns;

cin >> userAns;

if (userAns = 'Y') //each house costs $300

{

storeValues[i].money -= 300;

positionArray[i].house += 1;

}

else

{

cout << "This player does not want to buy the position, do you want to buy it?" << endl;

cin >> userAns;

if (userAns = 'Y')

{

int x = storeValues[0 % 2 + 1].money -= positionArray[i].price; // how to loop the numbers

cout << "it is " << x << "Turn " << endl;

}

}

if (storeValues[i].position == 11)

{

cout << "Your position was 11 and now you are in jail" << endl;

storeValues[i].position = 66; // In jail

cout << "You have to pay $50 " << endl;

//getOutOfJail(*&positionArray, storeValues[i]); // idk if this function call works?

}

// calling the getOutOfJail function

}

int POSITION(Player storeValues, int diceTotal, Position p[]) // try to pass in the function as int

{

storeValues.position = storeValues.position + diceTotal; // the position is added to the sum of the dices .

p[storeValues[i].position].position = storeValues.position;

return storeValues.position;

}

void chance( Player storeValues[], int i)

{

//if(storeValues[i])

int card = rand() % 2 + 1; // 18 cards

cout << "Your card is " << card << endl;

switch (card)

{

case 1:

storeValues[i].position = 1; //Advance to Go (Collect $200)

storeValues[i].money += 200;

break;

case 2:

if (storeValues[i].position >= 9)

{

storeValues[i].money += 200;

}

storeValues[i].position = 9; //bow street

break;

case 3:

if (storeValues[i].position >= 27)

{

storeValues[i].money += 200;

}

storeValues[i].position = 27; //bond street

break;

}

}

bool Isturn(Player storeValues[], int quantity)

{

int turn = 1;

for (int i = 0; i < quantity; i++)

{

if (storeValues[i].turn == turn)

return true;

else {

cout << "Idk" << endl;

cout << "Try This: ";

return false;

}

turn++;

}

return true;

}

void palyersAssignment(vector &players, int playersToAdd) {

for (int i = 0; i < playersToAdd; i++)//CHAD: Altered for loop to start at 0 and execute code first then add 1 to i

{

Player newPlayer;//CHAD: Creates new player on every itteration of the for loop

cout << "player " << i + 1 << " turn is #: " << i + 1 << endl;

newPlayer.turn = i + 1;

cout << "Enter the name for " << i + 1 << endl;

cin >> newPlayer.name; // then how do I access this <- how do I give it a name?

players.push_back(newPlayer);

cout << endl;

}

}

int dice1()

{

int t, t1;

// this the generator, the seed

t = distribution(rng); // assigning the number ;

t1 = distribution(rng);

//cout << "This is dice1(), for player 1 " << "1st dice: " << t << " 2nd dice: " << t1 << endl;

int sum = t + t1;

cout << "The sum of the dices1() is: " << sum << endl;

return sum;

}

int dice2()

{

int t, t1;

t = distribution(rng); // assigning the number ;

t1 = distribution(rng);

//cout << "This is dice2(), For player 2 " << "1st dice: " << t << " 2nd dice: " << t1 << endl;

int sum = t + t1;

cout << "The sum of the dices2() is: " << sum << endl;

return sum;

}

void getOutOfJail(Position positionArray[], Player player) {

bool did_U_RoleDobls = true; // I did role doubles

cout << "If it prints the dices for 1 then it is inside getOutOfJail" << endl;

int sumOfDices = dice1();

if (player.position == 66)

{

cout << "GetOutOFJailFunction only if you role doubles" << endl;

if (did_U_RoleDobls&&sumOfDices == 12)

{

cout << "You are getting out of Jail and heading to ""Chance" << endl;

player.position = 23;

}

else

cout << "Your new balance is: " << player.money - 50 << endl; //subtract 50 from balance.

}

}

void assignment(Position positionArray[])

{

positionArray[1].name = "Go";

positionArray[1].price = 0;

//***********************************

positionArray[2].name = "Vine Street";

positionArray[2].price = 150;

positionArray[2].owner = -1;

positionArray[2].color = "brown";

positionArray[2].position = 2;

positionArray[3].name = "Community Chest";

positionArray[3].price = 0;

positionArray[3].owner = -1;

positionArray[3].color = "PLAIN";

positionArray[3].position = 3;

positionArray[5].name = "Income Tax";

positionArray[5].price = 100;

positionArray[5].owner = -1;

positionArray[5].color = "PLAIN";

positionArray[5].position = 5;

//In Jail

positionArray[6].name = "Just Visiting";

positionArray[6].position = 6;

positionArray[6].price = 0;

positionArray[8].name = "Chance";

positionArray[8].price = 0;

positionArray[8].owner = -1;

positionArray[8].color = "PLAIN";

positionArray[8].position = 8;

//

positionArray[8].name = "Chance";

positionArray[8].price = 0;

positionArray[8].owner = -1;

positionArray[8].color = "PLAIN";

positionArray[8].position = 8;

//

positionArray[8].name = "Chance";

positionArray[8].price = 0;

positionArray[8].owner = -1;

positionArray[8].color = "PLAIN";

positionArray[8].position = 8;

// 11

positionArray[11].name = "Go TO Jail";

positionArray[8].price = 0;

positionArray[8].owner = -1;

positionArray[8].color = "PLAIN";

positionArray[8].position = 8;

//

positionArray[23].name = "Chance";

}

//////////////////////////

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 Databases Questions!