Problem with using a switch()

So I am able to get my code working, but on void RollDiceAndMove(), I cannot seem to get my code to make a new position (line 104) and sending it back to the while loop (lines 54-55). If I put an actual number into switch() (let's say 100) it will switch outputs between 100 and 0.

I just can't seem to get the RollDiceAndMove code working correctly. Any Help would be greatly appreciated!

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

using namespace std;

void RollDiceAndMove(); //game rolls dice, and moves the player
void CheckChutes(); // game checks to see if player landed on chutes
void CheckLadders(); // game checks to see if player landed on ladders
void whoWon(); // game checks to see if anyone has won yet
void welcomeMessage(); // shows display message, and asks if they want to play
void goodbyeMessage(); // tells user goodbye after game

int RollDie;
int NewPos;
int NewPos1;




int main()
{
	bool player1Turn = 0; //switches between player1 and player2's turns
	string player1Name, player2Name; // gets names from both players
	int choice = 'y'; // answer when asked if players want to play
	int Player1Loc = 0, Player2Loc = 0; // location of the players
	int Playerpos;

	
	
welcomeMessage();
		
		cout << endl << endl;
		cout << "Enter your names: " << endl << endl;
		cout << "Player1: ";
		cin >> player1Name;
		cout << endl << endl;
		cout << "Player2: ";
		cin >> player2Name;



while(choice == 'y'){
		while(Player1Loc != 100 && Player2Loc != 100){
				if(player1Turn){
					cout << endl << endl;
					cout << player1Name << ": Its your turn." << endl << endl;
					cout << "Press Enter to Start Turn: ";
					cin.get();
					cout << endl << endl;
					RollDiceAndMove();
					cout << "New Position Amount: " << NewPos1 << endl;
					//CheckChutes();
					//CheckLadders();
					player1Turn = false;
				}
				else{
					cout << endl << endl;
					cout << player2Name << ": Its your turn." << endl << endl;
					cout << "Press Enter to Start Turn: ";
					cin.get();
					cout << endl;
					RollDiceAndMove();
					cout << "New Position Amount: " << NewPos << endl;
					// CheckChutes();
					// CheckLadders();
					player1Turn = true;
				}
			}
		while (NewPos == 100) {
			whoWon();
		}
}


	return 0;
}

void welcomeMessage()
{
	int choice;
	// shows welcome message, and asks if they want to play
    // IN: choice
	cout << "Welcome to the chutes and ladders game. Both " << endl;
	cout << "players start at 0, and the first one to 100 " << endl;
	cout << "wins the game. However, if you land on a chute," << endl;
	cout << "your player will move down, but a ladder " << endl;
	cout << "will move you up." << endl;
	
}

void goodbyeMessage()
{
	// shows the goodbye message
	cout << "Thanks for playing!" << endl;
}

void RollDiceAndMove ()
{
	// rolls dice, and moves the player
	switch(NewPos = *I don't know what to put here* )
	{
		for (int Player1pos = 0; Player1pos < 100; Player1pos++)
		{
			RollDie = rand() % 6 + 1; 
			Player1pos = 1;
			NewPos1 = Player1pos + RollDie;
			CheckChutes();
			CheckLadders();
			cout << "Your new location is: " << NewPos << endl;
			break;
		}
		for (int Player2pos = 0; Player2pos < 100; Player2pos++)
		{
			RollDie = rand() % 6 + 1;
			Player2pos = 1;
			NewPos = Player2pos + RollDie;
			CheckChutes();
			CheckLadders();
			cout << "Your new location is: " << NewPos << endl;
			break;
		}
	}
}
void whoWon()
{
	int NewPos;
	// determines if user has won, when their location is at 100
	RollDiceAndMove();
	if (NewPos == 100){
		cout << "You won!" << endl;
	}
}
void CheckChutes ()
{
	
	int NewPos;
	//checks if chutes, if yes, then moves player backwards
	if (NewPos == 98)
		NewPos = 78;
	else if (NewPos == 95)
		NewPos = 75;
	else if (NewPos == 93)
		NewPos = 70;
	else if (NewPos == 87)
		NewPos = 24;
	else if (NewPos == 64)
		NewPos = 60;
	else if (NewPos == 62)
		NewPos = 19;
	else if (NewPos == 56)
		NewPos = 53;
	else if (NewPos == 49)
		NewPos = 11;
	else if (NewPos == 48)
		NewPos = 26;
	else if (NewPos == 16)
		NewPos = 6;
	else
		NewPos = NewPos;
	cout << "You landed on chutes, and have to move down" << endl;
}

void CheckLadders ()
{
	int NewPos;
	// checks if ladders. if yes, moves player forwards
	if (NewPos == 1)
		NewPos = 38;
	else if (NewPos == 4)
		NewPos = 14;
	else if (NewPos == 9)
		NewPos = 21;
	else if (NewPos == 23)
		NewPos = 44;
	else if (NewPos == 28)
		NewPos = 84;
	else if (NewPos == 36)
		NewPos = 44;
	else if (NewPos == 51)
		NewPos = 66;
	else if (NewPos == 71)
		NewPos = 90;
	else if (NewPos == 80)
		NewPos = 100;
	else
		NewPos = NewPos;
	cout << "You landed on ladders, and get to move up the board!" << endl;
} 
Last edited on
I don't know what exactly you're trying to do on line 104?

You cannot use switch() for a range of number (use if() for that).
Use switch() for single numbers like in CheckChutes ()/CheckLadders ()
Hi,

Some thoughts:

line 10 : Functions should only do 1 conceptual thing, consider splitting this function into 2 functions that return values.

Lines 17 to 19: Avoid global variables, send values to functions that need them.

lines 36 to 42: this should also be in a function.

Line 46: Doesn't achieve anything, you don't change the value of the variable in the condition.

Line 47: I have a pathological hatred for conditions like these :+) Consider having a bool variable GameWon and use that in the condition.

Line 49 to 58 and line 61 to 71: These 2 snippets are almost identical, can you figure out how to make them 1 function? They are also exactly the same as the RollDiceAndMove function, so the same logic exists 4 times in the code. Whenever you have identical code there is always a better way.

Line 73: you have doubled up on your logic here, the win condition is looked at here and in the WhoWon function. WhoWon also calls RollDiceAndMove which is also called on lines 54 and 65. So hence the idea of 1 function 1 action. The WhoWon function should return a bool : the state of the GameWon variable.

With the CheckChutes and CheckLadders functions, consider having an array of pairs of numbers (or 2 parallel arrays ). The first number is CurentPosition, the second is the NewPosition. Write a function that finds the CurrentPosition and returns the NewPosition using a for loop.

Edit: Or use a std::vector<std::pair>

Good Luck !!
Last edited on
Topic archived. No new replies allowed.