Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- void game(int rate);
- void addcard(char* who);
- int cost(int cards[], int count);
- void output(int cards[], int suits[], int count);
- bool isBlackjack(int rate);
- int balance = 1000;
- int rate;
- int winMultiplier;
- bool isUsed[13][4];
- int playerCards[12], playerSuits[12], playerCount;
- int dealerCards[12], dealerSuits[12], dealerCount;
- char *cardsName[13] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
- char *suitName[4] = {"spades", "hearts", "diamonds", "clubs"};
- //пики черви бубны трефы
- int main()
- {
- extern int rate;
- extern int balance;
- printf("Blackjack game\n");
- again: ;
- printf("\nYour balance is %d.\n", balance);
- if (balance == 0) {
- printf("Game over.\n");
- getchar();
- return 0;
- }
- printf("Enter rate (or 0 to end game):\n");
- ratein: ;
- scanf("%d", &rate);
- if (rate == 0) {
- printf("Game was over with balance %d.\n", balance);
- getchar();
- return 0;
- } else if (rate > balance) {
- printf("Rate is too big. Enter smaller or 0 to end game.\n");
- goto ratein;
- } else {
- game(rate);
- goto again;
- }
- }
- void game(int rate)
- {
- extern int winMultiplier;
- winMultiplier = 1;
- extern int balance;
- extern int playerCards[12], playerSuits[12], playerCount;
- playerCount = 0;
- extern int dealerCards[12], dealerSuits[12], dealerCount;
- dealerCount = 0;
- addcard("player");
- addcard("player");
- addcard("dealer");
- if (playerCount == 2 && isBlackjack(rate) == true)
- return;
- costCheck: ;
- printf("Your cards: ");
- output(playerCards, playerSuits, playerCount);
- printf("Your points: %d\n", cost(playerCards, playerCount));
- printf("Dealer cards: ");
- output(dealerCards, dealerSuits, dealerCount);
- printf("Dealer points: %d\n", cost(dealerCards, dealerCount));
- if (cost(playerCards, playerCount) > 21) {
- printf("Oops! You've got overkill with %d points. You lost.\n", cost(playerCards, playerCount));
- balance -= rate;
- return;
- }
- if (cost(dealerCards, dealerCount) > 21) {
- printf("Dealer has got overkill with %d points. You won!\n", cost(dealerCards, dealerCount));
- balance += rate * winMultiplier;
- return;
- }
- if (cost(playerCards, playerCount) <= 21 && cost(dealerCards, dealerCount) > 17 &&
- cost(dealerCards, dealerCount) < 21) {
- printf("You've won with %d points, which more than %d dealer points.",
- cost(playerCards, playerCount), cost(dealerCards, dealerCount));
- balance += rate * winMultiplier;
- return;
- }
- char c;
- if (cost(playerCards, playerCount) < 21) {
- printf("One more card (Y/n) ?\n");
- scanf("%c", &c);
- if (c == 'Y' || c == 'y') {
- addcard("player");
- goto costCheck;
- }
- }
- if ((c == 'N' || c == 'n') && cost(dealerCards, dealerCount) < 17) {
- addcard("dealer");
- goto costCheck;
- }
- }
- void addcard(char* who)
- {
- extern bool isUsed[13][4];
- extern int playerCards[12], playerSuits[12], playerCount;
- extern int dealerCards[12], dealerSuits[12], dealerCount;
- if (!strcmp(who, "player")){
- do {
- srand(time(NULL));
- playerCards[playerCount] = rand() % 13;
- srand(time(NULL));
- playerSuits[playerCount] = rand() % 4;
- } while (isUsed[playerCards[playerCount]][playerSuits[playerCount]] == true);
- isUsed[playerCards[playerCount]][playerSuits[playerCount]] = true;
- ++playerCount;
- } else {
- do {
- srand(time(NULL));
- dealerCards[dealerCount] = rand() % 13;
- srand(time(NULL));
- dealerSuits[dealerCount] = rand() % 4;
- } while (isUsed[dealerCards[dealerCount]][dealerSuits[dealerCount]] == true);
- isUsed[dealerCards[dealerCount]][dealerSuits[dealerCount]] = true;
- ++dealerCount;
- }
- }
- int cost(int cards[], int count)
- {
- int i, total = 0;
- for (i = 0; i < count; ++i){
- switch (cards[i]) {
- case 0: total += 11; break; //Ace
- case 1: total += 2; break; //2
- case 2: total += 3; break; //3
- case 3: total += 4; break; //4
- case 4: total += 5; break; //5
- case 5: total += 6; break; //6
- case 6: total += 7; break; //7
- case 7: total += 8; break; //8
- case 8: total += 9; break; //9
- case 9: total += 10; break; //10
- case 10: //Jack
- case 11: //Queen
- case 12: total += 10; break; //King
- }
- }
- if (total > 21) { //Если набралось больше 21, то пересчет:
- total = 0; //тузы будут стоить 1 вместо 11
- for (i = 0; i < count; ++i)
- switch (cards[i]) {
- case 0: total += 1; break; //Ace
- case 1: total += 2; break; //2
- case 2: total += 3; break; //3
- case 3: total += 4; break; //4
- case 4: total += 5; break; //5
- case 5: total += 6; break; //6
- case 6: total += 7; break; //7
- case 7: total += 8; break; //8
- case 8: total += 9; break; //9
- case 9: total += 10; break; //10
- case 10: //Jack
- case 11: //Queen
- case 12: total += 10; break; //King
- }
- }
- return total;
- }
- bool isBlackjack(int rate)
- {
- extern int playerCards[12], playerSuits[12], playerCount;
- extern int dealerCards[12], dealerSuits[12], dealerCount;
- extern int winMultiplier;
- if (playerCount == 2 && dealerCount == 1) {
- if (cost(playerCards, playerCount) == 21 && !(dealerCards[0] == 2 || dealerCards[0] == 9 ||
- dealerCards[0] == 10 || dealerCards[0] == 11 ||
- dealerCards[0] == 12)) {
- balance += rate * 1.5;
- printf("Congratulations! You winned with Blackjack!\n");
- return true;
- } else if (dealerCards[0] == 0) {
- printf("Do you want to take your win which equal to the rate (Y/n) ?");
- int c;
- c = getchar();
- if (c == 'Y' || c == 'y') {
- balance += rate;
- return true;
- } else if (c == 'N' || c == 'n') {
- winMultiplier = 1.5;
- return false;
- }
- }
- }
- return false;
- }
- void output(int cards[], int suits[], int count)
- {
- extern char *cardsName[13];
- extern char *suitName[4];
- int i;
- for (i = 0; i < count; ++i) {
- printf("%s of %s", cardsName[cards[i]], suitName[suits[i]]);
- if (i != count - 1)
- printf(", ");
- else
- printf("\n");
- }
- }
Add Comment
Please, Sign In to add comment