/* ---------------------------------------------------------------------- // Figure 16.2: A carnival game. // Figure 16.3: Masked menu function for the carnival game. // --------------------------------------------------------------------- */ #include "tools.h" #define PRIZES 10 typedef struct ITEMS { char name[20]; int price; } item; int choose ( item inventory[], bool mask[] ); void main ( void ) { int count = 0, total = 0; /* Number and value of prizes selected so far. */ int pick; /* Player's selection. */ char yesno; /* Continue or quit flag. */ int max; /* Player's random limit. */ item inventory [PRIZES] = { {"ball",5}, {"bear",15}, {"doll",10}, {"gorilla",15}, {"hat",1}, {"kazoo",3}, {"lollypop",3}, {"spider",1}, {"squirt gun",10}, {"stickers",1} }; bool mask[PRIZES] = {true,true,true,true,true,true,true,true,true,true}; banner(); puts( " Prize Giveaway Game" ); max = (time( NULL) % 10) + 6; /* Choose the random goal. */ // printf( " max = %i\n", max); /* For debugging only. */ for (yesno = 'y'; tolower( yesno ) == 'y'; scanf( " %c", &yesno )) { pick = choose( inventory, mask ); total += inventory[pick].price; if (total > max) break; ++count; printf( " Do you want to keep going? " ); } if (total > max) printf( " You ran out of luck! You lose.\n" ); else printf( " You are a lucky stiff! \n" " You have %i prizes worth %i tickets!\n", count, total ); bye(); } /*----------------------------------------------------------- */ int choose ( item inventory[], bool mask[] ) { int k; puts( "\n Here are the remaining prizes:" ); for (k = 0; k < PRIZES; ++k) if (mask[k]) printf( "%2i: %s\n", k, inventory[k].name ); printf( " Your choice? " ); scanf( "%i", &k ); while (k < 0 || k >= PRIZES || mask[k] == false) { puts( " Hey, can't you read? Choose a number on the list!" ); scanf( "%i", &k ); } mask[k] = false; return k; } /*----------------------------------------------------------- */ /* Output ------------------------------------------------------- Patience S. Goodenough CS 110 Fri Aug 27 1999 10:35:01 ------------------------------------------------------- Prize Giveaway Game Here are the remaining prizes: 0: ball 1: bear 2: doll 3: gorilla 4: hat 5: kazoo 6: lollypop 7: spider 8: squirt gun 9: stickers Your choice? 1 You ran out of luck! You lose. Normal termination. ------------------------------------------------------- Patience S. Goodenough CS 110 Fri Aug 27 1999 10:35:28 ------------------------------------------------------- Prize Giveaway Game Here are the remaining prizes: 0: ball 1: bear 2: doll 3: gorilla 4: hat 5: kazoo 6: lollypop 7: spider 8: squirt gun 9: stickers Your choice? 2 Do you want to keep going? n You are a lucky stiff! You have 1 prizes worth 10 tickets! Normal termination. ------------------------------------------------------- Patience S. Goodenough CS 110 Fri Aug 27 1999 10:35:28 ------------------------------------------------------- Prize Giveaway Game Here are the remaining prizes: 0: ball 1: bear 2: doll 3: gorilla 4: hat 5: kazoo 6: lollypop 7: spider 8: squirt gun 9: stickers Your choice? 2 Do you want to keep going? n You are a lucky stiff! You have 1 prizes worth 10 tickets! Normal termination. (beth:0.658)~/appliedc/ccode/ch_16_arrays> carn ------------------------------------------------------- Patience S. Goodenough CS 110 Fri Aug 27 1999 10:35:53 ------------------------------------------------------- Prize Giveaway Game Here are the remaining prizes: 0: ball 1: bear 2: doll 3: gorilla 4: hat 5: kazoo 6: lollypop 7: spider 8: squirt gun 9: stickers Your choice? 0 Do you want to keep going? y Here are the remaining prizes: 1: bear 2: doll 3: gorilla 4: hat 5: kazoo 6: lollypop 7: spider 8: squirt gun 9: stickers Your choice? 4 Do you want to keep going? y Here are the remaining prizes: 1: bear 2: doll 3: gorilla 5: kazoo 6: lollypop 7: spider 8: squirt gun 9: stickers Your choice? 5 Do you want to keep going? y Here are the remaining prizes: 1: bear 2: doll 3: gorilla 6: lollypop 7: spider 8: squirt gun 9: stickers Your choice? 7 You ran out of luck! You lose. Normal termination. ------------------------------------------------------- Patience S. Goodenough CS 110 Fri Aug 27 1999 10:36:56 ------------------------------------------------------- Prize Giveaway Game Here are the remaining prizes: 0: ball 1: bear 2: doll 3: gorilla 4: hat 5: kazoo 6: lollypop 7: spider 8: squirt gun 9: stickers Your choice? 0 Do you want to keep going? y Here are the remaining prizes: 1: bear 2: doll 3: gorilla 4: hat 5: kazoo 6: lollypop 7: spider 8: squirt gun 9: stickers Your choice? 5 Do you want to keep going? y Here are the remaining prizes: 1: bear 2: doll 3: gorilla 4: hat 6: lollypop 7: spider 8: squirt gun 9: stickers Your choice? 7 Do you want to keep going? n You are a lucky stiff! You have 3 prizes worth 9 tickets! Normal termination. ------------------------------------------------------- Patience S. Goodenough CS 110 Fri Aug 27 1999 11:25:43 ------------------------------------------------------- Prize Giveaway Game Here are the remaining prizes: 0: ball 1: bear 2: doll 3: gorilla 4: hat 5: kazoo 6: lollypop 7: spider 8: squirt gun 9: stickers Your choice? 10 Hey, can't you read? Choose a number on the list, sap! -1 Hey, can't you read? Choose a number on the list, sap! 3 You ran out of luck! You lose. Normal termination.