473,785 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 3145
YarrOfDoom
1,247 Recognized Expert Top Contributor
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
mProgramz
2 New Member
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 Recognized Expert Moderator Expert
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
2630
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 description: It is very similar to the card game Uno, though played with a standard deck of cards. An initial card is played off the top of the deck, and then each player has to play a card of the same suit, or the same face value in another suit....
138
6594
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 yeah,-that's-non-obvious dept. theodp writes "As if there weren't enough dodgy patents, here's an excerpt from one granted to Microsoft Tuesday for a 'Method and apparatus for displaying information regarding stored data in a gaming system': 'When saving a game,...
6
1637
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 between 1 and 9 is placed in each cell. * The user and computer take turns by "clicking" on table cells. When a cell is "clicked" by the user, the user scores the number of hidden points in the cell. When the computer takes a turn, it "clicks on"...
6
1891
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 and ask. Any help is really appreciated; this should be a piece of cake…driving me crazy. I am writing a simple program (from a book for fun) that creates a deck of cards, shuffles them, gives the user a card, and asks if the next card will be...
1
2328
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: player gets a choice to go first or second player picks first player can choose either 1 2 3 player picks 1 // computer now has 2 choices 2,3 computer picks 2 //player can now pick 1,2 or 3 again etc.
1
4660
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 1, 2, or 3 stones during their turn. I am kind of lost at the moment. I have several methods and I want to send several things between them. One is who is playing and the other is how many stones there are left. I am also not sure if I am just...
2
5316
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 managed to write a code for a single player (human -vs- computer) game. My problem is, now we want to merge the two games with options to pick which game you would like to play. However, the games were written totally different styles and I can't...
30
9138
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 with it. that will be great. thereis an attched file, to see the code more cleaer. from random import choice as randomcards def total(hand): # how many aces in the hand aces = hand.count(11) # to complicate things a little the...
1
8463
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 dice in a craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes “the point.” The...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.