how do you write this in processing: #include <iostream.h> //needed for cin>> #i
ID: 3634209 • Letter: H
Question
how do you write this in processing:#include <iostream.h> //needed for cin>>
#include <stdlib.h> //needed for rand() and srand()
#include <time.h> //needed for time()
#include <string.h> //needed for strcpy()
#include <process.h> //needed for system("cls")
//Global variables
int p_ace=0, d_ace=0; //counter for how many aces have been dealt. Initialized to 0
int a=0; //index variable needed for card dealing
int cards[53]; //array filled with card values
char names2[53][25]; //Used to display name of card
char names[53][25] = { //array filled with card names. Used for dealing
"Zero",
"Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades",
"Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades",
"Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades",
"King of Spades",
"Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts",
"Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts",
"Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts",
"King of Hearts",
"Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs",
"Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs",
"Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs",
"King of Clubs",
"Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds",
"Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds",
"Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds",
"King of Diamonds"
};
//Function Prototypes
void rules();
void shuffle();
void shuffle2();
void init();
void display(int *, int*);
void input(char *);
void process(int *, int *, int*);
void exit(int *, int *, int *, int *);
void main()
{
int option;
shuffle2();
cout<<"Welcome To..."<<endl;
cout<<" BBBBBBBBBB LL A CCCCCCCCCCC K K ";
cout<<" B B LL A A C K K ";
cout<<" B B LL A A C K K ";
cout<<" B B LL A A C K K ";
cout<<" BBBBBBBBBB LL A A C KKK ";
cout<<" B B LL AAAAAAAAA C K K ";
cout<<" B B LL A A C K K ";
cout<<" B B LL A A C K K ";
cout<<" BBBBBBBBBB LLLLLLLLLLLL A A CCCCCCCCCCC K K ";
cout<<" ";
cout<<" JJJJJJJJJJJJ A CCCCCCCCCCC K K ";
cout<<" JJ A A C K K ";
cout<<" JJ A A C K K ";
cout<<" JJ A A C K K ";
cout<<" JJ AAAAAAAAA C KKK ";
cout<<" JJ A A C K K ";
cout<<" JJ JJ A A C K K ";
cout<<" JJ JJ A A C K K ";
cout<<" JJJJJJJJ A A CCCCCCCCCCC K K ";
cout<<endl;
cout<<endl;
cout<<"Press 1 To see the rules of BlackJack"<<endl;
cout<<"Press 2 To play BlackJack"<<endl;
cin>>option;
while((option!=1) && (option!=2))
{
cout<<endl;
cout<<"Sorry that is not an option. Please try again."<<endl;
cout<<endl;
cout<<"Press 1 To see the rules of BlackJack"<<endl;
cout<<"Press 2 To play BlackJack"<<endl;
cin>>option;
}
if (option==1)
{
rules();
}
else if(option==2)
{
system("cls");
shuffle();
init();
}
}
void rules() //displays the rules of blackjack
{
int option2;
system("cls");
cout<<"Here are the rules:"<<endl;
cout<<"A natural 21 means an automatic win."<<endl;
cout<<"If both players hand's are equal, then it's a tie."<<endl;
cout<<"The game is played with a standard 52 card deck. "<<endl;
cout<<"The deck re-shuffles after 52 cards have been dealt."<<endl;
cout<<"There is no doubling down or other complications."<<endl;
cout<<"J,Q,K all count as 10. An Ace can count as an 11 or a 1."<<endl;
cout<<endl;
cout<<"Press 1 to play BlackJack "<<endl;
cin>>option2;
while(option2!=1)
{
system("cls");
cout<<endl;
cout<<"Sorry that was not an option. Please try again."<<endl;
cout<<endl;
cout<<"Here are the rules:"<<endl;
cout<<"A natural 21 means an automatic win."<<endl;
cout<<"If both players hand's are equal, then it's a tie."<<endl;
cout<<"The game is played with a standard 52 card deck. "<<endl;
cout<<"The deck re-shuffles after 52 cards have been dealt."<<endl;
cout<<"J,Q,K all count as 10. An Ace can count as an 11 or a 1."<<endl;
cout<<endl;
cout<<"Press 1 to play BlackJack "<<endl;
cin>>option2;
}
system("cls");
shuffle();
init();
}
void init() //Deals two cards randomly to the players. And decides how the aces should be handled
{
static int test=0; //used in checking for natural blackjack
int p_card1, p_card2; //variables for cards dealt to player
int d_card1, d_card2; //variables for cards dealt to dealer
int p_total, d_total; //total value of dealer's and player's hands
int *p_test = &test;
if(a==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was just re-shuffled. "<<endl;
shuffle();
a=0;
}
a++;
if(a==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was just re-shuffled. "<<endl;
shuffle();
a=1;
}
p_card1=cards[a];
strcpy(names2[1],names[a]);
a++;
if(a==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was just re-shuffled. "<<endl;
shuffle();
a=1;
}
p_card2=cards[a];
strcpy(names2[2],names[a]);
a++;
p_total=p_card1 + p_card2; //Get the total of the players hand
if(a==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was just re-shuffled."<<endl;
shuffle();
a=1;
}
d_card1=cards[a];
strcpy(names2[3],names[a]);
a++;
if(a==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was just re-shuffled."<<endl;
shuffle();
a=1;
}
d_card2=cards[a];
strcpy(names2[4],names[a]);
d_total=d_card1 + d_card2; //Get the total of the dealers hand
if(p_card1==1) //If first card dealt is an ace, make it an 11
{
p_ace=1; //Add 1 to p_ace. Lets you know later on if any of the
p_card1=p_card1+10; //original two cards where an ace
p_total=p_card1 + p_card2;
}
if(p_card2==1) //If second card dealt is an ace, decide wheter to make it
{ //an 11 or 1
if(p_card1 != 11) //Make the second card an 11 only if the first card dealt is
{ //not already an 11
p_ace=1;
p_card2=p_card2+10;
p_total=p_card1 + p_card2;
}
else //Otherwise make it a 1
{
p_card2=1;
p_total=p_card1 + p_card2;
}
}
if(d_card1==1) //Now do it again for the dealers cards
{
d_ace=1;
d_card1=d_card1+10;
d_total=d_card1+d_card2;
}
if(d_card2==1)
{
if(d_card1 != 11)
{
d_ace=1;
d_card2=d_card2+10;
d_total=d_card1+d_card2;
}
else
{
d_card2=1;
d_total=d_card1+d_card2;
}
}
display(&p_total, &d_total) ;
process(p_test, &p_total, &d_total);
}
void display(int *p_p_total, int *p_d_total) //display players and dealers current hand
{
cout<<"You are dealt a "<<names2[1]<<" and a "<<names2[2]<<" for a total of "<<*p_p_total<<endl;
cout<<"Dealer is dealt a "<<names2[3]<<" and a "<<names2[4]<<" for a total of "<<*p_d_total<<endl;
}
void input(char *choice) //Asks player if he wants a card
{
cout<<"Would you like to Hit or Pass? (Press H or P)"<<endl;
cin>>*choice;
}
void process(int *test, int *p_total, int *d_total)
{
static char choice2;
static int lost=0, won=0, tied=0; //won/lost/tie record
int *p_lost=&lost;
int *p_won=&won;
int *p_tied=&tied;
char *p_choice=&choice2;
if(*test==0) //Checks to see if anyone has a natural blackjack. Only do this if test=0
{
if((*p_total==21) && (*d_total!=21)) //If player has a natural 21 and the dealer
{ //doesnt, he automatically wins
//display(&p_total, &d_total) ;
cout<<"You have BlackJack! You win!"<<endl;
won=won+1;
exit(test, p_won, p_lost, p_tied);
}
else if((*d_total==21) && (*p_total!=21)) //If dealer has a natural 21 and the player
{ //doesnt, dealer automatically wins
//display(&p_total, &d_total) ;
cout<<"Dealer has BlackJack! You Lose."<<endl;
lost=lost+1;
exit(test, p_won, p_lost, p_tied);
}
else if((*d_total==21) && (*p_total==21)) //If both the player and dealer have a natural 21,
{ //its a tie
//display(&p_total, &d_total) ;
cout<<"You both have BlackJack! It's a tie!"<<endl;
tied=tied+1;
exit(test, p_won, p_lost, p_tied);
}
else //Otherwise show player the current hands and goto input() function to see if
{ //he wants another card
*test=1; //Make test a 1 so it doesnt check for natural blackjack again
//display(&p_total, &d_total);
input(p_choice);
}
}
if(((choice2 == 'H') || (choice2 == 'h')) && (*test==1)) //check to see if player entered H or h
{
a++;
if(a==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was just re-shuffled."<<endl;
shuffle();
a=1;
}
if(cards[a]==1) //If card dealt is an ace, decide wheter it should be a 1
{ //or 11 for the player.
if(*p_total<=10) //If his current hand is under 11 make the ace an 11 and
{ //add one to p_ace
p_ace=1;
*p_total=*p_total+11;
}
else //Otherwise make it a 1
{
*p_total=*p_total + 1;
}
}
else //card dealt is not an ace, just add it to his total
*p_total= *p_total + cards[a];
if((*p_total>=22) && (p_ace==1)) //If player is over 21 but has an ace that is an
{ //11, make that ace into a one. Subtract 1 from
*p_total= *p_total-10; //p_ace to make sure it only does this once.
p_ace=0;
}
if(*p_total>=22) //If player is over 21 he loses and dealer wins
{
cout<<"You are dealt a "<<names[a]<<" for a total of "<<*p_total<<endl;
cout<<"You are over. Dealer wins."<<endl;
lost=lost+1;
exit(test, p_won, p_lost, p_tied);
}
else if(*p_total==21) //If player has 21 he automaticaly wins.
{
cout<<"You are dealt a "<<names[a]<<" for a total of "<<*p_total<<endl;
cout<<"You have BlackJack! You win!"<<endl;
won=won+1;
exit(test, p_won, p_lost, p_tied);
}
else if(*p_total<=20) //If player has 20 or less, let him decide if he
{ //wants to hit again
cout<<"You are dealt a "<<names[a]<<" for a total of "<<*p_total<<endl;
input(p_choice);
process(test, p_total, d_total);
}
}
else if(((choice2 =='P') || (choice2 == 'p')) && (*test==1)) //checks to see if player entered P or p
{
while(*d_total<=16) //Keep getting cards as long as dealer has a 16 or less
{
a++;
if(a==53) //checks to see if all cards have been dealt and if so, reshuffle deck
{
cout<<"Deck was just re-shuffled."<<endl;
shuffle();
a=1;
}
if(cards[a]==1) //If card dealt is an ace, decide wheter it should be a
{ //1 or 11 for the dealer.
if(*d_total<=10) //If his current hand is under 11 make the ace an 11 and
{ //add one to d_ace
d_ace=1;
*d_total=*d_total+11;
}
else //Otherwise make it a 1
{
*d_total= *d_total + 1;
}
}
else //card dealt is not an ace, just add it to his total
*d_total= *d_total + cards[a];
if((*d_total>=22) && (d_ace==1)) //If dealer is over 21 but has an ace that is an
{ //11, make that ace into a one. Subtract 1 from
*d_total= *d_total-10; //d_ace to make sure it only does this once.
d_ace=0;
}
cout<<"Dealer is dealt a "<<names[a]<<" for a total of "<<*d_total<<endl;
} //end of while statement
if(*d_total>=22) //Checks to see if dealer has gone over. If he has, player wins.
{
cout<<"Dealer has a total of "<<*d_total<<endl;
cout<<"Dealer is over. You win!"<<endl;
won=won+1;
exit(test, p_won, p_lost, p_tied);
}
else if(*d_total==21) //Checks to see if dealer has blackjack. If so, player
{ //automaticaly loses
cout<<"Dealer has BlackJack. You lose."<<endl;
lost=lost+1;
exit(test, p_won, p_lost, p_tied);
}
else if((*d_total>*p_total) && (*d_total<=20)) //Dealer wins if his hand is higher than
{ //players
cout<<"Dealer stands with a "<<*d_total<<endl;
cout<<"Dealer wins"<<endl;
lost=lost + 1;
exit(test, p_won, p_lost, p_tied);
}
else if(*d_total==*p_total) //If dealer and player have same hand, it's a tie
{
cout<<"Dealer stands with a "<<*d_total<<endl;
cout<<"You tied"<<endl;
tied=tied + 1;
exit(test, p_won, p_lost, p_tied);
}
else if(*d_total<*p_total) //If player has a higher hand, he wins
{
cout<<"Dealer stands with a "<<*d_total<<endl;
cout<<"You win!"<<endl;
won=won + 1;
exit(test, p_won, p_lost, p_tied);
}
}
//if they enter something other than a H, h, P, or p let them try again
else if((choice2 !='P') && (choice2 != 'p') && (*test==1) && (choice2 !='H') && (choice2 != 'h') && (choice2 !=''))
{
cout<<"Sorry that is not an option. Please try again."<<endl;
input(p_choice);
process(test, p_total, d_total);
}
}
void exit(int *test, int *won, int *lost, int *tied) //Go here once hand is over
{
char choice4;
cout<<endl;
cout<<"Would you like to play again? (Press Y or N)"<<endl; //Ask player if they want to keep
cin>>choice4; //playing
while((choice4!='Y') && (choice4!='y') && (choice4!='N') && (choice4!='n'))
{
cout<<endl;
cout<<"Sorry that is not an option. Please try again."<<endl;
cin>>choice4;
}
if((choice4 == 'Y') || (choice4 == 'y')) //If yes, start over again
{
cout<<endl;
*test=0; //reset test to 0 so it checks for natural blackjack
p_ace=0; //Reinitialize these as 0. Otherwise, if a person was dealt an ace and it
d_ace=0; //didnt get turned into a one, the program will subtract 10 form their total
init();
}
else if((choice4 == 'N') || (choice4 == 'n')) //If player quits show him his w/l/t record
{
cout<<endl;
cout<<"Thank you for playing."<<endl;
cout<<"Your record was "<<*won<<" wins "<<*lost<<" losses and "<<*tied<<" ties."<<endl;
cout<<endl;
}
}
void shuffle() //shuffles the arrays into a random order
{
int j, k, temp;
char temp2[25];
srand((unsigned) time(NULL)); //Seeds rand()
j = 52;
while(j > 0) //algorithm that puts the card values and names in random order
{
k = rand() % j +1; //set k to a random # between 1 and the current value of j
temp = cards[j];
strcpy(temp2,names[j]);
cards[j] = cards[k];
strcpy(names[j],names[k]);
cards[k] = temp;
strcpy(names[k],temp2);
j--;
}
//So what happens basically is that it shuffles the deck by taking a
//card and switching it with a random card. It does this for all 52
//cards.
}
void shuffle2() //fills 52 card array with values of cards
{
int index, l;
//Spades
for(index=1; index<=10; index++) //Fills an array with values of the cards Ace-10. Here Ace is 1.
{
cards[index]=index;
}
cards[11]=10; //Values for Jack Queen King
cards[12]=10;
cards[13]=10;
l=1;
//Hearts
for(index=14; index<=23; index++) //Fills an array with values of the cards Ace-10. Here Ace is 1.
{
cards[index]=l;
l++;
}
cards[24]=10; //Values for Jack Queen King
cards[25]=10;
cards[26]=10;
l=1;
//Clubs
for(index=27; index<=36; index++) //Fills an array with values of the cards Ace-10. Here Ace is 1.
{
cards[index]=l;
l++;
}
cards[37]=10; //Values for Jack Queen King
cards[38]=10;
cards[39]=10;
l=1;
//Diamonds
for(index=40; index<=49; index++) //Fills an array with values of the cards Ace-10. Here Ace is 1.
{
cards[index]=l;
l++;
}
cards[50]=10; //Values for Jack Queen King
cards[51]=10;
cards[52]=10;
}
http://processing.org/
http://www.learningprocessing.com/
Explanation / Answer
#include 002 #include 003 #include 004 #include 005 006 int p_ace=0, d_ace=0; 007 int a=0; 008 int cards[53]; 009 char names2[53][25]; 010 char names[53][25] = { //this is for filling the arrays with the names of the cards 011 "Zero", "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", 012 "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", 013 "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades", 014 015 "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", 016 "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", 017 "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts", 018 019 "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", 020 "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", 021 "Jack of Clubs", "Queen of Clubs", "King of Clubs", 022 023 "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", 024 "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", 025 "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds" 026 }; 027 028 void rules(); 029 void shuffle(); 030 void shuffle2(); 031 void init(); 032 void display(int *, int*); 033 void input(char *); 034 void process(int *, int *, int*); 035 void exit(int *, int *, int *, int *); 036 int menu(); 037 void welcome(); 038 int option; 039 040 void welcome() 041 { 042 std::coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.