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?