Value Swapping from Function Returns

Can get names to swap values with case 7 and 11.

Building a roll-and-move style boardgame for a final project and the stipulation for case 7 and 11 has you swap positions with highest/lowest scorer. I've got the code to run decently but the swapping doesn't seem to work. I feel like it has something to do with the sorting but, to be honest, my head is all mushy and I can't see it. Partial code (2 players) included.
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <iostream>
#include <string>
#include<ctime>
#include<cstdlib>
using namespace std;

const int board = 50;
int numberOfPlayers,
dieOne,
dieTwo,
diceTotal;

int scoreMax,
scoreMin,
currentplayer = 0,
playerOne,
playerTwo,
playerThree,
playerFour;
string currentPlayerName,
highestScore,
lowestScore,
playerOneName,
playerTwoName,
playerThreeName,
playerFourName,
playerNames[] = { playerOneName, playerTwoName, playerThreeName, playerFourName };
int playerTotal[4];





//Random number generator
class diceRoll {
public:


	void roll_di(int& a) {

		a = (rand() % 6) + 1;

	}
};
//Simplified roll command (standard).
int Roll(string, int) {
	string repeat;
	cout << "\n" << currentPlayerName << ": Press 'enter' to roll the dice!";
	getline(cin, repeat);
	cout << endl;
	srand((unsigned)time(0));
	diceRoll rolling;
	rolling.roll_di(dieOne);
	rolling.roll_di(dieTwo);
	diceTotal = dieOne + dieTwo;
	cout << currentPlayerName << ", you rolled " << dieOne << " and " << dieTwo << " for a total of " << diceTotal;

	// Score modifier switch/case.
	switch (diceTotal) {
	case 4:
		(currentplayer >= 1);
		currentplayer -= 1;
		cout << "\n You lose 1 point!";
		break;
	case 2:
		currentplayer += diceTotal;
		cout << "\nYou gain two points!";
		break;
	case 3:

	case 5:

	case 6:

	case 8:

	case 9:

	case 10:
		if
			((currentplayer + diceTotal) < 51) {
			currentplayer += diceTotal;
			cout << "\nYou gain " << diceTotal << " points!" << endl;
		}
		else {
			currentplayer = currentplayer;
		}
		break;

	case 7:
		currentplayer = -1;
		break;

	case 11:
		currentplayer = -2;
		break;

	case 12:
		currentplayer = 0;
		break;
	}
	getline(cin, repeat);
	return currentplayer;
}
//Simplified roll command (player value = 0, must roll doubles to leave "start")
int startRoll(string, int) {
	string repeat;
	cout << "\n\n" << currentPlayerName << ": Press 'enter' to roll the dice!";
	getline(cin, repeat);
	cout << endl;
	srand((unsigned)time(0));
	diceRoll rolling;
	rolling.roll_di(dieOne);
	rolling.roll_di(dieTwo);
	if (dieOne == dieTwo) {
		cout << "Congratulations, " << currentPlayerName << " you rolled double " << dieOne << "s!! You are now on the board!\n";
		getline(cin, repeat);
		cout << endl;
		currentplayer = 1;
	}
	// Allow for leaving start on 7.
	else if (dieOne + dieTwo == 7) {
		currentplayer = -1;
	}
	else {
		currentplayer = 0;
		cout << "You rolled a " << dieOne << "and a " << dieTwo << ".  You are still in Start, roll doubles to leave start!" << endl;
	}
	return currentplayer;
}

int main()
{
	//I feel like this should go closer to the sorter, but I can't get it to work anywhere else.
	scoreMax = playerTotal[0];
	highestScore = playerNames[0];
	scoreMin = playerTotal[0];
	lowestScore = playerNames[0];

	//Player quantity and arbitrary names.
	cout << "How many players, between 2 and 4, will there be?";
	cin >> numberOfPlayers;
	cout << endl;
	cout << "Please enter the name of Player 1: ";
	cin >> playerOneName;
	playerOne = 0;
	cout << endl;
	cout << "Please enter the name of Player 2: ";
	cin >> playerTwoName;
	playerTwo = 0;
	cout << endl;
	if (numberOfPlayers >= 3) {
		cout << "Please enter the name of Player 3: ";
		cin >> playerThreeName;
		playerThree = 0;
		cout << endl;
		if (numberOfPlayers == 4) {
			cout << "Please enter the name of Player 4: ";
			cin >> playerFourName;
			playerFour = 0;
			cout << endl;
			if (numberOfPlayers >4) {
				cout << "Please enter number of players, 2-4 ";
				cin >> numberOfPlayers;
				cout << endl;
			}
		}
	}
	// Process.  Run through the cycle of turns while the winning qualifications (fifty pts) is not yet met.
	do {
		//Initializing the function variables to modify the scores correctly.
		currentplayer = playerOne;
		currentPlayerName = playerOneName;
		//Separation quantifier for start position (must roll doubles to leave start).
		if (playerOne> 0) {
			Roll(playerOneName, playerOne);
		}
		//Start position routine.
		else {
			startRoll(playerOneName, playerOne);
		}
		//Qualifier for Case 7, allows for swapping of highest-point player with start-position player (or any lower player).
		if (currentplayer == -1) {
			for (int i = 0; i < 4; i++)
			{
				if (playerTotal[i] > scoreMax)
				{
					scoreMax = playerTotal[i];
					highestScore = playerNames[i];
					playerOne = scoreMax;
					playerTotal[i] = currentplayer;
					currentplayer = playerOne;
				}
			}
			cout << "You have swapped places with " << highestScore << " in the lead!" << endl;
		}
		//Qualifier for Case 11, forces swap to lowest-position/star position player.
		else if (currentplayer == -2) {
			for (int i = 0; i < 4; i++)
			{
				if (playerTotal[i] < scoreMin)
				{
					scoreMin = playerTotal[i];
					lowestScore = playerNames[i];
					playerOne = scoreMin;
					playerTotal[i] = currentplayer;
					currentplayer = playerOne;
				}
			}
			cout << "You have swapped places with " << lowestScore << "in the rear!" << endl;
		}
		//Score print.
		else {
			playerOne = currentplayer;
		}
		cout << "\nYour score is now " << playerOne << endl;

		//Re-initialize moving variable (zero almost certainly unnecessary).  Follow through similar steps as playerOne.
		currentplayer = playerTwo;
		currentPlayerName = playerTwoName;
		if (playerTwo > 0) {
			Roll(currentPlayerName, currentplayer);
		}
		else {
			startRoll(currentPlayerName, currentplayer);
		}
		//Qualifier for Case 7, allows for swapping of highest-point player with start-position player (or any lower player).
		if (currentplayer == -1) {
			for (int i = 0; i < 4; i++)
			{
				if (playerTotal[i] > scoreMax)
				{
					scoreMax = playerTotal[i];
					highestScore = playerNames[i];
					playerTwo = scoreMax;
					playerTotal[i] = currentplayer;
					currentplayer = playerTwo;
				}
			}
			cout << "You have swapped places with " << highestScore << " in the lead!" << endl;
		}
		//Qualifier for Case 11, forces swap to lowest-position/star position player.
		else if (currentplayer == -2) {
			for (int i = 0; i < 4; i++)
			{
				if (playerTotal[i] < scoreMin)
				{
					scoreMin = playerTotal[i];
					lowestScore = playerNames[i];
					playerTwo = scoreMin;
					playerTotal[i] = currentplayer;
					currentplayer = playerTwo;
				}
			}
			cout << "You have swapped places with " << lowestScore << "in the rear!" << endl;
		}
		else {
			playerTwo = currentplayer;
		}
		cout << "\nYour score is now " << playerTwo << endl;


	//Winning criteria.  No winning announcement yet, still working on skeleton.
	while (playerOne < board && playerTwo < board && playerThree < board && playerFour < board);

	return 0;
}
Last edited on
Topic archived. No new replies allowed.