473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

game of 21, person vs. computer. first closest to 21 without going over wins help fin

Expand|Select|Wrap|Line Numbers
  1. //Specification: This program plays a version of
  2. //the card game of 21.
  3. //A human player is pitted against the computer.
  4. //The player who is the closest to 21 without
  5. //going over wins the hand. 
  6. #include <iostream>
  7. #include <ctime>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. //prototypes...
  13. void play21(void);
  14. int dealCards(int, string);
  15. void hit(int &);
  16. void determineWinner(int, int);
  17. int Random(int, int);
  18.  
  19.  
  20. void main(){
  21.  
  22.        char keepPlaying = 'n'; //loop control variable
  23.  
  24.        do {
  25.               play21();
  26.  
  27.               //keep playing?
  28.              cout << "Do you want to play anouther hand (y/n)?";
  29.              cin >> keepPlaying;
  30.      } while(keepPlaying == 'Y' || keepPlaying == 'y');
  31. }
  32.  
  33. void play21(void){
  34.         //play one hand of 21
  35.  
  36.         //randomize the cards
  37.         srand((int) time(0));
  38.  
  39.        // deal the cards
  40.          int person = dealCards(2, "Your Cards:");
  41.          cout << " = " << person << endl;
  42.          int house = dealCards(2, "Computers Cards:");
  43.          cout << " = " << house << endl;
  44.  
  45.         // Ask if human wants a hit and keep hitting...
  46.         hit(person);
  47.         cout << endl;
  48.  
  49.        //Determine if computer takes a hit
  50.        while ((house < person) && (house <= 21) && (person <= 21)) {
  51.                house += dealCards(1, "The Computer takes a card ");
  52.                cout << endl;
  53.         }
  54.  
  55.        //show who won....
  56.        determineWinner(person, house);
  57. }
  58.  
  59. void determineWinner(int humanScore, int houseScore) {
  60. //Compare the scores to see who won
  61. //Both the human and the house score totals are provided as arguments
  62. //Display total scores and indicate winner
  63. //possible outcomes: human wins, computer wins, tie
  64.  
  65. }
  66.  
  67. int dealCards(int numberOfCards, string message){
  68. //This function deals the cards
  69. //The number of cards to be dealt is provided as an argument
  70. //A message indicating which player is receiving the cards is also
  71. //given as an argument
  72. //The player message and the cards dealt is displayed to the screen
  73. //the total value of the dealt cards  is returned
  74.  
  75. }
  76.  
  77. void hit(int &playerScore){
  78. //This function asks the human if they want another card -- 'a hit'
  79. //the player's score total is accumulated as they take cards
  80. //the player can continue taking cards until they wish to stop or they exceed 21
  81. //After a card is taken (use the dealCards function) the user's current total is displayed
  82. //If the user goes over 21 'busted' is displayed
  83.  
  84. }
  85.  
  86. int Random(int lowerLimit, int upperLimit) {
  87. //returns a random number within the given boundary
  88.          return 1 + rand() % (upperLimit - lowerLimit + 1);
  89. }
Nov 20 '09 #1
3 3120
YarrOfDoom
1,247 Expert 1GB
I'm sorry, but what exactly is your question?

Also, when you're posting code, you can use code-tags (put [code] in front of your code, and [/code] behind it, and it will be nicely formatted). Other guidelines like this and rules can be found in the faq.
Nov 20 '09 #2
Expand|Select|Wrap|Line Numbers
  1. //Specification: This program plays a version of 
  2. //the card game of 21. 
  3. //A human player is pitted against the computer. 
  4. //The player who is the closest to 21 without 
  5. //going over wins the hand.  
  6. #include <iostream> 
  7. #include <ctime> 
  8. #include <string> 
  9.  
  10. using namespace std; 
  11.  
  12. //prototypes... 
  13. void play21(void); 
  14. int dealCards(int, string); 
  15. void hit(int &); 
  16. void determineWinner(int, int); 
  17. int Random(int, int); 
  18.  
  19.  
  20. void main(){ 
  21.  
  22.        char keepPlaying = 'n'; //loop control variable 
  23.  
  24.        do { 
  25.               play21(); 
  26.  
  27.               //keep playing? 
  28.              cout << "Do you want to play anouther hand (y/n)?"; 
  29.              cin >> keepPlaying; 
  30.      } while(keepPlaying == 'Y' || keepPlaying == 'y'); 
  31.  
  32. void play21(void){ 
  33.         //play one hand of 21 
  34.  
  35.         //randomize the cards 
  36.         srand((int) time(0)); 
  37.  
  38.        // deal the cards 
  39.          int person = dealCards(2, "Your Cards:"); 
  40.          cout << " = " << person << endl; 
  41.          int house = dealCards(2, "Computers Cards:"); 
  42.          cout << " = " << house << endl; 
  43.  
  44.         // Ask if human wants a hit and keep hitting... 
  45.         hit(person); 
  46.         cout << endl; 
  47.  
  48.        //Determine if computer takes a hit 
  49.        while ((house < person) && (house <= 21) && (person <= 21)) { 
  50.                house += dealCards(1, "The Computer takes a card "); 
  51.                cout << endl; 
  52.         } 
  53.  
  54.        //show who won.... 
  55.        determineWinner(person, house); 
  56.  
  57. void determineWinner(int humanScore, int houseScore) { 
  58. //Compare the scores to see who won 
  59. //Both the human and the house score totals are provided as arguments 
  60. //Display total scores and indicate winner 
  61. //possible outcomes: human wins, computer wins, tie 
  62.  
  63.  
  64. int dealCards(int numberOfCards, string message){ 
  65. //This function deals the cards 
  66. //The number of cards to be dealt is provided as an argument 
  67. //A message indicating which player is receiving the cards is also 
  68. //given as an argument 
  69. //The player message and the cards dealt is displayed to the screen 
  70. //the total value of the dealt cards  is returned 
  71.  
  72.  
  73. void hit(int &playerScore){ 
  74. //This function asks the human if they want another card -- 'a hit' 
  75. //the player's score total is accumulated as they take cards 
  76. //the player can continue taking cards until they wish to stop or they exceed 21 
  77. //After a card is taken (use the dealCards function) the user's current total is displayed 
  78. //If the user goes over 21 'busted' is displayed 
  79.  
  80.  
  81. int Random(int lowerLimit, int upperLimit) { 
  82. //returns a random number within the given boundary 
  83.          return 1 + rand() % (upperLimit - lowerLimit + 1); 
  84.  
Nov 23 '09 #3
Banfa
9,065 Expert Mod 8TB
You have still not asked a question!

If you are having a prblem with the post code then you need to either post any compiler and linker errors you are getting or post a description of the runtime errors including, expected behaviour, actual behaviour, input data, output.

Additionally you need to have the courtesy to actually ask for the help you require.
Nov 23 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Sam | last post by:
Hey all, I want to create a computerised version of this game, though I'm not really sure how to go about it. For those who don't know how the game works, here's my attempt at a brief...
138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
6
by: jar13861 | last post by:
Create a game in a 3x3 HTML table that works as follows: * The goal of the game is to beat the computer by scoring more points than the computer. * The game starts when a hidden random number...
6
by: JNeko | last post by:
Hello all, awesome site! I guess I am technically not a beginner in JAVA, but from my code you would not realize it! I don’t expect anyone to help me with this, but I figure I might as well as try...
1
by: HighBuddha | last post by:
Hello, i have a question about game trees. I've been given an assignment in class (highschool) and the assignment is to write a program the simulates The Game Of Eight. The game runs like this: ...
1
by: Leiram | last post by:
I am trying to write a game where there is 13 stones and you play against the computer to make sure that you don't take the last stone. You or the computer (depending on the turn) is allowed to take...
2
by: LilMeechc20 | last post by:
Hello, I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I...
30
by: imran akhtar | last post by:
i have a balckjack code, which does not seem to run, in python, it comes up with syntax error, i have try sortng it out. does not seem to work. below is my code, if anyone can work out wht wrong...
1
by: Shark2026 | last post by:
Hi there I need to make a Craps game for my class. Here are the parameters for it. In the game of craps, a pass line bet proceeds as follows. Two six-sided dice are rolled; the first roll of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.