|
|
@ -12,6 +12,7 @@ int blackjack(){ |
|
|
|
|
|
|
|
|
|
|
|
while(balance > 0){ |
|
|
|
printf("Guthaben: %d\n", balance); |
|
|
|
int bet = getBetBJ(balance); |
|
|
|
balance -= bet; |
|
|
|
|
|
|
@ -74,6 +75,7 @@ int getRandCard(){ |
|
|
|
int playBJ(int bet, int balance){ |
|
|
|
|
|
|
|
int winnings = 0; |
|
|
|
int userCardsTotal = 0, computerCardsTotal = 0; |
|
|
|
|
|
|
|
int *computerCardArray = (int*) malloc(10 * sizeof(int)); |
|
|
|
|
|
|
@ -96,13 +98,68 @@ int playBJ(int bet, int balance){ |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(int i = 2; i < 10; i++){ |
|
|
|
|
|
|
|
userCardsTotal = calculateCardsTotal(userCardArray, i); |
|
|
|
|
|
|
|
printf("Your Cards Total : %d\n", userCardsTotal); |
|
|
|
|
|
|
|
if(userCardsTotal > 21){ |
|
|
|
printf("BUST\n"); |
|
|
|
|
|
|
|
free(userCardArray); |
|
|
|
free(computerCardArray); |
|
|
|
return balance; |
|
|
|
} |
|
|
|
|
|
|
|
if(hit()){ |
|
|
|
userCardArray[i] = getRandCard(); |
|
|
|
printf("new card: %d\n", userCardArray[i]); |
|
|
|
} else{ |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for(int i = 1; i < 10; i++){ |
|
|
|
|
|
|
|
computerCardsTotal = calculateCardsTotal(computerCardArray, i); |
|
|
|
|
|
|
|
if(computerCardsTotal < 17){ |
|
|
|
computerCardArray[i] = getRandCard(); |
|
|
|
printf("Computer Card: %d\n", computerCardArray[i]); |
|
|
|
computerCardsTotal = calculateCardsTotal(computerCardArray, i + 1); |
|
|
|
} |
|
|
|
printf("Computer Cards Total: %d\n", computerCardsTotal); |
|
|
|
|
|
|
|
if(computerCardsTotal >= 17 && computerCardsTotal < 21){ |
|
|
|
if(calculateIfYouWon(userCardsTotal, computerCardsTotal)){ |
|
|
|
winnings = calculateWinnings(false, bet); |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
if(computerCardsTotal > 21){ |
|
|
|
printf("COMPUTER BUST\n"); |
|
|
|
winnings = calculateWinnings(false, bet); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
free(userCardArray); |
|
|
|
} |
|
|
|
|
|
|
|
free(computerCardArray); |
|
|
|
} |
|
|
|
|
|
|
|
return balance; |
|
|
|
return balance + winnings; |
|
|
|
} |
|
|
|
|
|
|
|
bool checkForBlackjack(int userCardArray[]){ |
|
|
@ -113,6 +170,28 @@ bool checkForBlackjack(int userCardArray[]){ |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
bool hit(){ |
|
|
|
|
|
|
|
int answer; |
|
|
|
printf("Hit (1) or stand (2): "); |
|
|
|
scanf("%d", &answer); |
|
|
|
|
|
|
|
if(answer != 1){ |
|
|
|
printf("stand.\n"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
printf("hit.\n"); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
bool calculateIfYouWon(int userCardsTotal, int computerCardsTotal){ |
|
|
|
if(userCardsTotal > computerCardsTotal){ |
|
|
|
return true; |
|
|
|
} |
|
|
|
printf("Leider verloren.\n"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
int calculateCardsTotal(int cardsArray[], int len){ |
|
|
|
int sum = 0; |
|
|
|
bool aceFound = false; |
|
|
@ -130,7 +209,9 @@ int calculateCardsTotal(int cardsArray[], int len){ |
|
|
|
|
|
|
|
int calculateWinnings(bool blackjack, int bet){ |
|
|
|
if(blackjack){ |
|
|
|
printf("Du hast %d gewonnen!\n", bet * 2); |
|
|
|
return 3 * bet; |
|
|
|
} |
|
|
|
printf("Du hast %d gewonnen!\n", bet); |
|
|
|
return 2 * bet; |
|
|
|
} |