int main(void){ srand(time(NULL)); unsigned deck[52] = { 1,1,1,1, // ACE 2,2,2,2, // TWO 3,3,3,3, // THREE 4,4,4,4, // FOUR 5,5,5,5, // FIVE 6,6,6,6, // SIX 7,7,7,7, // SEVEN 8,8,8,8, // EIGHT 9,9,9,9, // NINE 10,10,10,10, // TEN 10,10,10,10, // JACK 10,10,10,10, // QUEEN 10,10,10,10 // KING }; unsigned quit = 0; // stop the game unsigned player1_hand[26]; unsigned computer_hand[26]; do{ removeCardsFromHand(player1_hand); removeCardsFromHand(computer_hand); shuffleDeck(deck); size_t deckIndex = 0; size_t player1Index = 0; player1_hand[player1Index] = deck[deckIndex]; deckIndex++; player1Index++; player1_hand[player1Index] = deck[deckIndex]; deckIndex++; player1Index++; size_t computerIndex = 0; computer_hand[computerIndex] = deck[deckIndex]; deckIndex++; computerIndex++; computer_hand[computerIndex] = deck[deckIndex]; deckIndex++; computerIndex++; do{ printf("Player 1's hand: "); printHand(player1_hand); printf("Computer's hand: [unknown] "); printHand(computer_hand + 1); printf("0 for Hit, 1 for Stay: "); unsigned stay; scanf("%u", &stay); if(stay){ if(calculateHandValue(computer_hand) < 18){ computer_hand[computerIndex] = deck[deckIndex]; deckIndex++; computerIndex++; } // staying so leave break; } // hitting player1_hand[player1Index] = deck[deckIndex]; deckIndex++; player1Index++; if(calculateHandValue(computer_hand) < 18){ computer_hand[computerIndex] = deck[deckIndex]; deckIndex++; computerIndex++; } }while(1); putchar('\n'); printf("Player 1's hand: "); printHand(player1_hand); unsigned player1_value = calculateHandValue(player1_hand); printf("Player 1's Value: %u\n", player1_value); printf("Computer's hand: "); printHand(computer_hand); unsigned computer_value = calculateHandValue(computer_hand); printf("Computer's Value: %u\n", computer_value); putchar('\n'); if(player1_value > 21){ printf("Player 1 bust!\n"); if(computer_value > 21){ printf("Computer bust!\n"); }else{ printf("Computer wins!\n"); } }else if(computer_value > 21){ printf("Computer bust!\n"); if(player1_value > 21){ printf("Player 1 bust!\n"); }else{ printf("Player 1 wins!\n"); } }else if(player1_value > computer_value){ printf("Player 1 wins!\n"); }else{ printf("Computer wins!\n"); } putchar('\n'); printf("0 for Quit, 1 for New Game: "); scanf("%u", &quit); putchar('\n'); }while(quit != 0); return 0; }
int enterBetAI (P_PLAYER player, int minimumBet) { //int bluffingVal = player->AIAgressivity; //not currently used int handValue = calculateHandValue(player); int totalBet = game.bet.pot; int round = game.flow.round; int maxBet = maximumBet(player); int foldChance = 0; int raiseChance = 0; int raiseValue = 0; //initialize random value time_t t; srand((unsigned) time(&t)); int randomNumber = rand() % 100; //all in option if (maxBet <= minimumBet) { player->allIn = 1; if (maxBet > minimumBet) return maxBet - minimumBet; else return 0; } if (handValue < 16) { foldChance = 10; raiseChance = 5; } else { foldChance = 5; raiseChance = 25; } //higher raise change depending on value of hand cards if (handValue > 16) raiseChance += sqrt(handValue) * 0.5; //larger the stakes and lesser handValue, the higher is fold chance if (minimumBet > 0) foldChance += (minimumBet + (totalBet * 0.1)) / (sqrt(handValue) + 5); else foldChance = 0; //debug print printf("\nRound %d \nrandomNumber: %d \nhandValue: %d \n%s chances: \nfoldChance %d \nraiseChance %d \n", round, randomNumber, handValue, player->name, foldChance, raiseChance); if (randomNumber <= foldChance) { return -1; //fold } else if (randomNumber >= 100-raiseChance) { //calculation how much to raise int raiseRate = randomNumber - (100 - raiseChance); //round raise value raiseValue = (raiseRate / 10); raiseValue = minimumBet + (raiseValue * 10); if (raiseValue > maxBet) raiseValue = maxBet; return raiseValue - minimumBet; } else { return 0; //called } }