Game code issues

i have two problems
1. the program stops after i ask for the player to enter a name to be associated with their piece.
2. i need to have a function that will find the player that is in the lead. the current player will take the place of the lead player and the lead player will go back home (or 0 in this case). does any one have any ideas.
this is the game Sorry!
thank you for any help

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
  #include <iostream>
#include <string>
#include <time.h>
#include <cstdlib>
using namespace std;

void move(int, int, bool);	//moving the player
int dice();					//rolling dice function
bool startGame(int);

enum PlayerColor { yellow, blue, red, green };

struct Player 
{
	string name;
	PlayerColor color;
};

int board[50];
Player members[4];

int main()
{
	bool endGame = false;
	
	int currentPlayers = 1;
	int numOfPlayers;			//store the number of players

	Player p0;
	Player p1;
	Player p2;
	Player p3;


	cout << "Select Players 1 - 4: ";
	cin >> numOfPlayers;

	if (numOfPlayers == 2)			//determing how many players there will be 
	{			              // and saving them to the array
		members[0] = p0;   //along with gathering names for the players.
		members[1] = p1;

		cin.ignore();
		cout << "P1 Name: ";
		getline(cin, p0.name);
		cout << "P2 Name: ";
		getline(cin, p1.name);

	}
	if (numOfPlayers == 3)
	{
		members[0] = p0;
		members[1] = p1;
		members[2] = p2;

		cin.ignore();
		cout << "P1 Name: ";
		getline(cin, p0.name);
		cout << "P2 Name: ";
		getline(cin, p1.name);
		cout << "P3 Name: ";
		getline(cin, p2.name);
	}
	if (numOfPlayers == 4)
	{
		members[0] = p0;
		members[1] = p1;
		members[2] = p2;
		members[3] = p3;

		cin.ignore();
		cout << "P1 Name: ";
		getline(cin, p0.name);
		cout << "P2 Name: ";
		getline(cin, p1.name);
		cout << "P3 Name: ";
		getline(cin, p2.name);
		cout << "P4 Name: ";
		getline(cin, p3.name);
	}
	if (numOfPlayers == 1 || numOfPlayers > 4)
	{
		cout << "There must be atleast 2 players and no more then 4 players";	//checking for correct number of players
		system("pause");
	}

	
	do
	{
		int roll1 = dice();				//rolling dice
		int roll2 = dice();
		bool doubles = false;
		int total = roll1 + roll2;		//save the value of the dice
		if (roll1 == roll2)				//test for a double
		{
			doubles = true;				//if true

			int roll1 = dice();			//dice will be rolled again
			int roll2 = dice();

			total = total + roll1 + roll2;	//and the sum of the second roll will be added to the first roll
		}
		

		move(currentPlayers, total, doubles);

		if (currentPlayers > numOfPlayers)	//determining when its player 1 turn
		{
			currentPlayers = 1;
		}
		endGame = true;
	} while (!endGame);
}

void move(int currentPlayer, int total, bool doubles)
{
	int currentPosition = 0;
	for (int i = 0; i < 50; i++)
	{
		if (board[i] == currentPlayer)
		{
			currentPosition = i;
		}
	}

	int movePosition = currentPosition + total;
	int positionOccupy = board[movePosition];

	switch (total)
	{
	case 2: if (currentPosition == 0)	//checking if player is on board and if not
	{									//then the current position will be changed to 1
		currentPosition = 1;
	}
			if (currentPosition > 0)	//if they are on the board then player will move two. 
			{
				total = 2;
			}
			break;
	case 3:
	case 5:
	case 6:
	case 8:
	case 9:
	case 10:
		break;
	case 4: currentPosition = currentPosition - 1;  //send player back one
		break;
	case 7:
		break;
	case 11: 
		break;
	case 12: board[currentPosition] = 0;			//send players current position to zero. 
	}

}

bool startGame(int currentPlayer)		//checks the board for the player
{
	bool find = true;
	for (int i = 0; i < 50; i++)
	{
		if (board[i] == currentPlayer)
		{
			find = false;
		}
	}
	return find;
}

int dice()
{
	int value;
	const int sides = 6;
	const int min = 1;

	value = (rand() % (sides - min + 1)) + min;

	return value;
}
Hello!

A few questions: why do you initialize your array members separately and then manually add them to the array?

I would personally recommend using a vector which you can push and pop from dynamically. Do a little research on this and you will find it very useful for game programming! Also, remember that vectors reallocate memory thus invalidating pointers and references if the reserved block is not big enough to hold a new member, so I'd recommend using the .reserve(number) command to reserve a space for the maximum number of players you anticipate in your program.

I don't know if this is the best way, but you could find the player with the highest score by simply looping through like this:

1
2
3
4
5
6
7
8
/* Notes: I have am using a variable called sizeOfMembers to denote the current number of players stored in the members array. I think you use numOfPlayers or something instead though.
Also, I am assuming that there is a "score" variable stored in the member class.
*/

int highestscore=0;
for(int i = 0; i < sizeOfMembers; i++) {
  if(members[i].score > highestscore) {highestscore=members[i].score;}
}


Also, cin pauses the program while waiting for user input. If you want the program to continue running while the user inputs something, you will need to use an asynchronous input function of some sort. Describing what you'd like to do specifically would help us further guide you here!
Last edited on
i initialized the members separately in the array because i wanted the user to choose the number of players that will be involved. the way i did it was probably not the most effective way, but it was the way my teacher had showed us.

i did not have a high score variable but that does make sense to have that and would be a good way to keep track on were players are on the board.
thank you for the guidance!
Topic archived. No new replies allowed.