0

Hi all,
I am just new to progamming .I am writng small twenty card game.What I want I dont repeat the cards again to next player after first user received.And in real I wana do game with more than two player.But in my code have some problem with print out total score that who win or lose (only two player is ok).I can get total score by comparing two player. But I want to compare with more than two players.How can I do with my code.
here is my simple code.. Thanks in advance.:sad:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
void display();
void intro();
int your_total(int urcard);
int comp_total(int compcard);
struct{
int face;
unsigned int value;
} 
card[13] = 
{	{'2', 2},{'2', 3},{'2', 4},
    {'5', 5},{'6', 6},{'7', 7},
    {'8', 8},{'9', 9},{'X', 10},
    {'J', 13},{'Q', 12},{'K', 11},
    {'A', 1}
};
char suit[4]={'D','C','H','S'};

int main()
{
intro();
display();
return 0;
}
void intro()
{
	cout<<"simple twenty one card"<<endl;
	cout<<"------------------------"<<endl;
	cout<<"Total number gotta cut out upon number21!"<<endl;
	cout<<"But u can have only 5 cards in maximum"<<endl;
	cout<<"<TenX=10,King=11,Queen=12,Jack=13,Ace=1>"<<endl<<endl;
}
int your_total(int urcard)
{
	int urtotal=0;
	srand((unsigned)time(0));
	cout<<"How many cards u want?(up to 5 cards)"<<endl;
	cin>>urcard;

		if (urcard>5)
		cout<<"You could not get more than five cards!"<<endl;
		else
		{
		cout<<"     Ur card"<<endl;
				for (int ucard=0;ucard<urcard;ucard++)
				{
					int x = rand() % 13;
					char d;
					d=rand()%4;
					cout<<" |"<<suit[d];
					printf("%c| ", card[x].face);
					urtotal+=card[x].value;
				if(urtotal>42)
				urtotal=urtotal-30;
				else if(urtotal>31&&urtotal<42)
				urtotal=urtotal-20;
				else if(urtotal>21&&urtotal<32)
				urtotal=urtotal-10;
				}
		}
		
		return urtotal;
		
}
int comp_total(int compcard)
{
			srand((unsigned)time(0));
			
			cout<<endl<<"How many cards you want computer?"<<endl;
			int lowest=2, highest=5; 
			int range=(highest-lowest)+1;
			int total_num=0;
					for(int index=0; index<1; index++){ 
					compcard= lowest+int(range*rand()/(RAND_MAX + 1.0)); 
					cout <<compcard<<endl;
					}
			cout<<"     My cards"<<endl;
			  for (int Ccard=0;Ccard<compcard;Ccard++)
			  {
				 	int x = rand() % 13;
				char d;
				d=rand()%4;
				cout<<" |"<<suit[d];
				printf("%c| ", card[x].face);
				total_num+=card[x].value;
				if(total_num>42)
				total_num=total_num-30;
				else if(total_num>31&&total_num<42)
				total_num=total_num-20;
				else if(total_num>21&&total_num<32)
				total_num=total_num-10;
				
		     }
			return total_num;
		  

}
void display()
{
	int comptotal, urtotal,person=0,computer=0, compscore=0,urscore=0,draw=0;
	const int high=21;
	char cond;
	//int ur;
	do{
	
		//srand(time(NULL));
		srand((unsigned)time(0));
		urtotal=your_total(person);
		comptotal=comp_total(computer);
		
		cout<<endl<<"I have "<<comptotal<<" and you have "<<urtotal<<" so ";
		if(comptotal<=high&& urtotal<comptotal)
		{
			cout <<"I win! "<<endl;
			compscore++;
		}
		else if(comptotal<=high&& urtotal>high)
		{
			cout <<"I win! "<<endl;
			compscore++;
		}
		else if (urtotal<=high&& urtotal>comptotal)
		{
		cout<<" you win!"<<endl;
		urscore++;
		}
		else if (urtotal<=high&& comptotal>high)
		{
		cout<<" you win"<<endl;
		urscore++;
		}
		else //(comptotal==urtotal)
		{
		cout<<"no win"<<endl;
		draw++;
		}
		cout<<endl<<"Wana play again(n/y?)"<<endl;
		cin>>cond;
	}while(cond=='y');
	cout<<"I win: "<<compscore<<endl<<"U win: "<<urscore<<endl<<"Draw : "<<draw<<endl;
	//cin>>ur;

}
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Salem
0

card deck[52]; Where card is a type containing 'rank' and 'suit' to specify an individual card.

Create functions which work on this array to
- initialise: from 2 of clubs to ace of spades in linear order
- shuffle: to randomise the deck
- deal: take the next card

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.