1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
else if (Choice == 2) { // choice from main menu
int Player1win = 0;
int Player1lose = 0;
int Player1tie = 0;
int Player2win = 0;
int Player2lose = 0;
int Player2tie = 0;
// Ask for user name
cout << "Please enter player 1 first name?: "; // output display
cin >> Player; // input from display
cout << "Please enter player 2 first name?: "; // output display
cin >> Player2; // input from display
// Loop once for each hand
while (true) {
// "Shuffle" the cards; set them all to undealt
Shuffle(CardDeck);
// Deal the hands. Get two cards for each
PlayerHand[0] = GetNextCard(CardDeck); // Get first player 1 random card, run function
Player2Hand[0] = GetNextCard(CardDeck); // Get first player 2 random card, run function
PlayerHand[1] = GetNextCard(CardDeck); // Get second player 1 random card, run function
Player2Hand[1] = GetNextCard(CardDeck); // Get second player 2 random card, run function
Player1CardCount = 2; // starting number of cards for player 1
Player2CardCount = 2; // Starting number of cards for player 2
// Signal a new hand.
cout << "-------------------------New Hand-------------------------" << endl;
char PlayerChoice; // letter to pick hit or stay for player 1
char Player2Choice; // letter to pick hit or stay for player 2
bool PlayerHits = true; // if player wants to hit
int PlayerScore = ScoreHand(PlayerHand, Player1CardCount); // run player score
// Get Player's Hits. Calculate the score and redisplay after each hit.
do {
// Print the dealt cards
cout << Player << "'s Hand: Score = " << ScoreHand(PlayerHand, Player1CardCount) << endl;
PrintHand(PlayerHand, Player1CardCount);
cout << endl;
cout << Player2 << "'s Hand: Score = " << ScoreHand(Player2Hand, Player2CardCount) << endl;
PrintHand(Player2Hand, Player2CardCount);
// Ask the Player 1 if he wants a hit or to stay
cout << Player <<", Hit(h) or Stay(s)?: ";
cin >> PlayerChoice;
if (PlayerChoice == 'h') {
PlayerHand[PlayerCardCount] = GetNextCard(CardDeck);
++Player1CardCount;
} else if (PlayerChoice == 's') {
PlayerHits = false;
} else {
cout << "Error" << endl;
}
cout << endl;
// Get the Player's current score to update and check for bust.
PlayerScore = ScoreHand(PlayerHand, Player1CardCount);
} while (PlayerHits && PlayerScore < 22);
// Once the player 1 is done taking hits, check whether he busted
if (PlayerScore > 21) {
// The Player busted. The Player 2 wins.
cout << Player2 << " Wins!" << endl;
mPrintScoresAndHands(Player2Hand, Player2CardCount, PlayerHand, Player1CardCount, Player, WhatNow, Player2);
else {
// If the player didn't bust, Ask if Player2 wants a hit or to stay
cout << Player2 <<", Hit(h) or Stay(s)?: ";
cin >> Player2Choice;
if (Player2Choice == 'h') {
Player2Hand[Player2CardCount] = GetNextCard(CardDeck);
++Player2CardCount;
} else if (Player2Choice == 's') {
PlayerHits = false;
} else {
cout << "Error" << endl; //display error
}
}
cout << endl;
Player2Score = ScoreHand(Player2Hand, Player2CardCount);
} while (PlayerHits && Player2Score < 22);
if (Player2Score > 21) {// if player 2 is greater than 21, bust
// The player2 busted. Player 1 wins
cout << endl << "***WINNER!!! " << Player << " Winssss!" << endl;
mPrintScoresAndHands(Player2Hand, Player2CardCount, PlayerHand, Player1CardCount, Player, WhatNow, Player2); // Run function, print end hand
else {
// Compare scores and determine the winner
if (PlayerScore == Player2Score) {
// Tie, if scores are equal
cout << endl << "***WINNER!!! " << "The Game is a Tie!" << endl;
mPrintScoresAndHands(Player2Hand, Player2CardCount, PlayerHand, Player1CardCount, Player, WhatNow, Player2); // Run function, print end hand
} else if (PlayerScore > Player2Score) {
// The Player 1 wins, if player 1's score is greater than player 2
cout << endl << "***WINNER!!! " << Player << " Winss!" << endl;
mPrintScoresAndHands(Player2Hand, Player2CardCount, PlayerHand, Player1CardCount, Player, WhatNow, Player2); // Run function, print end hand
} else { //else player 2 must be greater then player 1
// The Player 2 wins
cout << endl << "***WINNER!!! " << Player2 << " Wins!" << endl;
mPrintScoresAndHands(Player2Hand, Player2CardCount, PlayerHand, Player1CardCount, Player, WhatNow, Player2); // Run function, print end hand
}
}
}
wins << Player << " Wins: " << Player1win << " Ties: " << Player1tie << " Lose: " << Player1lose << endl;
wins << Player2 << " Wins: " << Player2win << " Ties: " << Player2tie << " Lose: " << Player2lose << endl;
|