Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. /*
  8. * Blackjack game, generates two random cards for player to reach goal of 21
  9. * total.
  10. */
  11. int main () {
  12.  
  13. int total;
  14. srand (time(NULL));//to get random numbers
  15.  
  16. char again = 'y';//initially yes so the loop runs the first time
  17.  
  18. // while the player wants to play, continue this loop
  19. while (again == 'y'){
  20.  
  21. int firstCard = rand() % 10 + 1;
  22. int secondCard = rand() % 10 + 1;
  23.  
  24. cout << "First cards: " << firstCard <<", "<< secondCard << endl;
  25. total = firstCard+secondCard;
  26. cout << "Total: "<< total << endl;
  27.  
  28. char cont = 'y';
  29. cout << "Do you want another card? (y/n): " << endl;
  30. cin >> cont;//sets player's response to the cont variable
  31.  
  32. //if player wants to draw more, generate random card
  33. while (cont == 'y'){
  34.  
  35. int card = rand() % 10 + 1;
  36. cout << "New Card:" << card << endl;
  37. total = card + total;
  38. cout << "Total: " << total << endl;
  39.  
  40. //allows more draws if total is under 21
  41. if (total < 21){
  42. cout << "Do you want another card? (y/n) " << endl;
  43. cin >> cont;
  44. }
  45.  
  46. //if total is over 21, bust and end current game. ask if continue
  47. else if (total > 21) {
  48. cout << "BUST." << endl;
  49. cout << "Do you want to play again? (y/n) " << endl;
  50. cin >> again;
  51. cont = 'n';
  52. }
  53. //if total is 21, wins and ends current game. ask if continue
  54. else if (total == 21) {
  55. cout << "Winner!" << endl;
  56. cout << "Do you want to play again?" << endl;
  57. cin >> again;
  58. cont = 'n';
  59. }
  60.  
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement