程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 劣質代碼評析——《寫給大家看的C語言書(第2版)》附錄B之21點程序(八)

劣質代碼評析——《寫給大家看的C語言書(第2版)》附錄B之21點程序(八)

編輯:關於C語言


typedef
struct 
{
   int cards[52];
   int num_cards;
}
POKER ; 

 



。兩種情況下的分數分別用low和high描述。最後的有效分數用score描述。

 

typedef
struct 
{
   int score;
   int low  ;
   int high ;
}
GAMER ;

 

void game_21( void )
{
   POKER  poker;
   GAMER  player = { 0 , 0 , 0 } , 
          dealer = { 0 , 0 , 0 } ;
   //游戲過程

}

 

   

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 1
typedef
struct 
{
   int cards[52*N];
   int num_cards;
}
POKER ; 

 

   

#include <time.h>


srand( ( unsigned )time(NULL) );   

 

 

   puts("莊家拿牌:");
   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("總分:%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("你拿牌:");
   getcard( &player , dealcard( &poker ) );
   do{
      getcard( &player , dealcard( &poker ) );
   }while ( again("繼續要牌(Y/N)?") == YES );

 

 

YESNO again( char * );
YESNO again( char *p_message )
{
   int c;
   
   puts( p_message );
   c = getchar() ;
   
   while ( getchar() != '\n'){  //讀完一行 
   } 
   
   if ( c=='y' || c == 'Y' ){
      return YES;
   }
   
   return NO;
}

 

 

   puts("莊家繼續拿牌:");
   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("你輸了。"); 
       return ; 
   } 
     
   if ( dealer.score > 21 ){ 
      if( player.score > 21 ){ 
          puts("平局。"); 
          return ; 
      } 
   } 
   
   if ( dealer.score < 21 ){ 
      if( player.score > 21 ){ 
            puts("你輸了。"); 
            return ; 
       } 
       
      if( dealer.score >= player.score ){ 
          puts("你輸了。"); 
          return ; 
      } 
   } 
     
   puts("你贏了!\a"); 
   return; 
}

 

   

 

/* 21點游戲:對《寫給大家看的C語言書》附錄B之21點程序的重構 */

#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( GAMER , GAMER );

int main( void )
{
  
  do{
      system("CLS");
      game_21(); //一輪游戲      
  }while ( again( "繼續游戲(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;   
}

/* 宣布勝利 */
void declare_winner( GAMER dealer , GAMER player )
{
   if ( dealer.score == 21 ){ 
       puts("你輸了。"); 
       return ; 
   } 
     
   if ( dealer.score > 21 ){ 
      if( player.score > 21 ){ 
          puts("平局。"); 
          return ; 
      } 
   } 
   
   if ( dealer.score < 21 ){ 
      if( player.score > 21 ){ 
            puts("你輸了。"); 
            return ; 
      } 
       
      if( dealer.score >= player.score ){ 
          puts("你輸了。"); 
          return ; 
      } 
   } 
     
   puts("你贏了!\a"); 
   return; 
}
 
/* 計算*p_plr獲得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("總分:%d\n",p_plr->score);
   
}

/* 顯示card牌面 */
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;    
   } 
}

/* 初始化*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("莊家拿牌:");                       //莊家取第一張     
   getcard( &dealer , dealcard( &poker ) );
   
   puts("\n你拿牌:");                         //player抽牌 
   getcard( &player , dealcard( &poker ) );
   do{
      getcard( &player , dealcard( &poker ) );
   }while ( again("繼續要牌(Y/N)?") == YES );  

   puts("\n莊家繼續拿牌:");                  //莊家繼續抽牌 
   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'){  //讀完一行 
   } 
   
   if ( c=='y' || c == 'Y' ){
      return YES;
   }
   
   return NO;
}

(全文完)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved