Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- struct Player
- {
- char name[256];
- int sum;
- };
- void PrintPlayerSum(struct Player *p)
- {
- printf("Player %s has sum %d\n", p->name, p->sum);
- }
- void wait ( int seconds )
- {
- clock_t endwait;
- endwait = clock () + seconds * CLOCKS_PER_SEC ;
- while (clock() < endwait) {}
- }
- int main()
- {
- struct Player *player = malloc(sizeof(*player));
- strcpy( player->name, "John");
- player->sum = 0;
- while(1)
- {
- PrintPlayerSum(player);
- printf("Do you want another number? (y/n, q for quit) ");
- char ch;
- scanf("%s", &ch);
- if( ch == 'q' )
- break;
- if( ch == 'y' )
- {
- srand(time(NULL));
- int rnd = rand() % 13 + 1;
- player->sum += rnd;
- printf("Player got %d\n", rnd);
- }
- if( ch == 'n' || player->sum > 21)
- {
- if( player->sum > 21 )
- {
- printf("\n*** You lost the game, please try again... ***");
- }
- else
- {
- printf("\nCPU's turn\n");
- int cpusum = 0;
- while( 1 )
- {
- if( cpusum > 21 )
- {
- printf("\n*** CPU lost the game with the score %d, you win! ***", cpusum);
- break;
- }
- if( cpusum > player->sum )
- {
- printf("\n*** CPU won the game with the score %d, please try again ***", cpusum);
- break;
- }
- wait(1);
- srand(time(NULL));
- int rnd = rand() % 13 + 1;
- cpusum += rnd;
- printf("CPU got %d, sum is %d\n", rnd, cpusum);
- }
- }
- break;
- }
- printf("\n\n");
- }
- /* Cleanup ******************/
- free(player);
- /****************************/
- printf("\n\n\n");
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement