//Deals the randomized deck of cards to up to 5 players. void deal(void) { int players; ct1=0; do //continues to ask until they enter a number from 1 to 5. { printf("How many players(Max 5): "); scanf("%i", &players); }while(players>5); ct5=25; //Tells tradeIn where to start getting new cards from. ct4=0; //Deals to the specified number of players. for(ct1=0; ct1<players; ct1++) { for(ct2=0; ct2<293; ct2++) printf("\n"); printf("Player %i:\n", ct1+1); //Prints player number before displaying system ("PAUSE"); //cards. displayHand(); tradeIn(players); } ct4=0; //Counts array location from 0 to 5*players. //Displays everyone’s hand after all trades have been made. for(ct1=0; ct1<players; ct1++) { printf("Player %i\n", ct1+1); displayHand(); } }
void playGame(){ //take each players bet betTaker(); //deal all players their intial hand dealHand(); //display each players hand displayHand(); //loop through each player int i; for (i = 0; i < numPlayers; i++){ printf("Player %d:\n", i+1); int hit = hitDecide(i); while (hit == 1) { dealCard(i, 0); displayHand(); hit = hitDecide(i); } } //hit for the computer computerHit(); for (i = 0; i < numPlayers; i++){ int winner = evaluate(i); int money = distributeWinnings(winner, i); keepTrackOfWinnings(money, i); } }
void testCardDeal() { Player p1, p2; p1.name = "Player 1"; p2.name = "Player 2"; Card deck[DECK_SIZE]; initDeck(deck); cout << "-------- TESTING CARD FUNCTIONS ---------" << endl; cout << "--- ORIGINAL SORTED DECK ---" << endl << endl; displayDeck(deck); cout << endl << "--- SHUFFLED DECK ---" << endl << endl; shuffleCards(deck, DECK_SIZE); displayDeck(deck); drawHand(deck, p1); drawHand(deck, p2); cout << endl << "--- PLAYER HANDS ---" << endl << endl; displayHand(p1); cout << endl << endl; displayHand(p2); cout << endl << endl; tossCardFromHand(p1.hand[2], p1); tossCardFromHand(p2.hand[2], p2); cout << endl << "--- PLAYER HANDS ---" << endl << endl; displayHand(p1); cout << endl << endl; displayHand(p2); cout << endl << endl; drawCard(deck, p1); drawCard(deck, p2); cout << endl << "--- PLAYER HANDS ---" << endl << endl; displayHand(p1); cout << endl << endl; displayHand(p2); cout << endl << endl; }
//Displays 5 cards at a time working through the deck. void assigned(void) { char cont; int terminate; terminate=ct4=0; do { displayHand(); do { //Skips option to continue when the deck is cashed out. if(ct4!=50) { printf("continue? "); cont=getchar(); cont=getchar(); } else cont='N'; //Terminates the outer while loop. if(cont=='N' || cont=='n') terminate++; //Introducing couter 5 allows the loop to keep running until a valid input is recieved. if(cont=='Y' || cont=='y' || cont=='N' || cont=='n') ct5++; else { printf("You wanna try that again.....\n\n"); ct5=0; } }while(ct5==0); }while(terminate==0); printf("\n"); }
void Blackjack::startGame() { string cardName[] = { "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "KING", "QUEEN", "ACE" }; map<int, int> mp; map<string, int> card; deque<int> userPurse;//holds users pokerChips gameCounter = 0;//starts with 0 playing = false;//playing is false by default until user decides to play pokerChip = 1000;//each poker chip = $1000.00 userBet = 0; aceValue = 0;//this will hold the users choice to make the ACE have a face value of either 1 or 11. userInput = ' ';//this variable will be used to decide if the user decides to quit before starting program userChoice;//this variable will hold users decision to hit or stay. playAgain; user_stay, dealer_stay;//will evaluate to true if the user or dealer decide to STAY(not take anymore cards) userWins = 0; dealerWins = 0; drawCounter = 0;//holds number of draws occured. /*this for loop will iterate through our cards and assign each card its appropriate value*/ for (int i = 0; i < 12; ++i) { //we make cards 10 to K = 10 if (i > 8) { card[cardName[i]] = 10; } else { card[cardName[i]] = i + 2; }//end if else }//end for //The ACE will = 11 card[cardName[12]] = 11; //Output rules and make sure the user wants to play. cout << "Alright " << userName << ", this is Blackjack, best of 5 hands wins, you sittin' in?" << endl; //data validation while (userInput != 'Q' && userInput != 'Y') { cout << "\nEnter 'Q' to leave,\nor 'Y' to sit down and play BlackJack: "; cin >> userInput; userInput = toupper(userInput);//convert to uppercase for easier comparison }//end while //if user enters Y, playing evaluates to true and we give the user 10 Poker Chips if (userInput == 'Y') { playing = true; cout << "\nHere are 10 Poker Chips to start you off, 1 Chip = $1000.00" "\nyou must bet at least one chip to play.\nBet all 10 if you're feelin lucky.\n" << endl; //give player 10 PokerChips for (int i = 0; i < 10; ++i) { userPurse.push_back(pokerChip); }//end for } else//if they press Q the program ends. { cout << "Very well, good day." << endl;//quit game. }//end if else //Game begins inside this while loop while (playing == true) { //set variables to appropriate values gameCounter = 0; userWins = 0; dealerWins = 0; userBet = 0; //flush buffer incase user entered more than one character cin.clear(); fflush(stdin); cout << userName << "'s Purse: $" << (userPurse.size() * pokerChip) << endl; cout << "Alright " << userName << ", Place your bet: "; //validate input, make sure users bet is at least 1 and not > their purse. while (userBet > userPurse.size() || userBet < 1) { userBet = 0; cin >> userBet; //while input is not an integer, we clear buffer and ask for new input while (!cin) { cout << "That is not a number,\nenter the number of chips you want to bet: "; cin.clear();//empty buffer cin.ignore();//ignore what was entered. cin >> userBet; }//end inner while //output the problem with their input so the user knows if (userBet < 1) { cout << "You need to bet at least one chip cheap-o,\nenter the number of chips you want to bet: "; } else if (userBet > userPurse.size()) { cout << "Hah, you don't have that many chips " << userName << ",\nenter the number of chips you want to bet: "; }//end if else }//end middle while //remove PokerChips they bet from purse for (int i = 0; i < userBet; ++i) { userPurse.pop_back(); }//end for //OUTPUT TEST cout << "\n\n" << userName << "'s Purse: $" << (userPurse.size() * pokerChip) << endl; cout << userName << "'s Bet: $" << (userBet * pokerChip) << endl; srand(time(NULL));//new random number to use for shuffling. //create out vector<string> userCards; vector<string> dealerCards; vector<string> deck; map<int, int>mp; while (userWins < 3 && dealerWins < 3) { ++gameCounter; //increment gameCounter //We clear the players hands and reshuffle the deck to start a new game. userCards.clear(); dealerCards.clear(); deck.clear(); mp.clear(); //output player bet, game #, and wins for each player cout << "===============GAME=STATS=================" << endl; cout << "GAME " << gameCounter << endl; cout << userName << "'s GAMES WON: " << userWins << "\nDEALER GAMES WON: " << dealerWins << "\n" << "DRAWS: " << drawCounter << endl; cout << "================GAME=" << gameCounter << "=====================" << endl; int randCard = shuffle(); int usedCard = 1;// //shuffle deck for (int i = 0; i < 52; i++) { while (mp[randCard] == usedCard)//while a card has already been placed in the deck, we search for another card. { randCard = shuffle();//generate a new card }//end while mp[randCard] = 1;//the card added to the deck will now = 1 so we know its been added already. int cardNum = getCard(randCard);//gives cardNum the value of a card deck.push_back(cardName[cardNum - 1]);//we place the num and its corresponding cardName in the deck. }//end for //gives the player and dealer 2 cards each userCards.push_back(deck[0]); userCards.push_back(deck[1]); dealerCards.push_back(deck[2]); dealerCards.push_back(deck[3]); //we need to put the dealers second card face down so the user cannot see it dealerHand = 0; userHand = 0; //output users hand /*takes users cards, adds them together and puts them in userHand this lets us to keep track of the users cards sum*/ userHand = card[userCards[0]] + card[userCards[1]]; //do the same with dealerHand dealerHand = card[dealerCards[0]] + card[dealerCards[1]]; int count = 4;//starts at 4 because 4 cards are removed from deck from the beginning userChoice = ' ';//holds the users userChoice to stay (S) or hit(H) user_stay = false; dealer_stay = false; //while neither player has decided to STAY this loop runs. while (user_stay == false || dealer_stay == false) { cout << "===================STARTING COMPARISON=====\n" << endl; //display both players hands cout << "Dealers Hand: "; displayDealerHand(dealerCards);//displays all cards with 1 face down. cout << "\n" << userName << "'s Hand: "; displayHand(userCards);//displays all cards cout << userName << " has: " << userHand << endl; //see if someone has blackjack already(if dealt an ace + 10,J,K or Q) if (userHand == 21) { cout << userName << " has Blackjack!" << endl; ++userWins; break; } else if (dealerHand == 21) { cout << "Dealer has Blackjack !" << endl; cout << "\nDEALER HAND: "; displayHand(dealerCards); ++dealerWins; break; } else if (dealerHand == 21 && userHand == 21) { cout << "Dealer: PUSH! Lucky this time " << userName << ",\nyou get your bet back." << endl; ++drawCounter; break; } else if (userHand > 21) { cout << "Dealer: Jeeze, two aces, pretty rare." << endl; getAceValue(userHand); cout << "Dealer: you now have " << userHand << "."; } else if (dealerHand > 21)//the dealer has been dealt 2 aces { dealerHand -= 10;/*so we automatically make one of their values 1 because that is what any player would do */ }//end if else //Ask user to hit or stay if (user_stay == false) { cout << "\nSo " << userName << ", will you hit or stay?\nEnter H or S: "; userChoice = ' ';//make userChoice null for next time through while (userChoice != 'H' && userChoice != 'S') { userChoice = ' '; cin >> userChoice; userChoice = toupper(userChoice); }//end inner while }//end if cout << "====================================" << endl; //flush buffer cin.clear(); fflush(stdin); /*IF user choses to 'hit' we give them another card, ELSE they stay with what they have and we break the loop and compare their hand with the dealers*/ if (userChoice == 'H') { cout << userName << " decides to hit." << endl; userCards.push_back(deck[count]); //CODE CHECK -- userCards.push_back("ACE"); userHand += card[userCards[userCards.size() - 1]]; ++count; //if user gets an ACE we decide if its value is 1 or 11. if (userCards.back() == "ACE") { int &ref = userHand; getAceValue(ref);//call method to decide what the ace's value will be. cout << "\n" << endl; }//end inner if //display hand to user cout << userName << "'s Hand: "; displayHand(userCards); cout << userName << " has: " << userHand << endl; //if users new card puts their score over 21, user loses. if (userHand > 21) { cout << "Dealer: " << userName << " busts!" << endl; ++dealerWins; break; } else if (userHand == 21) { cout << userName << " has Blackjack!" << endl; ++userWins; break; }//end inner if else } else if (userChoice == 'S') { cout << userName << " decides to stay." << endl; user_stay = true; }//end outer if else /*if the dealers hand sum is < 17 they will take another card. This loop only runs when the dealer hasn't decided to stay.*/ if (dealer_stay == false) { if (dealerHand <= 17) { cout << "\nDealer: I'll hit" << endl; dealerCards.push_back(deck[count]); dealerHand += card[dealerCards[dealerCards.size() - 1]];//adds one more card to dealers hand. ++count;//increase count so the next card is different. //if dealers last card is an ace and it puts him over 21, dealer decides to make ACE value == 1. if (dealerCards.back() == "ACE" && dealerHand > 21) { dealerHand -= 10;//take 10 off of dealers sum cout << "\nDealer gets an ACE!\nDealer: I'll make its value a 1." << endl; }//end inner if //check dealers status for under 21, blackjack, or bust. if (dealerHand < 21) { cout << "DEALER HAND: "; displayDealerHand(dealerCards); } else if (dealerHand > 21) { cout << "DEALER HAND: "; displayHand(dealerCards); cout << "DEALER BUSTS, " << userName << " WINS!" << endl; ++userWins; break; } else if (dealerHand == 21) { cout << "Dealer has Blackjack!" << endl; cout << "\nDEALER HAND: "; displayHand(dealerCards); ++dealerWins; break; }//end inner if else } else//else dealer stays with current cards { cout << "\nDealer: I'll stay.\n"; dealer_stay = true; }//end middle if else }//end outer if //if dealer and user decide to stay we see who wins. if (user_stay == true && dealer_stay == true) { //both players have decided to stay, display both hands and see who has won. cout << "\nDEALER HAND: "; displayHand(dealerCards); cout << userName << "'s Hand: "; displayHand(userCards); if (userHand > dealerHand)//if users hand is greater than dealers hand after both players decide to STAY { cout << userName << " wins!" << endl; ++userWins; break; } else if (dealerHand > userHand)//if dealers hand is greater than users hand after both players decide to STAY { cout << "Dealer wins!" << endl; ++dealerWins; break;//break out of inner loop } else if (dealerHand == userHand)//if both players have 21 no one wins { cout << "The game is a draw.\n" << userName << " you get your bet back." << endl; ++drawCounter; break; }//end inner if else }//end outer if }//end inner while }//end middle while //output game stats cout << "\n" << userName << "'s GAMES WON: " << userWins << "\nDEALER GAMES WON: " << dealerWins << "\n" "DRAWS: " << drawCounter << endl; cout << "================BEST=OF=THREE=HANDS=IS=OVER===================" << endl; //flush buffer cin.clear(); fflush(stdin); /*if the user wins we return double their initial bet and we ask them if they want to play again, if they play again they keep their winnings, if not the game is reset.*/ playAgain = ' '; if (userWins == 3) { cout << "\t================" << userName << " WINS POT================" << endl; //place users bet * 2 back in userPurse for (int i = 0; i < (userBet * 2); ++i) { userPurse.push_back(pokerChip); }//end for cout << userName << "'s PURSE: " << (userPurse.size() * pokerChip) << endl; cout << userName << " wins game, you get back double your bet! Play again?" << endl; //TEMP: userInput was used in the beginning, will set to null now to use for same purpose. while (playAgain != 'Y' && playAgain != 'N') { cout << "Enter Y or N: "; cin >> playAgain; playAgain = toupper(playAgain); }//end inner while if (playAgain == 'Y') { cout << "Alright lets keep playing." << endl; } else { cout << "GAME DONE" << endl; playing = false; }//end inner if else }
int main(){ float result; char userName [40]; int i, count, choice, betMarker, pLeft; double bet, pot; Player newPlayers [PLAYERS]; /*Intro for the game*/ setup(newPlayers, userName); Deck newDeck; srand(time(NULL)); /*MAIN GAME LOOP, BASED ON GALLEONS*/ while (newPlayers[0].galleons >= 5){ printf("\n\n======== NEW ROUND ========\n\n\n"); pot = bet = 0; /*Always update each player to be in the game initially, unless they have less than 5 galleons*/ for(i = 0; i<PLAYERS; i++){ newPlayers[i].inGame = 1; if (newPlayers[i].galleons < 5){ newPlayers[i].galleons = 0; newPlayers[i].inGame = 0; newPlayers[i].checkBet = 0; } newPlayers[i].checkBet = 0; newPlayers[i].galleons = newPlayers[i].galleons - 5; if(newPlayers[i].inGame == 1){ pot = pot + 5; } } /*if the three computers are out of the game, user has won, so end the game!*/ pLeft = 0; for (i = 1; i<PLAYERS; i++){ if (newPlayers[i].inGame == 0){ pLeft++; } if (pLeft == 3){ printf("\n\nCONGRATULATIONS %s, you won! Harry, Voldy and Dumbly seem to have lost all their money!!!\n\n", newPlayers[0].name); return 0; } } sleep(2); /*Print out current standings, how many galleons each player has and if they are still in*/ printf("Current Standings:\n\n"); for (i = 0; i<PLAYERS; i++){ if (newPlayers[i].inGame == 1){ printf("%s has %.2f Galleons\n", newPlayers[i].name, newPlayers[i].galleons); } } for (i = 0; i<PLAYERS; i++){ if (newPlayers[i].inGame == 0){ printf("%s is out of the game\n", newPlayers[i].name); } } /*Initialize Game*/ fillDeck(&newDeck); shuffleDeck(&newDeck); dealOut(&newDeck, newPlayers); printf("\nYou are about to receive your hand!\n"); /*User specifics*/ sleep(1); displayHand(newPlayers); result = MC_rate(newPlayers, 0); sleep(1); /*Monte Carlo Advice in MC.c*/ advice(&newPlayers[0]); /*This exchange is only for the user, computers have its own function*/ exchangeCards(newPlayers, &newDeck); displayHand(newPlayers); printf("\nYou can now either: \n1) Place a Bet\n2) Pass\n3) Fold\nPlease type 1,2, or 3: "); scanf("%d", &choice); if (choice == 1){ bet = placeBet(newPlayers, &pot); } else if (choice == 3){ endPlayer(newPlayers, 0); } else if (choice == 2){ printf("You have chosen to pass.\n"); } /*run Computer exchanges and whether they decide to raise, pass or fold*/ count = 1; while (count <= 3){ if(newPlayers[count].inGame == 1){ sleep(1); /*result value is more for testing purposes, but still needs to run to fill in the correct*/ /*int array mcAdivce for Monte carlo advice for each computer*/ result = MC_rate(newPlayers, count); /*This can be found in exchange.c, just exchange based on MC advice*/ computerExchange(newPlayers, &newDeck, count); /*This can be found in display.c, determines for the computer what to do (raise, pass, fold)*/ comp_decision(newPlayers, count, &bet, &pot); /*keep track of which computer raises for later*/ if (newPlayers[count].checkBet > 0){ betMarker = count; } } count++; } /*if any computer raises, then go back to user one time to either match or fold, then each computer*/ if (betMarker > 0){ if (newPlayers[0].galleons < newPlayers[betMarker].checkBet){ printf("\nOH NOOOO, the bet is more than you can afford, and by wizarding rules, you lose the game!!!\n\n"); return 0; } if (newPlayers[0].inGame != 0){ printf("%s raised by %.2lf. Would you like to either:\n1) Match\n2) Fold \nPlease type 1 or 2: ", newPlayers[betMarker].name, newPlayers[betMarker].checkBet); scanf("%d", &choice); if (choice == 1){ newPlayers[0].galleons = newPlayers[0].galleons - newPlayers[betMarker].checkBet; pot = pot + newPlayers[betMarker].checkBet; printf("You have matched the raise\n"); } else { newPlayers[0].inGame = 0; printf("You Folded\n"); } } /*Determine whether each computer should match or fold*/ for (i = 1; i<betMarker; i++){ if (newPlayers[i].inGame == 1){ if (newPlayers[i].galleons > newPlayers[betMarker].checkBet){ result = analyze_hand(newPlayers[i]); if (result < 23){ newPlayers[i].inGame = 0; printf("%s has folded\n", newPlayers[i].name); } else{ newPlayers[i].galleons = newPlayers[i].galleons - newPlayers[betMarker].checkBet; pot = pot + newPlayers[betMarker].checkBet; printf("%s has matched the raise\n",newPlayers[i].name); } } } } } sleep(2); /*find winner of the game, give winning's to rightful winner, display winner's hand*/ findWinner(newPlayers, &pot); /*reset betMarker for next game*/ betMarker = 0; sleep(2); } /*After game loop, you have lost all your money or you have less than the 5 Galleon entrance amount*/ printf("\n\nOH NO, you lost at Wizard's poker!!!!\n"); printf("Final Galleon Count for each player:\n"); for (i = 0; i < PLAYERS; i++){ if (newPlayers[i].galleons < 0){ newPlayers[i].galleons = 0; } printf("\t%s has %.2f Galleons\n", newPlayers[i].name, newPlayers[i].galleons); } return 0; }
int main() { int i, j; int seed = 1000; int numPlayer = 2; int p, r; int k[10] = {adventurer, great_hall, cutpurse, gardens, mine , remodel, smithy, village, sea_hag, embargo}; int cards[10] = {copper, smithy, village, silver, mine, gold, gardens, remodel, embargo, cutpurse}; struct gameState G, testG; int index, count; int treasureCount; int treasureDrawn, otherRevealed; int preDeck, preDiscard, preHand; bool pass = true; memset(&G, 23, sizeof(struct gameState)); // clear the game state r = initializeGame(numPlayer, k, seed, &G); // initialize a new game printf("----------------- Testing adventurer\n"); // Number of runs for (j = 1; j < 1001; j++) { // Copy the game state to a test case memcpy(&testG, &G, sizeof(struct gameState)); // Player 0 p = 0; printf("----------------- Random Deck Test %d\n", j); printf("----------------- Initial counts\n"); // Initialize deckCount and treasureCount testG.deckCount[p] = 0; treasureCount = 0; // Create deck of 10 random cards for (i = 0; i < 10; i++) { index = floor(Random() * 10); testG.deck[p][i] = cards[index]; testG.deckCount[p]++; } // Check deck for number of treasures for (i = 0; i < testG.deckCount[p]; i++) { if (testG.deck[p][i] == copper || testG.deck[p][i] == silver || testG.deck[p][i] == gold) { treasureCount++; } } // Discard should be empty testG.discardCount[p] = 0; // Create fixed hand of 5 cards testG.handCount[p] = 0; for (i = 0; i < 5; i++) { testG.hand[p][i] = estate; testG.handCount[p]++; } // Save pre-test values preDeck = testG.deckCount[p]; preDiscard = testG.discardCount[p]; preHand = testG.handCount[p]; // Calculate expected values // If no treasures in deck, then reveal all cards in deck if (treasureCount == 0) { treasureDrawn = 0; otherRevealed = testG.deckCount[p]; } // If 1 treasure in deck, // then reveal all cards in deck in an attempt to get 2 treasures else if (treasureCount == 1) { treasureDrawn = 1; otherRevealed = testG.deckCount[p] - treasureDrawn; } // If at least 2 treasures in deck else { treasureDrawn = 0; otherRevealed = 0; i = testG.deckCount[p] - 1; while (treasureDrawn < 2) { if (testG.deck[p][i] == copper || testG.deck[p][i] == silver || testG.deck[p][i] == gold) { treasureDrawn++; i--; } else { otherRevealed++; i--; } } } displayAll(&testG, p); printf("----------------- After playAdventurer\n"); playAdventurer(&testG, p); displayDeck(&testG, p); // Check that deck is correct count = preDeck - otherRevealed - treasureDrawn; printf("Deck count: %d, Expected: %d\n", testG.deckCount[p], count); if (testG.deckCount[p] != count) { printf("----------------- TEST FAILED!\n"); pass = false; } displayDiscard(&testG, p); // Check that discard is correct count = preDiscard + otherRevealed; printf("Discard count: %d, Expected: %d\n", testG.discardCount[p], count); if (testG.discardCount[p] != count) { printf("----------------- TEST FAILED!\n"); pass = false; } displayHand(&testG, p); // Check that count of cards is correct count = preHand + treasureDrawn; printf("Hand count: %d, Expected: %d\n", testG.handCount[p], count); if (testG.handCount[p] != count) { printf("----------------- TEST FAILED!\n"); pass = false; } } if (pass) { printf("\nAll tests passed!\n"); } else { printf("\nSome test(s) failed!\n"); } return 0; }