Embaralhamento e distribuição de cartas. Modifique o programa que você desenvolveu no Exercício 18.10 para que ele trate de uma mão de pôquer de cinco cartas. Depois, escreva funções para realizar cada uma das tarefas a seguir:
a) Determinar se a mão contém um par.
b) Determinar se a mão contém dois pares.
c) Determinar se a mão contém três cartas iguais (por exemplo, três valetes)
. d) Determinar se a mão contém quatro cartas iguais (por exemplo, quatro ases).
e) Determinar se a mão contém um flush (ou seja, todas as cinco cartas do mesmo naipe).
f) Determinar se a mão contém um straight (ou seja, cinco cartas de valores consecutivos).
Passo 1
Fala Galera!!! Tudo certo? Espero que sim!! Bora para mais um exercício.
Nesse exercício vamos criar um programa que contenha duas classes Card e DeckOfCards com seus respectivos dados-membros e funções-membros que serão responsáveis em simular o embaralhamento e distribuição de cartas. Além disso nosso programa será capaz de exibir uma mão de pôquer com cincos cartas e determinar qual a pontuação da mão.
Vamos iniciar definindo todas as funções que serão responsáveis pelo funcionamento do jogo:
bool checkstraight(int handn[])
{int i,j,t;
for(i=0;i
for(j=i+1;j
if(handn[i]>handn[j])
{t=handn[i];
handn[i]=handn[j];
handn[j]=t;
}
for(i=1;i
if(handn[i]!=handn[i-1]+1)
return false;
return true;
}
void same(int handn[],bool ¬hing,bool &pair,bool &pair2,bool &three,bool &four)
{bool used[13]={false};
nothing=false;
pair=false;
pair2=false;
three=false;
four=false;
int b[13]={0};
int i,j;
for( i = 0; i
{if(!used[handn[i]])
{int count = 1;
for(int j = i+1; j
{
if(handn[j] == handn[i])
{
count++;
}
}
used[handn[i]]=true;
b[handn[i]]=count;
}
}
cout
for(i=0;i
if(b[i]==4)
{four=true;
return;
}
else if(b[i]==3)
{three=true;
for(j=0;j
if(b[j]==2)
pair=true;
return;
}
else if(b[i]==2)
{pair=true;
for(j=0;j
if(b[j]==3)
three=true;
for(j=i+1;j
if(b[j]==2)
{pair2=true;
return;
}
return;
}
nothing=true;
return;
}
void dealcards(int hands[],int handn[],int decks[],int deckn[])
{int i,j,cards=0;
for(i=0;i
{hands[i]=decks[cards];
handn[i]=deckn[cards];
cards++;
}
return;
}
bool checkflush(int hands[])
{int j,count=0,type,handcount;
type=hands[0];
handcount=1;
for(j=1;j
if(hands[j]==type)
handcount++;
if(handcount==5)
return true;
else
return false;
}
void shuffle(int decks[],int deckn[])
{bool cards[4][13];
int i,j,num,type;
for(i=0;i
for(j=0;j
cards[i][j]=false;
for(j=0;j
{do
{
num=rand()%13;
type=rand()%4;
}while(cards[type][num]);
decks[j]=type;
deckn[j]=num;
cards[type][num]=true;
}
return;
}
Passo 2
Em seguida vamos inicializar a função principal main() e fazer a chamada das funções:
int main()
{
//definindo variaveiz
int i,j,flushes,decks[52],deckn[52];
int hands[5],handn[5],scored,count;
bool flush,straight;
bool pair,pair2,three,four,nothing;
//matriz dos nipes
string suit[4]={"copa","paus","espada","ouro"};
//matriz das cartas
string card[13]={"As","2","3","4","5","6","7","8","9","10","valete","rainha","rei"};
int deal;
char yesno='Y';
srand(time(0));
do
{scored=0;
//embaralhando
shuffle(decks,deckn);
cout
for(j=0;j
cout
cout
dealcards(hands,handn,decks,deckn);
//exibir mao
cout
for(j=0;j
cout
cout
flush=checkflush(hands);
straight=checkstraight(handn);
same(handn,nothing,pair,pair2,three,four);
//mostrar pontuacao
cout
if(straight&&flush)
cout
else if(pair&&three)
cout
else if(straight)
cout
else if(flush)
cout
else if(four)
cout
else if(three)
cout
else if(pair2)
cout
else if(pair)
cout
else
cout
//jogar novamente
cout
cin>>yesno;
}while(toupper(yesno)=='Y');
system("pause");
return 0;
}
E assim finalizamos o exercício, agora é só executar e esperar a sorte!!!
Até o próximo exercício galera!!
Resposta
Código:
//cabeçalhos
#include
#include
#include
using namespace std;
void shuffle(int[],int[]);
void dealcards(int[],int[],int[],int[]);
bool checkflush(int[]);
bool checkstraight(int[]);
void same(int [],bool &,bool &,bool &,bool &,bool &);
int main()
{
//definindo variaveiz
int i,j,flushes,decks[52],deckn[52];
int hands[5],handn[5],scored,count;
bool flush,straight;
bool pair,pair2,three,four,nothing;
//matriz dos nipes
string suit[4]={"copa","paus","espada","ouro"};
//matriz das cartas
string card[13]={"As","2","3","4","5","6","7","8","9","10","valete","rainha","rei"};
int deal;
char yesno='Y';
srand(time(0));
do
{scored=0;
//embaralhando
shuffle(decks,deckn);
cout
for(j=0;j
cout
cout
dealcards(hands,handn,decks,deckn);
//exibir mao
cout
for(j=0;j
cout
cout
flush=checkflush(hands);
straight=checkstraight(handn);
same(handn,nothing,pair,pair2,three,four);
//mostrar pontuacao
cout
if(straight&&flush)
cout
else if(pair&&three)
cout
else if(straight)
cout
else if(flush)
cout
else if(four)
cout
else if(three)
cout
else if(pair2)
cout
else if(pair)
cout
else
cout
//jogar novamente
cout
cin>>yesno;
}while(toupper(yesno)=='Y');
system("pause");
return 0;
}
bool checkstraight(int handn[])
{int i,j,t;
for(i=0;i
for(j=i+1;j
if(handn[i]>handn[j])
{t=handn[i];
handn[i]=handn[j];
handn[j]=t;
}
for(i=1;i
if(handn[i]!=handn[i-1]+1)
return false;
return true;
}
void same(int handn[],bool ¬hing,bool &pair,bool &pair2,bool &three,bool &four)
{bool used[13]={false};
nothing=false;
pair=false;
pair2=false;
three=false;
four=false;
int b[13]={0};
int i,j;
for( i = 0; i
{if(!used[handn[i]])
{int count = 1;
for(int j = i+1; j
{
if(handn[j] == handn[i])
{
count++;
}
}
used[handn[i]]=true;
b[handn[i]]=count;
}
}
cout
for(i=0;i
if(b[i]==4)
{four=true;
return;
}
else if(b[i]==3)
{three=true;
for(j=0;j
if(b[j]==2)
pair=true;
return;
}
else if(b[i]==2)
{pair=true;
for(j=0;j
if(b[j]==3)
three=true;
for(j=i+1;j
if(b[j]==2)
{pair2=true;
return;
}
return;
}
nothing=true;
return;
}
void dealcards(int hands[],int handn[],int decks[],int deckn[])
{int i,j,cards=0;
for(i=0;i
{hands[i]=decks[cards];
handn[i]=deckn[cards];
cards++;
}
return;
}
bool checkflush(int hands[])
{int j,count=0,type,handcount;
type=hands[0];
handcount=1;
for(j=1;j
if(hands[j]==type)
handcount++;
if(handcount==5)
return true;
else
return false;
}
void shuffle(int decks[],int deckn[])
{bool cards[4][13];
int i,j,num,type;
for(i=0;i
for(j=0;j
cards[i][j]=false;
for(j=0;j
{do
{
num=rand()%13;
type=rand()%4;
}while(cards[type][num]);
decks[j]=type;
deckn[j]=num;
cards[type][num]=true;
}
return;
}