473,888 Members | 1,528 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>
  10. using namespace std;
  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);
  20. void main(){
  22.        char keepPlaying = 'n'; //loop control variable
  24.        do {
  25.               play21();
  27.               //keep playing?
  28.              cout << "Do you want to play anouther hand (y/n)?";
  29.              cin >> keepPlaying;
  30.      } while(keepPlaying == 'Y' || keepPlaying == 'y');
  31. }
  33. void play21(void){
  34.         //play one hand of 21
  36.         //randomize the cards
  37.         srand((int) time(0));
  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;
  45.         // Ask if human wants a hit and keep hitting...
  46.         hit(person);
  47.         cout << endl;
  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.         }
  55.        //show who won....
  56.        determineWinner(person, house);
  57. }
  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
  65. }
  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
  75. }
  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
  84. }
  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 3149
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
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> 
  10. using namespace std; 
  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); 
  20. void main(){ 
  22.        char keepPlaying = 'n'; //loop control variable 
  24.        do { 
  25.               play21(); 
  27.               //keep playing? 
  28.              cout << "Do you want to play anouther hand (y/n)?"; 
  29.              cin >> keepPlaying; 
  30.      } while(keepPlaying == 'Y' || keepPlaying == 'y'); 
  32. void play21(void){ 
  33.         //play one hand of 21 
  35.         //randomize the cards 
  36.         srand((int) time(0)); 
  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; 
  44.         // Ask if human wants a hit and keep hitting... 
  45.         hit(person); 
  46.         cout << endl; 
  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.         } 
  54.        //show who won.... 
  55.        determineWinner(person, house); 
  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 
  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 
  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 
  81. int Random(int lowerLimit, int upperLimit) { 
  82. //returns a random number within the given boundary 
  83.          return 1 + rand() % (upperLimit - lowerLimit + 1); 
Nov 23 '09 #3
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

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....
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,...
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"...
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...
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.
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...
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...
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...
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...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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,...
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...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
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.