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
|
#include "basic.h"
void War(int array[],int &player,int &computer, int &split);
void playerShuffle(int array[],int startPoint,int endPoint);
void Card(int array[],int x)
{
if (array[x]<27)
Color(12);//assign red to lower values for hearts/diamonds
else
Color(8);//assign dark text to higher values for clubs/spades
int temp=x;//output suit symbols based on value
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)//subtract as needed to get true value
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);
}
int main()
{
int array[52]={0};//array of cards
int player=25,computer=26;//tracks starting positions of players
int psum=0,csum=0,rounds=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";
if (split==0 || split==52)
break;
if (player<0)
{
playerShuffle(array,0,split-1);
player=split-1;
cout<<"You shuffled your cards.\n";
}
if (computer>51)
{
playerShuffle(array,split,51);
computer=split;
cout<<"I shuffled my cards.\n";
}
cout<<"Push a key to play another round.";
getch();
cout<<endl;
rounds++;//count each round played
}
cout<<((split!=0) ? "I " : "You ")<<"ran out of cards.\n";
cout<<((split==0) ? "I " : "You ")<<"win!\n";
cout<<"We played "<<rounds<<" rounds.\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";
cout<<"Your cards: \tTotal:\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<<"My cards:\n";
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";
}
void playerShuffle(int array[],int startPoint,int endPoint)
{
for (int i=startPoint;i<endPoint+1;i++)
{
int temp=array[i];//use temp to hold value being swapped
int temp2=rand()%(endPoint+1);//randomize where to swap with
array[i]=array[temp2];//copy from randomized position
array[temp2]=temp;//replace randomized position with temp's value
}//repeat loop until player's cards are shuffled
}
|