Plz help

Im trying to make a monopoly game.
Question1:How to make the program terminate if player has 0 or - cash?
Question2:How to make second player?
Question3:How to make a double? When rolled double player should roll dices again , if rolled 3 times , send to jail.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  #include <iostream>
#include <ctime>
#include <string>


using namespace std;

void declare();
void func_pos();

struct places {
    string name;
	int price;
	int rent;
	int number;
	int owner;
};

struct player {
    int money;
    int position;
};

player me;
places square[ 13 ];

void declare()
{
       me.money = 350;
       me.position = 0;

       square[ 0 ].name = "Start";
       square[ 0 ].number = 0;
       square[ 0 ].owner = 0;
       square[ 0 ].price = 0;
       square[ 0 ].rent = 0;

       square[ 1 ].name = "Leytonstone (Cost 200)";
       square[ 1 ].number = 1;
       square[ 1 ].owner = 0;
       square[ 1 ].price = 200;
       square[ 1 ].rent = 50;

       square[ 2 ].name = "Leyton (Cost 160)";
       square[ 2 ].number = 2;
       square[ 2 ].owner = 0;
       square[ 2 ].price = 160;
       square[ 2 ].rent = 40;

       square[ 3 ].name = "Stratford (Cost 150)";
       square[ 3 ].number = 3;
       square[ 3 ].owner = 0;
       square[ 3 ].price = 150;
       square[ 3 ].rent = 35;

       square[ 4 ].name = "Mile End (Cost 80)";
       square[ 4 ].number = 4;
       square[ 4 ].owner = 0;
       square[ 4 ].price = 80;
       square[ 4 ].rent = 15;

       square[ 5 ].name = "Bethnal Green (Cost 160)";
       square[ 5 ].number = 5;
       square[ 5 ].owner = 0;
       square[ 5 ].price = 160;
       square[ 5 ].rent = 40;

       square[ 6 ].name = "Liverpool Street (Cost 120)";
       square[ 6 ].number = 6;
       square[ 6 ].owner = 0;
       square[ 6 ].price = 120;
       square[ 6 ].rent = 30;

       square[ 7 ].name = "Bank (Cost 90)";
       square[ 7 ].number = 7;
       square[ 7 ].owner = 0;
       square[ 7 ].price = 90;
       square[ 7 ].rent = 20;

       square[ 8 ].name = "St. Paul's (Cost 110)";
       square[ 8 ].number = 8;
       square[ 8 ].owner = 0;
       square[ 8 ].price = 110;
       square[ 8 ].rent = 35;

       square[ 9 ].name = "Chancery Lane (Cost 80)";
       square[ 9 ].number = 9;
       square[ 9 ].owner = 0;
       square[ 9 ].price = 80;
       square[ 9 ].rent = 15;

       square[ 10 ].name = "Holborn (Cost 60)";
       square[ 10 ].number = 10;
       square[ 10 ].owner = 0;
       square[ 10 ].price = 60;
       square[ 10 ].rent = 10;
	   
	   square[ 11 ].name = "Tottenham Court Road (Cost 60)";
       square[ 11 ].number = 10;
       square[ 11 ].owner = 0;
       square[ 11 ].price = 60;
       square[ 11 ].rent = 10;

	   square[ 12 ].name = "Oxford Circus (Cost 150)";
       square[ 12 ].number = 10;
       square[ 12 ].owner = 0;
       square[ 12 ].price = 60;
       square[ 12 ].rent = 10;
}

int dice( ) //roll 2 random dices
{
    int d1,d2,roll;

	d1 = rand()%6 + 1;
	d2 = rand()%6 + 1;
	roll = d1 + d2;

	cout<<"You rolled "<< d1 <<" and " << d2<<"." <<endl <<"You moved " << roll <<" spaces." << endl;
		
    return roll;
}

void func_pos()
{
    int choose;
    int x = me.position;

    cout<< "You are at "<< square[ x ].name <<'\n';

    if( me.position != 0 & square[ x ].owner == 0)
    {
        cout << "1.Buy\n";
        cout << "2.Don't buy\n";
		cin >> choose;

        switch(choose)
        {
            case 1:
                me.money -= square[ x ].price;
                square[ x ].owner = 1;
				cout<< "You've bought " << square[x].name <<endl;
				cout<< "You have " << me.money <<" left."<<endl;
				
            case 2:
                cout<< "End of your turn "<<endl<<endl;
        }
    }

    else if( me.position != 0 & square[ x ].owner != 0)
    {
        cout<< "You paid " << square[ x ].rent << " as rent\n";
    }


}


int main()
{
    declare();
	srand(time(NULL));

	
    do
    {
        int g;
        g = dice();

        me.position += g;

        if( me.position >= 10)
        {
            me.position -= 10;
            me.money += 300;
        }

        func_pos();

    } while( me.money != 500);


	return 0;
}
Last edited on

Question1:How to make the program terminate if player has 0 or - cash?


Program termination -
1
2
3
4
5
6
7
8
9
10
struct player {
    int money;
    int position;
};
if(someplayer.money <= 0)
{
    //In proper Monopoly, the properties should not be put up for auction 
   //among all other players. If only two players are playing/remaining 
   //the game is over 
}



Question2:How to make second player?


Just declare another instance of player. Add something in your code to keep track of who has the current turn.


Question3:How to make a double? When rolled double player should roll dices again , if rolled 3 times , send to jail.


1
2
3
4
5
6
7
8
9
10
11
 
do
{
    //The player roll 
    //Check if double, if not doublerolled = false. 
    ++nrolls; 
    if(nrolls == 3)
    {
    //Send player to jail. 
    }
}while(doublerolled == true); 

tried to add second player, but something not working out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
#include <ctime>
#include <string>
using namespace std;

void declare();
void func_pos();

struct places {
    string name;
	int price;
	int rent;
	int number;
	int owner;
};

struct player1 {
    int money;
    int position;
};

struct player2 {
    int money;
    int position;
};

player1 p1;
places square[ 13 ];

player2 p2;
places square[ 13 ];

void declare()
{
       p1.money = 350;
       p1.position = 0;
	   p2.money = 350;
       p2.position = 0;

       square[ 0 ].name = "Start";
       square[ 0 ].number = 0;
       square[ 0 ].owner = 0;
       square[ 0 ].price = 0;
       square[ 0 ].rent = 0;

       square[ 1 ].name = "Leytonstone (Cost 200)";
       square[ 1 ].number = 1;
       square[ 1 ].owner = 0;
       square[ 1 ].price = 200;
       square[ 1 ].rent = 50;

       square[ 2 ].name = "Leyton (Cost 160)";
       square[ 2 ].number = 2;
       square[ 2 ].owner = 0;
       square[ 2 ].price = 160;
       square[ 2 ].rent = 40;

       square[ 3 ].name = "Stratford (Cost 150)";
       square[ 3 ].number = 3;
       square[ 3 ].owner = 0;
       square[ 3 ].price = 150;
       square[ 3 ].rent = 35;

       square[ 4 ].name = "Mile End (Cost 80)";
       square[ 4 ].number = 4;
       square[ 4 ].owner = 0;
       square[ 4 ].price = 80;
       square[ 4 ].rent = 15;

       square[ 5 ].name = "Bethnal Green (Cost 160)";
       square[ 5 ].number = 5;
       square[ 5 ].owner = 0;
       square[ 5 ].price = 160;
       square[ 5 ].rent = 40;

       square[ 6 ].name = "Liverpool Street (Cost 120)";
       square[ 6 ].number = 6;
       square[ 6 ].owner = 0;
       square[ 6 ].price = 120;
       square[ 6 ].rent = 30;

       square[ 7 ].name = "Bank (Cost 90)";
       square[ 7 ].number = 7;
       square[ 7 ].owner = 0;
       square[ 7 ].price = 90;
       square[ 7 ].rent = 20;

       square[ 8 ].name = "St. Paul's (Cost 110)";
       square[ 8 ].number = 8;
       square[ 8 ].owner = 0;
       square[ 8 ].price = 110;
       square[ 8 ].rent = 35;

       square[ 9 ].name = "Chancery Lane (Cost 80)";
       square[ 9 ].number = 9;
       square[ 9 ].owner = 0;
       square[ 9 ].price = 80;
       square[ 9 ].rent = 15;

       square[ 10 ].name = "Holborn (Cost 60)";
       square[ 10 ].number = 10;
       square[ 10 ].owner = 0;
       square[ 10 ].price = 60;
       square[ 10 ].rent = 10;
	   
	   square[ 11 ].name = "Tottenham Court Road (Cost 60)";
       square[ 11 ].number = 10;
       square[ 11 ].owner = 0;
       square[ 11 ].price = 60;
       square[ 11 ].rent = 10;

	   square[ 12 ].name = "Oxford Circus (Cost 150)";
       square[ 12 ].number = 10;
       square[ 12 ].owner = 0;
       square[ 12 ].price = 60;
       square[ 12 ].rent = 10;
}

int dice( ) //roll 2 random dices
{
    int d1,d2,roll;

	d1 = rand()%6 + 1;
	d2 = rand()%6 + 1;
	roll = d1 + d2;

	cout<<"You rolled "<< d1 <<" and " << d2<<"." <<endl <<"You moved " << roll <<" spaces." << endl;
		
    return roll;
}

void func_pos()
{
    int choose;
    int x = p1.position;
	int x = p2.position;

    cout<< "You are at "<< square[ x ].name <<'\n';

    if( p1.position != 0 & square[ x ].owner == 0)
    {
        cout << "1.Buy\n";
        cout << "2.Don't buy\n";
		cin >> choose;

        switch(choose)
        {
            case 1:
                p1.money -= square[ x ].price;
                square[ x ].owner = 1;
				cout<< "You've bought " << square[x].name <<endl;
				cout<< "You have " << p1.money <<" left."<<endl;
				
            case 2:
                cout<< "End of your turn "<<endl<<endl;
        }
    }

    else if( p1.position != 0 & square[ x ].owner != 0)
    {
        cout<< "You paid " << square[ x ].rent << " as rent\n";
    }
	
	if( p2.position != 0 & square[ x ].owner == 0)
    {
        cout << "1.Buy\n";
        cout << "2.Don't buy\n";
		cin >> choose;

        switch(choose)
        {
            case 1:
                p2.money -= square[ x ].price;
                square[ x ].owner = 1;
				cout<< "You've bought " << square[x].name <<endl;
				cout<< "You have " << p2.money <<" left."<<endl;
				
            case 2:
                cout<< "End of your turn "<<endl<<endl;
        }
    }

    else if( p2.position != 0 & square[ x ].owner != 0)
    {
        cout<< "You paid " << square[ x ].rent << " as rent\n";
    }


}


int main()
{
    declare();
	srand(time(NULL));

	
    do
    {
        int g;
        g = dice();

        p1.position += g;
		p2.position += g;

        if( p1.position >= 14)
        {
            p1.position -= 14;
            p1.money += 200;
        }

		if( p2.position >= 14)
        {
            p2.position -= 14;
            p2.money += 200;
        }

        func_pos();

    } while( p1.money||p2.money == 0);


	return 0;
}
I actually think you will very much struggle to make this game using your method. Monopoly is quite a complex game to make early on. There are many rules and things to consider. If you are sure you want to make Monopoly? If so, then I will help you to do so, but only if you will give more descriptive posts about your problems and if you understand this will probably take several weeks. Although, it will be a very good excersize for you.

but something not working out.


You have declared an identical struct twice. Player 2 does not need a different structure. Player 2 will be another instance of the first struct.
1
2
3
4
5
6
7
struct player
{
//All player stuff
}; 

player player1; 
player player2; 


You also tried to declare your game board twice. Each player does not have their own board! You just need one game board that all player will then play on.

You need to include <cstdlib> to use rand() function.

Line 135 you declare int x and then declare int x again on line 136. You can't declare anything twice.
Last edited on
Topic archived. No new replies allowed.