Advertisement
Advertisement
| 04.28.2008 at 11:26AM PDT, ID: 23359676 |
|
[x]
Attachment Details
|
||
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: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: |
#include <stdio.h>
#include <float.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define WORD_LENGTH 26
#define WHEEL_SIZE 24
#define NUM_PLAYERS 3
#define BANKRUPT 0
#define LOSE_TURN -999
#define VOWEL_COST 250
char to_upper(char aLtr)
{
char ltr = aLtr;
if (ltr >= 'a' && ltr <= 'z'){ /*converts letter to uppercase if typed in as lowercase*/
ltr = (char)((int)ltr - 32);
}
return ltr;
}
int spin_wheel()
{
int wheel[WHEEL_SIZE] = {800, 350, 450, 700, 300, 600, 5000, 300, 600, 300, 500,
800, 550, 400, 300, 900, 500, 300, 900, 600, 400, 300,
BANKRUPT, LOSE_TURN};
int spin = (int)(((float)rand() / RAND_MAX) * WHEEL_SIZE); /*randomly selects value*/
return wheel[spin];
}
int isvowel(char guess)
{
int istrue = 0; /*initializes is true to indicate a vowel has not been chosen*/
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; /*vowel has been chosen*/
}
void print_puzzle (int size, char puzzle[], char clue[])
{
int i;
printf("===============================\n");
printf("Puzzle\n");
printf("%s\n", clue); /*prints clue*/
printf("===============================\n\n");
for (i = 0; i < size; ++i){
printf("%c",puzzle[i]); /*prints word hidden*/
}
printf("\n\n===============================\n\n");
}
void print_score(int players, int score[])
{
int p;
printf("\n");
for (p = 0; p < players; ++p){
printf("Player %d ", p+1); /*prints player numbers*/
}
printf("\n");
for (p = 0; p < players; ++p){
printf("======== ");
}
printf("\n");
for (p = 0; p < players; ++p){
printf(" %d ", abs(score[p])); /*prints each players score*/
}
printf("\n\n");
}
int spin(int player, int scores[ ], char phrase[ ], char puzzle[ ], int size)
{
int player_continues = 1;
int spin_value;
char word[100];
printf("You chose to spin the wheel!\n\n");
spin_value = spin_wheel();
if (spin_value == -999) /* player spins lose turn */{
printf("Sorry Player %d, you spun lose a turn.\n", player+1);
player_continues = 0;
}
else if (spin_value == 0) /*player spins bankrupt */{
printf("Sorry Player %d, you spun bankrupt.\n", player+1);
scores[player] = 0;
player_continues = 0;
}
else{
char ltr = ' '; /*ltr is initialized to blank*/
int i;
int cnt = 0;
printf("You spun %d\n\n", spin_value);
/* getting the consonant */
do{
printf("Enter a letter ->");
if(fgets(word,100,stdin)){ /*gets users input*/
sscanf(word, "%c", <r);
}
printf("\n\n");
ltr = to_upper(ltr); /*converts letter choice to uppercase*/
if (isvowel(ltr)){ /*player cant guess vowel*/
printf("Incorrect input. You cannot pick a vowel when you've chosen to spin\n\n");
}
if (!isalpha(ltr)){ /*if input is something other than a letter*/
printf("Incorrect input. This is not a letter.\n\n");
}
}while(!isalpha(ltr)||isvowel(ltr)); /*continue until a consonant is chosen*/
for (i = 0; i < size; ++i){
if (phrase[i] == ltr){
if (puzzle[i] == ltr) /* already revealed */{
printf("Sorry, %c has already been revealed\n",ltr);
player_continues=0;
cnt = -1;
break;
}
else {
++cnt;
puzzle[i] = phrase[i]; /*reveals consonant*/
}
}
}
if (cnt > 0){
int value = cnt * spin_value; /*multiples numer of players choice of consonant by wheel 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; /*player looses turn*/
}
}
return player_continues;
}
int buy_vowel(int player, int scores[], char phrase[], char puzzle[], int size)
{
int player_continues = 0;
char ltr;
int i;
int cnt;
char word[100];
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 {
ltr = ' ';
cnt = 0;
while (isvowel(ltr) == 0) {
printf("Player %d, pick a vowel -> ",player+1);
if(fgets(word,100,stdin)){ /*gets users input*/
sscanf(word, "%c", <r);
}
ltr = to_upper(ltr); /*player choice converted to uppercase letter*/
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; /*player looses turn if letter has already been chosen*/
break;
}
else {
++cnt;
puzzle[i] = phrase[i]; /*letters of puzzle are revealed*/
}
}
}
scores[player] -= VOWEL_COST; /*cost of vowel is deducted from score*/
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;
}
int solve(int player, int score[], char phrase[], int size, char clue[]){
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 -> ");
gets(soln); /*accepts players answer*/
printf("\n\n");
for (i = 0; i < size; ++i){
soln[i] = to_upper(soln[i]); /*converts players guess to upper to avoid case sensitivity*/
if (isalpha(soln[i]) != phrase[i]) {
solved = 0; /*if players guess doesn't match phrase, puzzle is still not solved*/
break;
}
}
if (solved == 1) /*if guess is right*/{
printf("Congratulations Player %d!\n\n",player+1);
print_puzzle(size, phrase, clue);
score[player]+=1000; /*adds 1000 to players score for right answer*/
printf("You won the round!!!! Your score is %d\n\n",score[player]);
}
else {
printf("Sorry Player %d\n\n",player+1);
printf("Your solution does not match; a thousand dollars will be deducted.\n\n");
if(player[score]>=1000)
player[score]-=1000; /*1000 deducted for wrong answer if player has more than 1000*/
else
player[score]=0; /*if player does not have have 1000 score is just reduced to 0*/
}
return solved;
}
int player_turn(int player_num, int player, int score[], char phrase[], char puzzle[], char clue[], int size){
int solved = 0; /*solve is initialized to zero to indicate puzzle has not been solved*/
char player_continues = 1;
char options[10]; /*string to hold choice*/
while (player_continues == 1){
char choice = ' '; /*choice initialized to nothing*/
print_score(player_num, score);
print_puzzle(size, puzzle, clue);
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 -> ");
if(fgets(options,10,stdin))
{
sscanf(options, "%c", &choice); /*player types in choice and it is stored in options*/
}
choice = to_upper(choice); /*converts choice to uppercase to prevent case sensitivity*/
printf("\n");
}
if (choice == 'S'){ /*if player chooses spin*/
player_continues = spin(player, score, phrase, puzzle, size);
}
else if (choice == 'O'){ /*if player chooses solve*/
solved = solve(player, score, phrase, size, clue);
player_continues = 0;
}
else /* buy a vowel */{
player_continues = buy_vowel(player, score, phrase, puzzle, size);
}
}
return solved;
}
int initialize_array(char phrase[], char puzzle[], char clue[]){
FILE* phraseFile; /*input file*/
char temp[100]; /*temporary array*/
int line; /*line from which word and clue are chosen*/
int i=0; /*counter for loop*/
int len; /*length of line from file*/
line=rand()%41+1; /*randomization of line*/
phraseFile = fopen("clues.txt", "r"); /*opens file*/
if (!phraseFile) {
fprintf(stderr, "Oops - file not opened !\n"); /*if there is error in opening file program is exited*/
exit(1);
}
while(line--){
fgets(temp, 100, phraseFile); /*gets line and stores in temp*/
}
len=strlen(temp);
if(temp[len-1]=='\n')
temp[len-1]=0; /*places the null terminating character after word*/
/*separates clues from word*/
strcpy(clue,strtok(temp, "%"));
/*
strcpy(phrase,strtok(NULL,"%"));*/
char tmp[WORD_LENGTH];
strcpy(tmp,strtok(NULL,"%"));
strcpy(phrase, tmp + 1);
strcpy(puzzle,phrase);
/*strcpy(puzzle,phrase);*/
while(i<strlen(puzzle)){
if(isalpha(puzzle[i])){
puzzle[i] = '*'; /*hides word*/
}
++i;
}
fclose(phraseFile); /*closes file*/
return i;
}
int main(){
char phrase[WORD_LENGTH]; /*word that is chosen*/
char puzzle[WORD_LENGTH]; /*copy of phrase that is converted to *s*/
char clue[WORD_LENGTH]; /*corresponding clue in phrase file*//*length of phrase to chosen*/
int puzzle_length;
int round;
char num_players[5]; /*number that user types in*/
int solved = 0;
int current_player;
int player_scores[NUM_PLAYERS] = {0};
int a=0; /*variable that user types in that is saved to string num_player*/
srand(time(0)); /*condition to start randomization*/
current_player=rand()%3+1; /*what player the game starts with*/
printf("\n");
printf("Welcome to WHEEL OF FORTUNE!!!!!\n");
printf("\n");
/* Get the number of players */
while (a< 1 || a > 3){
printf("\nHow many players will be playing the game (1-3)? ");
if(fgets(num_players,5,stdin)){
sscanf(num_players, "%d", &a);
}
printf("\n");
}
/* Get the puzzle */
for(round=0; round<3; ++round){
puzzle_length = initialize_array(phrase, puzzle, clue); /*get the puzzle*/
current_player = rand()% a; /*starts with random player, not necessarily player 1*/
while (solved == 0) /*while the puzzle isnt solved*/{
solved = player_turn(a, current_player, player_scores, phrase, puzzle, clue, puzzle_length);
if (solved == 0){
++current_player;
if (current_player == a){
current_player = 0;
}
}
}
solved=0;
current_player = rand()% a; /*chooses player for next round*/
}
printf("Final Scores:\n");
print_score(a, player_scores);
/*Print who is the winner*/
if(player_scores[0]>player_scores[1] && player_scores[0]>player_scores[2])
printf("Player 1 is the winner with %d points!\n", player_scores[0]);
if(player_scores[1]>player_scores[0] && player_scores[1]>player_scores[2])
printf("Player 2 is the winner with %d points!\n", player_scores[1]);
if(player_scores[2]>player_scores[0] && player_scores[2]>player_scores[1])
printf("Player 3 is the winner with %d points!\n", player_scores[2]);
if(player_scores[0]==player_scores[1] && player_scores[0]>player_scores[2])
printf("Players 1 and 2 have tied with a score of %d\n", player_scores[0]);
if(player_scores[0]>player_scores[1] && player_scores[0]==player_scores[2])
printf("Players 1 and 3 have tied with a score of %d\n", player_scores[0]);
if(player_scores[1]==player_scores[2] && player_scores[1]>player_scores[0])
printf("Players 2 and 3 have tied with a score of %d\n", player_scores[1]);
return(0);
}
|