Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- /*
- * Blackjack game, generates two random cards for player to reach goal of 21
- * total.
- */
- int main () {
- int total;
- srand (time(NULL));//to get random numbers
- char again = 'y';//initially yes so the loop runs the first time
- // while the player wants to play, continue this loop
- while (again == 'y'){
- int firstCard = rand() % 10 + 1;
- int secondCard = rand() % 10 + 1;
- cout << "First cards: " << firstCard <<", "<< secondCard << endl;
- total = firstCard+secondCard;
- cout << "Total: "<< total << endl;
- char cont = 'y';
- cout << "Do you want another card? (y/n): " << endl;
- cin >> cont;//sets player's response to the cont variable
- //if player wants to draw more, generate random card
- while (cont == 'y'){
- int card = rand() % 10 + 1;
- cout << "New Card:" << card << endl;
- total = card + total;
- cout << "Total: " << total << endl;
- //allows more draws if total is under 21
- if (total < 21){
- cout << "Do you want another card? (y/n) " << endl;
- cin >> cont;
- }
- //if total is over 21, bust and end current game. ask if continue
- else if (total > 21) {
- cout << "BUST." << endl;
- cout << "Do you want to play again? (y/n) " << endl;
- cin >> again;
- cont = 'n';
- }
- //if total is 21, wins and ends current game. ask if continue
- else if (total == 21) {
- cout << "Winner!" << endl;
- cout << "Do you want to play again?" << endl;
- cin >> again;
- cont = 'n';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement