Evaluation of inferior code-21 points in Appendix B of C language (version 2nd)

Source: Internet
Author: User


typedefstruct {   int cards[52];   int num_cards;}POKER ; 

 



. In both cases, the scores are described with low and high. The final valid score is described in score.

 

typedefstruct {   int score;   int low  ;   int high ;}GAMER ;

 

Void game_21 (void) {POKER poker; GAMER player = {0, 0, 0}, dealer = {0, 0, 0}; // game process}

 

init_poker( &poker );void init_poker( POKER * );void init_poker( POKER *p_pkr ){   int i ;   p_pkr->num_cards =   sizeof p_pkr->cards                       / sizeof p_pkr->cards[0] ;//52                        for ( i = 0 ; i < p_pkr->num_cards ; i ++ ){      p_pkr->cards[i] = i % 13 + 1;    }}

 

 

#define N 1typedefstruct {   int cards[52*N];   int num_cards;}POKER ; 

 



#include <time.h>srand( ( unsigned )time(NULL) );   

 

 

Puts ("banker:"); getcard (& dealer, dealcard (& poker); int dealcard (POKER *); void disp (int); void getcard (GAMER *, int); int dealcard (POKER * p_pkr) {int num = rand () % p_pkr-> num_cards; int card = p_pkr-> cards [num]; p_pkr-> cards [num] = p_pkr-> cards [-- p_pkr-> num_cards]; return card;} void getcard (GAMER * p_plr, int card) {disp (card ); switch (card) {case 1: p_plr-> low + = 1; p _ Plr-> high + = 11; break; default: p_plr-> low + = card; p_plr-> high + = card; break; case 11: case 12: case 13: p_plr-> low + = 10; p_plr-> high + = 10; break;} p_plr-> score = p_plr-> high> 21? P_plr-> low: p_plr-> high; printf ("total score: % d \ n", p_plr-> score);} void disp (int card) {switch (card) {case 1: puts ("Ace"); return; default: printf ("% d \ n", card); return; case 11: puts ("Jack "); return; case 12: puts ("Queen"); return; case 13: puts ("King"); return ;}}

 

Puts ("your card:"); getcard (& player, dealcard (& poker); do {getcard (& player, dealcard (& poker ));} while (again ("continue to cards (Y/N )? ") = YES );

 

 

YESNO again (char *); YESNO again (char * p_message) {int c; puts (p_message); c = getchar (); while (getchar ()! = '\ N') {// read a row} if (c = 'y' | c = 'y') {return YES;} return NO ;}

 

 

Puts ("the dealer continues to take the card:"); do {getcard (& dealer, dealcard (& pkr) ;}while (dealer. score <17 );

 

 

   declare_winner( dealer , player );

 

 

Void declare_winner (GAMER, GAMER); void declare_winner (GAMER dealer, GAMER player) {if (dealer. score = 21) {puts ("You lost. "); Return;} if (dealer. score> 21) {if (player. score> 21) {puts (" draw. "); Return ;}} if (dealer. score <21) {if (player. score> 21) {puts (" You lost. "); Return;} if (dealer. score> = player. score) {puts (" You lost. "); Return ;}} puts (" You win! \ A "); return ;}

 

 

/* Game: rebuilding the program at in Appendix B of C language book for everyone */# include <stdio. h> # include <stdlib. h> # include <time. h> typedef enum {NO, YES,} YESNO; typedef struct {int cards [52]; int num_cards;} POKER; typedef struct {int score; int low; int high ;} GAMER; YESNO again (char *); void game_21 (void); void init_poker (POKER *); int dealcard (POKER *); void disp (int ); void getcard (GAMER *, int); void declare_winner (GA MER, GAMER); int main (void) {do {system ("CLS"); game_21 (); // round game} while (again ("continue game (Y/N )? ") = YES); system (" PAUSE "); return 0;} int dealcard (POKER * p_pkr) {int num = rand () % p_pkr-> num_cards; int card = p_pkr-> cards [num]; p_pkr-> cards [num] = p_pkr-> cards [-- p_pkr-> num_cards]; return card ;} /* announce victory */void declare_winner (GAMER dealer, GAMER player) {if (dealer. score = 21) {puts ("You lost. "); Return;} if (dealer. score> 21) {if (player. score> 21) {puts (" draw. "); Return ;}} if (dealer. score <21) {if (player. score> 21) {puts (" You lost. "); Return;} if (dealer. score> = player. score) {puts (" You lost. "); Return ;}} puts (" You win! \ A "); return;}/* calculate the * p_plr score */void getcard (GAMER * p_plr, int card) {disp (card); switch (card) {case 1: p_plr-> low + = 1; p_plr-> high + = 11; break; default: p_plr-> low + = card; p_plr-> high + = card; break; case 11: case 12: case 13: p_plr-> low + = 10; p_plr-> high + = 10; break ;} p_plr-> score = p_plr-> high> 21? P_plr-> low: p_plr-> high; printf ("total score: % d \ n", p_plr-> score );} /* display card faces */void disp (int card) {switch (card) {case 1: puts ("Ace"); return; default: printf ("% d \ n", card); return; case 11: puts ("Jack"); return; case 12: puts ("Queen"); return; case 13: puts ("King"); return ;}/ * initialize * p_pkr */void init_poker (POKER * p_pkr) {int I; p_pkr-> num_cards = sizeof p_pkr-> cards/sizeof p_pkr-> cards [0]; // 52 For (I = 0; I <p_pkr-> num_cards; I ++) {p_pkr-> cards [I] = I % 13 + 1 ;}} void game_21 (void) {POKER poker; GAMER player = {0, 0, 0}, dealer = {0, 0, 0}; init_poker (& poker); srand (unsigned) time (NULL); puts ("banker:"); // Banker takes the first getcard (& dealer, dealcard (& poker )); puts ("\ n your card:"); // player draws getcard (& player, dealcard (& poker); do {getcard (& player, dealcard (& poker ));} While (again ("continue to sign (Y/N )? ") = YES); puts (" \ n the dealer continues to take the card: "); // The dealer continues to draw the card do {getcard (& dealer, dealcard (& poker ));} while (dealer. score <17); declare_winner (dealer, player);} YESNO again (char * p_message) {int c; puts (p_message); c = getchar (); while (getchar ()! = '\ N') {// read a row} if (c = 'y' | c = 'y') {return YES;} return NO;
}

(Full text)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.