Code:
#define PHRASE_SIZE 81
#define NUM_PLAYERS 3
#define BANKRUPT 0
#define LOSE_TURN -1
#define VOWEL_COST 250
/***************************************/
/* to_upper */
/***************************************/
char to_upper(char aLtr)
{
char ltr = aLtr;
if (ltr >= 'a' && ltr <= 'z')
{
ltr = (char)((int)ltr - 32);
}
return ltr;
}
/***************************************/
/* spin_wheel */
/***************************************/
int spin_wheel()
{
int wheel[WHEEL_SIZE] = {50, 100, 150, 200, 250, 300, 350, 400, 450,
500, 1000, 1500, 2500, BANKRUPT, LOSE_TURN};
int spin = (int)(((float)rand() / RAND_MAX) * WHEEL_SIZE);
return wheel[spin];
}
/***************************************/
/* isvowel */
/***************************************/
int isvowel(char guess)
{
int istrue = 0;
if (guess == 'a' || guess == 'A' ||
guess == 'e' || guess == 'E' ||
guess == 'i' || guess == 'I' ||
guess == 'o' || guess == 'O' ||
guess == 'u' || guess == 'U')
{
istrue = 1;
}
return istrue;
}
/***************************************/
/* print_puzzle */
/***************************************/
void print_puzzle (int size, char puzzle[])
{
int i;
printf("===============================\n");
printf(" Puzzle\n");
printf("===============================\n\n");
for (i = 0; i < size; ++i)
{
printf("%c",puzzle[i]);
}
printf("\n\n===============================\n\n");
}
/***************************************/
/* print_score */
/***************************************/
void print_score(int players, int score[])
{
int p;
printf("\n");
for (p = 0; p < players; ++p)
{
printf("Player %d ", p+1);
}
printf("\n");
for (p = 0; p < players; ++p)
{
printf("======== ");
}
printf("\n");
for (p = 0; p < players; ++p)
{
printf(" %d ", abs(score[p]));
}
printf("\n\n");
}
/***************************************/
/* spin */
/***************************************/
int spin(int player, int scores[ ], char phrase[ ], char puzzle[ ], int size)
{
int player_continues = 1;
int spin_value;
printf("You chose to spin the wheel!\n\n");
spin_value = spin_wheel();
if (spin_value == -1) /* lose turn */
{
printf("Sorry Player %d, you spun LOSE A TURN\n", player+1);
player_continues = 0;
}
else if (spin_value == 0) /* bankrupt */
{
printf("Sorry Player %d, you spun BANKRUPT\n", player+1);
scores[player] = 0;
player_continues = 0;
}
else
{
char ltr = ' ';
int i;
int cnt = 0;
printf("You spun %d\n\n", spin_value);
/* Get the consonant */
while (ltr < 'B' || ltr > 'Z')
{
printf("Enter a letter ->");
scanf("%c", <r);
printf("\n\n");
ltr = to_upper(ltr);
if (isvowel(ltr))
{
printf("Incorrect input. You cannot pick a vowel when you've chosen to spin\n\n");
}
}
/* Check the consonant against the string */
for (i = 0; i < size; ++i)
{
if (phrase[i] == ltr)
{
if (puzzle[i] == ltr) /* already revealed */
{
printf("Sorry, %c has already been revealed",ltr);
cnt = -1;
break;
}
else {
++cnt;
puzzle[i] = phrase[i];
}
}
}
if (cnt > 0)
{
int value = cnt * spin_value;
printf("Congratulations player %d!\n\n",player+1);
printf("There are %d %cs in the puzzle.\n\n", cnt, ltr);
printf("Your current score is %d.\n\n",value);
scores[player] += value;
printf("Your total score is %d.\n\n", scores[player]);
}
else if (cnt == 0)
{
printf("Sorry, there aren't any %cs in the puzzle.\n\n",ltr);
player_continues = 0;
}
}
return player_continues;
}
/***************************************/
/* buy_vowel */
/***************************************/
int buy_vowel(int player, int scores[], char phrase[], char puzzle[], int size)
{
int player_continues = 0;
printf("You chose to buy a vowel!\n\n");
if (scores[player] < VOWEL_COST) {
printf("Sorry, you don't have enough money yet.\n\n");
player_continues = 1;
}
else {
char ltr = ' ';
int i;
int cnt = 0;
while (isvowel(ltr) == 0) {
printf("Player %d, pick a vowel -> ",player+1);
scanf("%c", <r);
ltr = to_upper(ltr);
printf("\n\n");
}
for (i = 0; i < size; ++i)
{
if (phrase[i] == ltr)
{
if (puzzle[i] == ltr) /* already revealed */
{
printf("Sorry, %c has already been revealed",ltr);
cnt = -1;
player_continues = 0;
break;
}
else {
++cnt;
puzzle[i] = phrase[i];
}
}
}
scores[player] -= VOWEL_COST;
if (cnt > 0)
{
printf("Congratulations player %d!\n\n",player+1);
printf("There are %d %cs in the puzzle.\n\n", cnt, ltr);
player_continues = 1;
}
else if (cnt == 0)
{
printf("Sorry, there aren't any %cs in the puzzle.\n\n",ltr);
player_continues = 0;
}
printf("Total cost for your guess is %d.\n\n",VOWEL_COST);
printf("Your total score is %d.\n\n", scores[player]);
}
return player_continues;
}
/***************************************/
/* solve */
/***************************************/
int solve(int player, int score[], char phrase[], int size)
{
char soln[256];
int i;
int solved = 1;
printf("You chose to solve the puzzle!\n\n");
printf("DIRECTIONS: Type in your solution on one line. Separate each word with one space.\n\n");
printf("Solution -> ");
fflush(stdin);
gets(soln);
printf("\n\n");
for (i = 0; i < size; ++i)
{
soln[i] = to_upper(soln[i]);
if (soln[i] != phrase[i])
{
solved = 0;
break;
}
}
if (solved == 1)
{
printf("Congratulations Player %d!\n\n",player+1);
print_puzzle(size, phrase);
printf("You won the round!!!! Your final score is %d\n\n",score[player]);
printf("Thanks for playing Wheel of Fortune!!!\n\n");
}
else
{
printf("Sorry Player %d\n\n",player+1);
printf("Your solution does not match\n\n");
}
return solved;
}
/***************************************/
/* player_turn */
/***************************************/
int player_turn(int player, int score[], char phrase[], char puzzle[], int size)
{
int solved = 0;
char player_continues = 1;
while (player_continues == 1)
{
char choice = ' ';
print_puzzle(size, puzzle);
printf("Player %d, what do you want to do?\n\n", player+1);
while (choice != 'S' && choice != 'O' && choice != 'B') {
printf("(S)pin, S(o)lve or (B)uy a vowel -> ");
scanf("%c", &choice);
choice = to_upper(choice);
printf("\n");
}
if (choice == 'S')
{
player_continues = spin(player, score, phrase, puzzle, size);
}
else if (choice == 'O')
{
solved = solve(player, score, phrase, size);
player_continues = 0;
}
else /* buy a vowel */
{
player_continues = buy_vowel(player, score, phrase, puzzle, size);
}
}
return solved;
}
/***************************************/
/* initialize_array */
/***************************************/
int initialize_array(char phrase[], char puzzle[])
{
FILE* phraseFile = NULL;
char filename[256];
int cnt = 0, x;
char word[100];
x=time(NULL);
while (!phraseFile)
{
printf("Enter the name of the file containing the secret phrase -> ");
scanf("%s", filename);
// phraseFile = fopen("d:/test.txt", "r");
phraseFile = fopen(filename, "r");
if (!phraseFile) printf("\nSorry, I could not open %s, lets try again\n",filename);
}
while (!feof(phraseFile))
{
char c = ' ';
fscanf(phraseFile, "%c", &c);
if ((int)c < 15) break;
if (c >= 'a' && c <= 'z') /* convert lower case to upper case */
{
c = to_upper(c);
}
phrase[cnt] = c; /* place the solution character into the array */
if (c >= 'A' && c <= 'Z')
{
puzzle[cnt] = '*';
}
else /* it's not a letter */
{
puzzle[cnt] = c;
}
++cnt;
}
return cnt;
}
/***************************************/
/* main */
/***************************************/
int main(int argc, char* argv[])
{
char phrase[NAME_LENGTH];
char puzzle[NAME_LENGTH];
int puzzle_length;
int num_players = 0;
int solved = 0;
int current_player;
int player_scores[NUM_PLAYERS] = {0, 0, 0};
printf("\n");
printf("Over 50,000 in cash and prizes just waiting to be won!\n");
printf(" Here on Wheel ... of ... Fortune!\n");
printf(" (loud drum roll and music...)\n");
printf("\n");
/* Get the number of players */
while (num_players < 1 || num_players > 3)
{
printf("\nHow many players will be playing the game (1-3)? ");
scanf("%d",&num_players);
printf("\n");
}
/* Get the puzzle */
puzzle_length = initialize_array(phrase, puzzle);
current_player = 0;
while (solved == 0) {
print_score(num_players, player_scores);
solved = player_turn(current_player, player_scores, phrase, puzzle, puzzle_length);
solved = player_turn(current_player, player_scores, phrase, puzzle, puzzle_length);
solved = player_turn(current_player, player_scores, phrase, puzzle, puzzle_length);
if (solved == 0)
{
++current_player;
if (current_player == num_players)
{
current_player = 0;
}
}
}
return(0);
}