Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <time.h>
  5.  
  6. struct Player
  7. {
  8.     char name[256];
  9.     int sum;
  10. };
  11.  
  12. void PrintPlayerSum(struct Player *p)
  13. {
  14.      printf("Player %s has sum %d\n", p->name, p->sum);
  15. }
  16.  
  17. void wait ( int seconds )
  18. {
  19.     clock_t endwait;
  20.     endwait = clock () + seconds * CLOCKS_PER_SEC ;
  21.     while (clock() < endwait) {}
  22. }
  23.  
  24. int main()
  25. {
  26.     struct Player *player = malloc(sizeof(*player));
  27.     strcpy( player->name, "John");
  28.     player->sum = 0;
  29.  
  30.     while(1)
  31.     {
  32.         PrintPlayerSum(player);
  33.  
  34.         printf("Do you want another number? (y/n, q for quit) ");
  35.         char ch;
  36.  
  37.         scanf("%s", &ch);
  38.  
  39.         if( ch == 'q' )
  40.             break;
  41.  
  42.         if( ch == 'y' )
  43.         {
  44.             srand(time(NULL));
  45.            
  46.             int rnd = rand() % 13 + 1;
  47.             player->sum += rnd;
  48.  
  49.             printf("Player got %d\n", rnd);
  50.         }
  51.  
  52.         if( ch == 'n' || player->sum > 21)
  53.         {
  54.             if( player->sum > 21 )
  55.             {
  56.                 printf("\n*** You lost the game, please try again... ***");
  57.             }
  58.             else
  59.             {
  60.                 printf("\nCPU's turn\n");
  61.  
  62.                 int cpusum = 0;
  63.  
  64.                 while( 1 )
  65.                 {
  66.                        if( cpusum > 21 )
  67.                        {
  68.                            printf("\n*** CPU lost the game with the score %d, you win! ***", cpusum);
  69.                            break;
  70.                        }
  71.  
  72.                        if( cpusum > player->sum )
  73.                        {
  74.                            printf("\n*** CPU won the game with the score %d, please try again ***", cpusum);
  75.                            break;
  76.                        }
  77.                        
  78.                        wait(1);
  79.                        srand(time(NULL));
  80.                        int rnd = rand() % 13 + 1;
  81.                        cpusum += rnd;
  82.  
  83.                        printf("CPU got %d, sum is %d\n", rnd, cpusum);
  84.                 }
  85.             }
  86.  
  87.             break;
  88.         }
  89.  
  90.         printf("\n\n");
  91.     }
  92.    
  93.     /* Cleanup ******************/
  94.     free(player);
  95.     /****************************/
  96.        
  97.     printf("\n\n\n");
  98.     system("PAUSE");
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement