1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <stdio.h>
#include<iostream>//required for cout
#include<windows.h>//required for rand
#include<time.h>//required for time
#include<conio.h>
using namespace std;
//function prototypes
void hit(int array[],int deck, int &q);
void shuffle(int array[],int startPoint,int endPoint);
void Color(int color)
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,color);
}
int Card(int array[],int x)
{
//change color and output card values based on numeric value pulled from deck
if (array[x]<27)
Color(12);
else
Color(8);
int temp=x;//use temp to translate values 1-52 into 1-13 for each suit
cout<<((array[x]<14) ? "\3" : (array[x]>13 && array[x]<27) ? "\4" :
(array[x]>26 && array[x]<40) ? "\5" : "\6");
temp=((array[x]>39) ? array[x]-39 : (array[x]>26 && array[x]<40) ?
array[x]-26 : (array[x]<27 && array[x]>13) ? array[x]-13 : array[x]);
if (temp<11 && temp!=1)
cout<<temp<<" ";//just print the value unless it is 1 or above 10
else//output A,J,Q, or K based on value
cout<<((temp==11) ? "J" : (temp==12) ? "Q" : (temp==13) ? "K" : "A")<<" ";
cout<<((temp==10) ? "" : " ");//add additional space for single digit values
Color(7);
//apply blackjack rules to card value
//deal with face cards
if (temp>10)
temp=10;
//deal with aces in main to get total card run
//returns cards value when desired as well as outputting the card itself
return temp;
}
int main()
{
int array[52]={0};//array of cards
int deck=0;//tracks position of draw
int psum=0,csum=0,rounds=0,play=1,q=2;
int split=26;//tracks where deck splits
srand(time(NULL));//set random # seed
//fill deck with values 1-52
for (int i=0;i<52;i++)
array[i]=i+1;
shuffle(array,0,51);//randomized shuffle of whole deck
//blackjack hand sample
//output initial cards and assign values
cout<<"Your cards\tTotal\n";
int temp=Card(array,deck),temp2=Card(array,deck+2);
//alters ace's value to reflect blackjack rules
//aces to 11 when sum is under 12
temp=((temp==1 && temp+temp2<12) ? 11 : temp);
temp2=((temp2==1 && temp+temp2<12) ? 11 : temp2);
//output sum of card values
cout<<"\t "<<temp+temp2<<endl;
//output house's first card
cout<<"House cards\n";
int temp0=Card(array,deck+1);
cout<<" ?\t\t ?\n";
if (temp+temp2==21)
{
cout<<"\nBlackjack!";
}
else
{
cout<<"Would you like to hit, or stand(H/S):";
int c=getchar();
if (c=='h' || c=='H')
{
hit(array,deck,q);
}
else if (c=='s' || c=='S')
{
cout<<"House cards\n";
int temp3=Card(array,deck+1);
int temp0=Card(array,deck+3);
cout<<"\t "<<temp0+temp3<<endl;
}
}
}//end of main
void shuffle(int array[],int startPoint,int endPoint)
{
for (int i=startPoint;i<endPoint+1;i++)
{
int temp=array[i];//use temp to hold i's value
int temp2=rand()%(endPoint+1);//use temp2 to randomize where to swap with
array[i]=array[temp2];//copy from randomized position to i's position
array[temp2]=temp;//replace randomized position with temp's value
}//repeat loop until cards between start and endpoint are shuffled
}
void hit (int array[],int deck, int &q)
{
//q is the quantity of cards in play
q++;
//start with repeat of initial cards
cout<<"Your cards\t Total\n";
int temp=Card(array,deck),temp2=Card(array,deck+2);
int temp3=Card(array,deck+q);
//alters ace's value to reflect blackjack rules
//aces to 11 when sum is under 12
temp=((temp==1 && temp+temp2+temp3<12) ? 11 : temp);
temp2=((temp2==1 && temp+temp2+temp3<12) ? 11 : temp2);
temp3=((temp3==1 && temp+temp2+temp3<12) ? 11 : temp3);
cout<<"\t "<<temp+temp2+temp3<<endl;
//output house's first card
cout<<"House cards\n";
int temp0=Card(array,deck+1);
if (temp+temp2+temp3<21)
cout<<" ?\t\t ?\n";
else
{
int tmp=Card(array,deck+q+1);
cout<<"\t "<<temp0+tmp<<endl;
}
}
|