help with board game program

i am stuck on how to make this program work. its a two player board game and certain spots means certain things (draw a card, spin the wheel, etc.). any help would be greatly appreciated. thanks

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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
using namespace std;

void roll_the_dice(int& spot);
void choose_card_or_wheel(int& spot,int& card_num);
void spin_the_wheel(int& spot,int& pos);
void pick_a_card(int& spot,int& pos);
void check_the_location(int player, int& spot, int& card_num);

int c=-1;

int main()
{
cout<<endl;
cout<<"          This is a super fun board game"<<endl<<endl<<endl;
cout<<" 2 player game - first to space 90 is the winner"<<endl<<endl;
int move, pos1=1, pos2=1, end1, end2, endf=0, spot,player1=1,player2=2;
int card_num;//cards

while (endf==0){
end1=0;
end2=0;
while(end1 == 0)
  {
cout <<" PLAYER # 1"<<endl;
		roll_the_dice(move);
        end1=1;
        cout<<" dice = "<<move<<endl;
        pos1=pos1+move;
        cout<<" position = "<<pos1<<endl<<endl<<endl;
		check_the_location(player1, spot, card_num);
		 end1=1;
		
}

 while(end2 == 0)
  {
	  cout <<" PLAYER # 2"<<endl;
		roll_the_dice(move);
        end1=1;
        cout<<" dice = "<<move<<endl;
        pos2=pos2+move;
        cout<<" position = "<<pos2<<endl<<endl<<endl;
		check_the_location(player2, spot, card_num);
		end2=1;
		
 }

 }
cout<<endl;
 return (0);
}

void check_the_location(int player, int& spot, int& card_num)
{
	int move, pos;

	if (pos = 10){
			pick_a_card(move, pos);
			pos=pos+move;}
		else if (pos = 30){
			pos = 70;}
		else if (pos = 40){
			spin_the_wheel(move,pos);
			pos=pos+move;}
		else if (pos = 60){
			pick_a_card(move, pos);
			pos=pos+move;}
		else if (pos = 70){
			spin_the_wheel(move,pos);
			pos=pos+move;}
		else if (pos = 80){
			choose_card_or_wheel(move, card_num);
			pos=pos+move;}
		else if (pos >= 90)
			{
    cout<<" P L A Y E R   #"<< player <<" I S   T H E   W I N N E R!!!"<<endl;
    }
		
}

void roll_the_dice(int& spot)
{
int x;
srand(time(0));
x=rand();
spot=(x%6)+1;

}

void choose_card_or_wheel(int& spot,int& card_num)
{
	int choice, move, pos, end1;

	cout <<" For wheel - enter 1"<<endl;
	cout <<" For cards - enter 2"<<endl<<endl;
	cin>>choice;
	switch (choice)
    {
    case 1: cout <<" You have chosen the wheel!"<<endl;
        spin_the_wheel(move,pos);
        end1=1;
        pos=pos+move;
        cout<<" wheel = "<<move<<endl;
        cout<<" position = "<<pos<<endl<<endl<<endl;
        win(pos,1);
            break;
    case 2: cout <<" You have chosen the cards!"<<endl;
        pick_a_card(move,pos);
        end1=card_num;
        cout<<" card = "<<move<<endl;
        pos=pos+move;
        cout<<" position = "<<pos<<endl<<endl<<endl;
        win(pos,1);
            break;
    default:cout <<" only inputs excepted are 1 or 2"<<endl;
    }
}

void spin_the_wheel(int& spot, int& pos)
{
int x,y;
srand(time(0));
x=rand();
y=(x%8)+1;
switch(y)
    {
    case 1: cout<<"   go to the beginning"<<endl;
    pos=pos-pos;
    spot=0;
        break;
    case 2: cout<<"   go back 8 spaces"<<endl;
    spot=-8;
        break;
    case 3: cout<<"   go back 7 spaces"<<endl;
    spot=-7;
        break;
    case 4: cout<<"   go forward 1 space"<<endl;
    spot=1;
        break;
    case 5: cout<<"   go back 4 spaces"<<endl;
    spot=-4;
        break;
    case 6: cout<<"   go forward 2 spaces"<<endl;
    spot=2;
        break;
    case 7: cout<<"   go back 6 spaces"<<endl;
    spot=-6;
        break;
    case 8: cout<<"   go back 9 spaces"<<endl;
    spot=-9;
        break;
    }
}

void pick_a_card(int& spot, int& pos)
{
switch(c)
    {
    case 0: cout<<"go back 9 spaces"<<endl;
    spot=-9;
        break;
    case 1: cout<<"go back to the beginning"<<endl;
    spot=0;
    pos=pos-pos;
        break;
    case 2: cout<<"go back 3 spaces"<<endl;
    spot=-3;
        break;
    case 3: cout<<"go back 8 spaces"<<endl;
    spot=-8;
        break;
    case 4: cout<<"go forward 2 spaces"<<endl;
    spot=2;
        break;
    case 5: cout<<"go forward 1 space"<<endl;
    spot=1;
        break;
    case 6: cout<<"go forward 3 spaces"<<endl;
    spot=3;
        break;
    case 7: cout<<"go back to the beginning"<<endl;
    spot=0;
	pos=pos-pos;
        break;
    case 8: cout<<"go back 4 spaces"<<endl;
    spot=-4;
        break;
    case 9: cout<<"go forwards 6 spaces"<<endl;
    spot=6;
        break;
    default: cout << "   no more cards left "<<endl;
    spot=0;
   
    }
}


Last edited on
OK, here's the deal - Edit your post so it uses code tags - the <> button on the right, and post your compiler output. If you do those things then we can help.

Hope all goes well - I look forward to your reply.
It compiles fine with a couple of warnings about the srand of time lose of data . But when the console runs its like it has an infinite loop and never ends.
while(end1 == 0)

The variable end1 is not initialised. Same with end2 on line 39. Just wondering if these should be if statements instead of while's?

You also need to fix up your indenting to make you intention clearer. At the moment while(end1 == 0) and while(end2 == 0) are inside the while (endf==0){ Is that what you want, or should they be if statements?

Is the variable endf changed anywhere to prevent the infinite looping?

Have you considered using bool variables for these instead of ints? You can use true & false then.

This:

pos1=pos1+move;

can be abbreviated to :

pos1 += move;

With this part:

1
2
3
4
5
6
7
8
9
10
11
12
13
void choose_card_or_wheel(int& spot,int& card_num)
{
	int choice, move, pos, end1;

	cout <<" For wheel - enter 1"<<endl;
	cout <<" For cards - enter 2"<<endl<<endl;
	cin>>choice;
	switch (choice)
    {
    case 1: cout <<" You have chosen the wheel!"<<endl;
        spin_the_wheel(move,pos);
        end1=1;
        pos=pos+move;


The variables move, pos are not initialised, but you use them in the function call spin_the_wheel Did your compiler not pick that up?

end1 is not the same as the end1 in other parts of the program because it is local to this function. That doesn't mean you should make it global.

With function names, I would use SelCardOrWheel or ChooseCardWheel, underscores make the names too long. Comment the name of the function so that readers know that it means Select Card Or Wheel. With names, you can abbreviate a bit, as long as it is not too confusing - I try to keep names to 10 chars long if possible.

On line 117:

win(pos,1);

Where is the declaration / definition of the function win? That is an error, but you say you had no errors?

On line 187:

pos=pos-pos;

A typo I am sure.

Hope this all helps


Topic archived. No new replies allowed.