VB icon

blackjack 21: console game

Email
Submitted on: 1/1/2015 3:58:00 AM
By: Anonymous (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: C++ (general)
Views: 965
 
     BLACKJACK VS THE COMP, My code probably isn't that great, but it works...
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: blackjack 21: console game
// Description:BLACKJACK VS THE COMP, My code probably isn't that great, but it works...
// By: Anonymous (from psc cd)
//**************************************

//This game simulates a simplified version of the card game 21
#include <iostream.h>//include neccesary libraries
#include <stdlib.h>
#include <time.h>
int you1,you2,you3, c1, c2, c3,ans,you,comp,ans2,youpoint,comppoint,drawpoint; 
//you1,2,3 are random #'s for player, C1,2,3 are random #'s for computer
// ans is # of cards, you and comp are total scores, youpoint compoint and drawpoint are total points for each
char yesno;//used to loop program
int main()
{
yesno='y';
comppoint=0;
youpoint=0;
drawpoint=0;//declares variable starting point
cout << "Ryan Fogarty" << endl << "Lab 21 Game" <<endl<<"Nov, 11, 1999"<<endl<<"This Program Simlulates the Card Game 21\n";
while ((yesno=='y')||(yesno=='Y'))// loop 
{
	cout <<"How many cards do you want?\n";
	cin >> ans;
	srand (time(NULL));// generates random #'s for comp and player
	you1 =(rand()%(10)) +1; //from 1 to 10
	you2 =(rand()%(10)) +1; //from 1 to 10
	you3 =(rand()%(10)) +1; //from 1 to 10
	c1 =(rand()%(10)) +1; //from 1 to 10
	c2 =(rand()%(10)) +1; //from 1 to 10
	c3=(rand()%(10)) +1; //from 1 to 10 
	if (ans==1)
		{
		cout <<"You: "<< you1 <<endl;
		cout <<"Computer: "<<c1<<" "<<c2<<" "<<c3<<endl;
		you = you1 ;
		comp = c1 + c2 + c3;
		}
	else if (ans==2)
		{
		cout <<"You: "<< you1 <<" "<<you2<<endl;
		cout <<"Computer: "<<c1<<" "<<c2<<" "<<c3<<endl;
		you = you1 + you2;
		comp = c1 + c2 + c3;
		}
	else if (ans==3)
		{
		cout <<"You: "<< you1 <<" "<<you2<<" "<<you3<<endl;
		cout <<"Computer: "<<c1<<" "<<c2<<" "<<c3<<endl;
		you = you1 + you2 +you3;
		comp = c1 + c2 + c3;
		}//if statement used to tell how many cards were chosen
		if ((you > comp)&&(you<=21)||(comp>21))
			{
			cout <<"I have " <<comp<<" and you have "<<you<<" so you win."<<endl;
			youpoint++;
			}
		else if ((comp > you)&&(comp<=21)||(you>21))
			{
			cout <<"I have " <<comp<<" and you have "<<you<<" so I win."<<endl;
			comppoint++;
			}
		else if ((comp == you))
			{
			cout <<"I have " <<comp<<" and you have "<<you<<" so we draw."<<endl;
			drawpoint++;
			}// statement tells who won and adds on total points
		cout <<"Would you like to play again? (Y/N)?";//to restart loop or not
		cin >> yesno;
		cout << endl;
	}
	cout << "\nComputer Wins = "<<comppoint<<endl;
	cout << "Your Wins = "<< youpoint<<endl;
	cout << "Draws = "<< drawpoint<< endl;// shows total amount of points
	return 0;
}


Other 21 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 Intermediate 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

6/23/2016 8:42:23 AMmailman

i like the code, nice job
(If this comment was disrespectful, please report it.)

 

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.