I am programming monopoly, and I have this program.
I have two players, both start from "GO" which is equal to position 1, then I created a RNG that works as tossing two dices. How do I make the program to do call a function whenever player1 or player2 reaches spot 8. I have my written code inside of main, and I believe it is commented out. Someone please take a look at it and help me!
here is my code:
#include <iostream>
#include <string>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <vector>
using namespace std;
struct Position {
string name;
int price;
int owner;
string color;
int position;
int numberOfPosition;
int house = 0;
};
struct Player {
int money = 1500;
int position = 1;
int turn;
string name;
//Position positionn;
//int number; // number of player
bool eliminated = true;
};
//*********************************************************************************
void startGame();
int dice1();
int dice2();
//int position(Player player1, int diceTotal, Position P[]);
//int position(Player player1, int diceTotal, Position &p);
void assignment(Position positionArray[]);
void doMath(Position positionArray[], Player player);
void getOutOfJail(Position positionArray[], Player player);
void palyersAssignment(vector<Player> &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
int main()
{
int roleIterator = 0;
int newPosition = 0;
int newPosition2 = 0;
// int wastemytime;
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<Player> 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;
}
//}while(storeValues[0].eliminated!=true);
int totalDice = dice1();
cout << endl << endl;
int totalDice1 = dice2(); // for the loop
storeValues[0].position = 8;
chance(storeValues,0);
cout <<"The new position is: " <<storeValues[0].position << endl;
// do
//****************************************************************************************
/*
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], totalDice, positionArray);
storeValues[0].position = newPosition;// prints the new position of player 1
cout << "Player 1 position is: " << newPosition << endl;
if (storeValues[0].position == 8)
chance(storeValues[0]);
cout << "Player 2 position is: " << newPosition2 << endl << endl << endl;
storeValues[1].position = newPosition2;
doMath(positionArray, storeValues[roleIterator]); // go to jail?
cout << "End of Loop" << endl << endl;
roleIterator++;
}
//while (!storeValues[0].eliminated || !storeValues[1].eliminated );// it is going to loop once rn!
*/
//cout << "The new Player 1 position is: " << newPosition << endl; // works
//cout << "The new Player 2 position is: " << newPosition2 << endl; // works
system("pause");
return 0;
}
void chance( Player storeValues[], int 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 {
bool did_U_RoleDobls = true; // I did role doubles
int sumOfDices = dice1();
if (player.position == 66)
{
cout << "Now you are in Jail" << 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 palyersAssignment(vector<Player> &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 = rand() % 6 + 1; // assigning the number ;
t1 = rand() % 6 + 1;
cout << "This is dice1(), for player 1 " << "1st dice: " << t << " 2nd dice: " << t1 << endl;
int sum = t + t1;
cout << "The sum of the dices is: " << sum << endl;
return sum;
}
int dice2()
{
int t, t1;
t = rand() % 6 + 1; // assigning the number ;
t1 = rand() % 6 + 1;
cout << "This is dice2(), For player 2 " << "1st dice: " << t << " 2nd dice: " << t1 << endl;
int sum = t + t1;
cout << "The sum of the dices is: " << sum << endl << endl;
return sum;
}
void doMath(Position positionArray[], Player player) // make the position of the struct assigned to a position spot
{
if (player.position == 11)
{
player.position = 66; // In jail
}
// calling the getOutOfJail function
getOutOfJail(*&positionArray, player); // idk if this function call works?
}
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.position].position = storeValues.position;
return storeValues.position;