VB icon

Twenty-One Card Game

Submitted on: 1/4/2015 3:01:00 PM
By: Morgan L.D. (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C++ (general)
Views: 820
     This is a re-creation of the card game Twenty-One. It is quite simple.


INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//INCLUDE files for :Twenty-One Card Game
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
// Name: Twenty-One Card Game
// Description:This is a re-creation of the card game Twenty-One. It is quite simple.
// By: Morgan L.D. (from psc cd)
// Inputs:Number of cards wanted and if you want to continue play
// Side Effects:Make sure to delete the character 'y' in the next input line when you type 'y' to continue play

//Chapter 4, Exercise 33 of "A Guide to Programming in C++"
//(c)Morgan L.D.
//November 18, 2002
//Created with Dev C++ Programmer
//*Note* - Include files may vary among programmers
//This is a card game similar to Twenty-One
#include <iostream.h>
#include <stdlib.h>
#include <lvp\conio.h>
#include <lvp\random.h>
void main()
 cout<<"--Twenty-One Card Gamer--"<<endl<<endl;
 int CardsComp=3, ScoreComp=0, ScoreYou=0, Draw=0, CardsYou, Continue;
 int Cards1=0, YouTot=0, CompTot=0, Cards=0;
 const int TO=21;//highest winning value
 while(1==1)//runs loop until its broken
 cout<<"How many cards do you want? ";
 cout<<"You: ";
 for(int CardsY=1; CardsY<=CardsYou; CardsY++)
 Cards=random(10)+1;//generates random cards set to specific #
 cout<<Cards<<" ";
 YouTot+=Cards;//totals cards
 cout<<"Computer: ";
 for(int CardsC=1; CardsC<=3; CardsC++)
 Cards1=random(10)+1; //generates random cards
 cout<<Cards1<<" ";
 CompTot+=Cards1;//totals cards
 cout<<"I have "<<CompTot<<" and you have "<<YouTot<<" so ";
 //below decides winning statement to output
 if(CompTot<=TO && CompTot>YouTot)
{ cout<<"I win"<<endl;
 else if(YouTot<=TO && YouTot>CompTot)
{ cout<<"you win"<<endl;
 else if(CompTot<=TO && YouTot>TO)
{ cout<<"I win"<<endl;
 else if(YouTot<=TO && CompTot>TO)
{ cout<<"you win"<<endl;
 else if(CompTot==YouTot)
{ cout<<"we draw"<<endl;
 CompTot=0;//clears totals so they don't carry on in the...
 YouTot=0; //next loop
 cout<<"Would you like to play again? (Y/N)? "; //loop program again
 if(Continue=='n' || Continue=='N')
break;//breaks loop if continue is true
 cout<<"Computer wins = "<<ScoreComp<<endl;//end of program
 cout<<"Your wins = "<<ScoreYou<<endl;//chart
 cout<<"Draws = "<<Draw<<endl;

Other 1 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.