Hey, I'm writing a wheel of fortune game and trying to figure out how to make the game update what players turn it is. It is assumed that there is only three players, and the variable "int players = 1" is defined globally, so if I change it in the main function, or other functions, it should stay updated. The thing to remember is that i'm only trying to make the player update when the user spins the wheel and either guesses the letter incorrectly or he spins lose turn or bankrupt. Ignore any other part of code such as choosing vowels or solving the puzzle. Here are the parts of the functions that i'm using.

int main ()
{
	 string phrase;			// to store the phrase array
	 string puzzle;			// to store the puzzle * array
	 
	 int score[NUM_PLAYERS] = {0, 0, 0}; // to hold the scores of each player
	 int count;							 // for "for" functions
	 bool winner = false;				 // to hold winner value
	 unsigned int seed;					 // for srand()
	 int highest;						 // to hold highest score

	 seed = static_cast<unsigned>(time (NULL));
	 srand (seed);						 // seeding random number generation

initialize_arrays(phrase, puzzle); //opens the initialize_arrays function

	while (!winner)						 // while the winner hasn't been found
	winner = (player_turn(players, score, phrase, puzzle));
	// ^^ calls player_turn function and sets its value to winner
	if (winner)
	{
		// sets highest to the highest score and finds what players score that is
		highest = score[0];
		for (count = 1; count < NUM_PLAYERS; count++)
		{
			if (score[count] > highest)
			{
				highest = score[count];
				players ++;
			}
		}
	// prints final notes after the game has ended
	cout << "Congradulations player " << players << " you have won this round!" << endl;
	cout << "Your total score is " << highest << endl << endl;
	print_score(players, score);
	cout << "Thanks for playing Wheel of Fortune!!!" << endl;
	cout << "Program Execution Terminated." << endl;
	}
	return (0);
}

That is the main function, and the function I am calling is the player_turn, which is:

bool player_turn(int players, int score[], string phrase,
string& puzzle)
{
bool spin_truth = false;
bool solved = false;
char choice;					// to hold players choice
print_score(players, score);	// print_score function


cout << endl << "Player " << players << " it's your turn." << endl << endl;

print_puzzle(puzzle);

cout << "Player " << players << " what would you like to do?" << endl << endl;
cout << "(s)pin, s(o)lve or (b)uy a vowel -> ";
cin >> choice;

	while ((choice != 's') && (choice !='o') && (choice!= 'b'))
	{
	cout << "you have entered an incorrect choice, please choose again -> ";
	cin  >> choice;
	}

if (choice == 's')
{
	spin_truth = spin(players, score, phrase, puzzle);

	if (spin_truth == false)
	{
		if (players == 3)
			players = 1;
		else if (players == 1)
			players = 2;
		else
			players = 3;
	}
}
else if (choice == 'o')
	solved = (solve(players, score, phrase, puzzle));

else
	buy_vowel(players, score, phrase, puzzle);

return(solved);
}

And player_turn calls the spin function, which is:

bool spin(int players, int score[], string phrase, string&
puzzle)
{
int value;
bool turn = false;
char choice;
int count = 0;
int length;
int letter = 0;			// to hold the amout of correct letters guessed

	length = phrase.length();

	cout << "You chose to spin the wheel!" << endl << endl;

value = spin_wheel(WHEEL, WHEEL_SIZE);

if (value == BANKRUPT)
{
	score[(players - 1)] = 0;
	turn = false;
	cout << "You went bankrupt!" << endl << endl << endl;
}
else if (value == LOSE_TURN)
{
	turn = false;
	cout << "You lose a turn!" << endl << endl << endl;
}
else
{
	cout << "You spun " << value << endl << endl;
	cout << "Pick a letter -> ";
	cin >> choice;
	
	while ((choice == 'a')||(choice == 'e') || (choice == 'i') || (choice == 'o') || (choice == 'u'))
	{
		cout << "You've entered a vowel! Please enter another letter -> ";
		cin >> choice;
		cout << endl;
	}

	for (count; count < length; count++)	
	{
		if (choice == phrase[count])
		{
			puzzle[count] = choice;
			score[(players - 1)] += value;
			letter ++;
		}
	}
	if (letter > 0)
	{
	cout << "Congratulations player " << players << "! There are " <<  letter;
	cout << " " << choice << "'s in the puzzle." << endl << endl << endl;
	cout << "Your current score is " << score[(players - 1)] << endl;
	cout << "Your total score is " << score[(players - 1)] << endl;
	turn = true;
	}
	else
	{
		cout << "That letter does not exist, your turn is over" << endl;
		turn = false;
}
}

return (turn);
}

As a side note, all of the functions that I haven't provided code for change the variable "players" in any way such. When I use the debugger, the part of the player_turn function does change the variable "players", but as soon as it goes back to the main function it gets reset to "1". How can I fix this?

Recommended Answers

All 5 Replies

If players is global, why are you passing it around? I believe you are not changing the global variable but the local variable passed in by value.

Florida State University Honor Code Violations and use of Internet

The teaching staff has encountered multiple posts on this web site
asking questions about how to do project 5 in this course. Some students
have posted their program source code on this web site. Asking for help
is a major reason the course teaching staff is provided to you.
We have a course-specificonline help email group, 21 teaching assistant
office hours a week, and 10 recitations a week, for the purpose of
providing help. There is no valid reason to post such questions to
strangers on the internet.

IMPORTANT:

(1) When you post your program source code on the internet, you ARE
violating the FSU honor code.

(3) Note that many people on these discussions boards are professionals
and find cheating offensive. We have had honor code cases where
students were turned in by strangers on the internet. Those students
were found guilty of violating the FSU honor code and hence received
an "F" grade in the course.

(4) If another student finds your code posted on a web site and turns it in,
and you also turn it in, you are BOTH guilty of violating the FSU honor
code and both students will receive an "F" in the course. Remember that
automated program plagiarism detection will quickly detect similar code
submissions. It is VERY easy to find your code posted on the internet, hence
you are risking an "F" grade in this course.

Professor Tyson

commented: Yeah.. -3

Hello Professor Tyson,

That's a great rule you have there and congratulations: you've found some of your students asking for help on the internet :|

Note that many people on these discussions boards are professionals
and find cheating offensive

Yes we do. This poster however showed that (s)he put in some genuine effort in solving this problem. (S)he is asking for help on a small bug that's in the program. This is not cheating in my opinion. This is asking for help because they probably didn't get it from the people that are responsible for their education (yes, I am talking about you). If you decide to fail this student for "cheating" because they asked for help on a glitch, then that's pathetic.

There is no valid reason to post such questions to
strangers on the internet.

Did it ever occur to you that "strangers on the internet" might actually be excellent programmers in "real life"? I've seen teachers surfing Daniweb looking to pick up new tip and tricks.

Now I've seen you post this same post all over Daniweb. At Daniweb we also have rules. We like to have on-topic replies that help future visitors (as well as the OP) of the thread with their issues (no hijacking). We also have a rule for flooding the boards with the same question (or rant in this case). So I've deleted all your other replies with the exception of this one.

commented: Stick it to him Nick! Grrr........ :):P +4

(1) When you post your program source code on the internet, you ARE
violating the FSU honor code.

I am both in industry and a college student. My school has a "Truth in Learning" policy that's similar to your "Honor Code". DaniWeb's forum rules are written and enforced in such a way as to ensure compatibility with any such policies. While it is true that the industry professionals that frequent these kinds of sites are less than friendly to cheaters, they are more than amicable, and receptive, to those that show a genuine interest in the subject and are in fact willing and trying to learn.

That being said, according to your logic, this student should fail for using any one of the ONLINE tools that your school has provided to them. How's that for a Catch 22? "Here's all this cool stuff for you to use, but don't use it or you'll fail because you used it."

What you have posted is an incomplete, and inaccurate, interpretation of the intent of such rules. When you post code with THE INTENT of giving it to others to use that is cheating. However, the OP's intent is to request assistance with an issue they're having. Thus it does NOT constitute cheating. This is networking. Last I checked, that's something generally encouraged by reputable institutions.

To keep this On-Topic:
As WaltP mentioned, you are not using your global correctly. Since it's global, it does not require passing, it's automatically in-scope. Because you pass it, and use an identical local identifier, the global version of it is "masked" from view. Thus, when you think you are accessing your global, you are actually accessing a local copy of it that has no effect on the global version.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.