Browse Source

zufaellig generierte Karten sind jetzt von der Zeit abhängig + weiterr Test

remotes/origin/stefan
fdai7472 11 months ago
parent
commit
7e078d20c6
  1. 14
      src/main/c/Stefan/blackjack.c
  2. 2
      src/main/c/Stefan/blackjack.h
  3. 14
      src/test/c/Stefan/test_blackjack.c

14
src/main/c/Stefan/blackjack.c

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h>
int blackjack(){ int blackjack(){
@ -65,7 +66,8 @@ int userInputBJ(){
// Liefert eine Zufallszahl zwischen 1 und 10 // Liefert eine Zufallszahl zwischen 1 und 10
// Chance für 10 ist 4 Mal höher als für den Rest // Chance für 10 ist 4 Mal höher als für den Rest
// 1 = Ass // 1 = Ass
int getRandCard(){
int getRandCard(int offset){
srand(time(NULL) + offset);
int r = rand() % 13; int r = rand() % 13;
r++; r++;
@ -91,16 +93,16 @@ int playBJ(int bet, int balance){
int *dealerCardsArray = (int*) malloc(10 * sizeof(int)); int *dealerCardsArray = (int*) malloc(10 * sizeof(int));
if(dealerCardsArray){ if(dealerCardsArray){
dealerCardsArray[0] = getRandCard();
dealerCardsArray[0] = getRandCard(0);
printf("computer rand card: %d\n", dealerCardsArray[0]); printf("computer rand card: %d\n", dealerCardsArray[0]);
int *userCardsArray = (int*) malloc(10 * sizeof(int)); int *userCardsArray = (int*) malloc(10 * sizeof(int));
if(userCardsArray){ if(userCardsArray){
userCardsArray[0] = getRandCard();
userCardsArray[0] = getRandCard(1);
printf("user rand card: %d\n", userCardsArray[0]); printf("user rand card: %d\n", userCardsArray[0]);
userCardsArray[1] = getRandCard();
userCardsArray[1] = getRandCard(2);
printf("user rand card: %d\n", userCardsArray[1]); printf("user rand card: %d\n", userCardsArray[1]);
if(checkForBlackjack(userCardsArray)){ if(checkForBlackjack(userCardsArray)){
@ -125,7 +127,7 @@ int playBJ(int bet, int balance){
} }
if(hit()){ if(hit()){
userCardsArray[i] = getRandCard();
userCardsArray[i] = getRandCard(i);
printf("new card: %d\n", userCardsArray[i]); printf("new card: %d\n", userCardsArray[i]);
} else{ } else{
break; break;
@ -138,7 +140,7 @@ int playBJ(int bet, int balance){
dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i); dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i);
if(dealerCardsTotal < 17){ if(dealerCardsTotal < 17){
dealerCardsArray[i] = getRandCard();
dealerCardsArray[i] = getRandCard(i);
printf("Computer Card: %d\n", dealerCardsArray[i]); printf("Computer Card: %d\n", dealerCardsArray[i]);
dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i + 1); dealerCardsTotal = calculateCardsTotal(dealerCardsArray, i + 1);
} }

2
src/main/c/Stefan/blackjack.h

@ -7,7 +7,7 @@ int blackjack();
int getBalanceBJ(); int getBalanceBJ();
int getBetBJ(int balance, bool *exitPtr); int getBetBJ(int balance, bool *exitPtr);
int userInputBJ(); int userInputBJ();
int getRandCard();
int getRandCard(int offset);
int playBJ(int bet, int balance); int playBJ(int bet, int balance);
bool checkForBlackjack(int userCards[]); bool checkForBlackjack(int userCards[]);
int calculateCardsTotal(int cardsArray[], int len); int calculateCardsTotal(int cardsArray[], int len);

14
src/test/c/Stefan/test_blackjack.c

@ -116,6 +116,20 @@ void test_cards_ace_ace_9_total_equals_21() {
} }
void test_cards_10_10_ace_total_equals_21() {
//arrange
int userCards[] = { 10, 10, 11};
int expected_result = 21;
//act
int actual_result = calculateCardsTotal(userCards, 3);
//assert
TEST_ASSERT_EQUAL_INT(expected_result, actual_result);
}
void test_blackjack_bet_equals_10() { void test_blackjack_bet_equals_10() {
//arrange //arrange

Loading…
Cancel
Save