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
|
void War(int array[],int &player,int &computer, int &split);
void shuffle(int array[])
{
int x=1;int y=0;
array[0]=rand()%52+1;//first number assigned
do
{
array[x]=rand()%52+1;//value generated for x's position
y=0;
while (y<x)
{//iterate through array while checking for duplicate values
if (array[y]==array[x])
{//if duplicate is found, generate another value for this position
--x;
break;
}
++y;//increment y to check additional array positions
}
++x;//increment x to assign values to additional array positions
} while (x<52);//continue loop until whole deck is assigned
}
void Color(int color)
{
//sample usage: Color(7); (7 is default color)
HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,color);
}
void Card(int array[],int x)
{
if (array[x]<27)
Color(12);
else
Color(8);
int temp=x;
cout<<((array[x]<14) ? "\3" : (array[x]>13 && array[x]<27) ? "\4" : (array[x]>26 && array[x]<40) ? "\5" : "\6");
array[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 (array[temp]<11 && array[temp]!=1)
cout<<array[temp]<<" ";//just print the value unless it is 1 or above 10
else//output A,J,Q, or K based on value
cout<<((array[temp]==11) ? "J" : (array[temp]==12) ? "Q" : (array[temp]==13) ? "K" : "A")<<" ";
cout<<((array[temp]==10) ? "" : " ");//add additional space for single digit values
Color(7);
}
int main()
{
int array[52]={0};//array of cards
int player=0,computer=26;//tracks starting positions of players
int psum=0,csum=0;
int split=26;//tracks where deck splits
srand(time(NULL));//set random # seed
shuffle(array);//randomized shuffle
while (split!=0 && split<52)
{
cout<<"Your card:\tValue:\n ";
Card(array,player);
psum=((array[player]>39) ? array[player]-39 : (array[player]>13 && array[player]<27) ? array[player]-13 :
(array[player]>26 && array[player]<40) ? array[player]-26 : array[player]);
player++;
cout<<"\t\t "<<psum<<endl;
cout<<"My card:\n ";
Card(array,computer);
csum=((array[computer]>39) ? array[computer]-39 : (array[computer]>13 && array[computer]<27) ? array[computer]-13 :
(array[computer]>26 && array[computer]<40) ? array[computer]-26 : array[computer]);
computer++;
cout<<"\t\t "<<csum<<endl;
cout<<((psum>csum) ? "You " : (csum>psum) ? "I " : "");
cout<<((psum!=csum) ? "won this round.\n" : "");
split=((psum>csum) ? split+1 : (csum>psum) ? split-1 : split);
if (psum==csum)
War(array,player,computer,split);
cout<<"You now have "<<split<<" cards.\n";
cout<<"Push a key to play another round.";
getch();
cout<<endl;
}
cout<<((split!=0) ? "I " : "You ")<<"ran out of cards.\n";
cout<<((split==0) ? "I " : "You ")<<"win!\n";
}//end of main
void War(int array[],int &player,int &computer, int &split)
{
int psum=0;int csum=0;//re-initialization
cout<<"\nWAR!!!!!!!!!!!!!!!!!\n";
for (int i=0;i<3;i++)
{
Card(array,player);
psum=((array[player]>39) ? psum+array[player]-39 : (array[player]>13 && array[player]<27) ?
psum+array[player]-13 : (array[player]>26 && array[player]<40) ? psum+array[player]-26 : psum+array[player]);
player++;
}
cout<<"\t "<<psum<<endl;
for (int i=0;i<3;i++)
{
Card(array,computer);
csum=((array[computer]>39) ? csum+array[computer]-39 : (array[computer]>13 && array[computer]<27) ?
csum+array[computer]-13 : (array[computer]>26 && array[computer]<40) ? csum+array[computer]-26 : csum+array[computer]);
computer++;
}
cout<<"\t "<<csum<<endl;
cout<<((psum>csum) ? "You " : (csum>psum) ? "I " : "");
cout<<((psum!=csum)? "won this battle.\n" : "");
split=((psum>csum) ? split+3 : (csum>psum) ? split-3 : split);
if (psum==csum)
cout<<"The battle was a draw.\n";
}
|