Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include<iomanip>
- using namespace std;
- void shuffle(int[],int[]);
- void dealcards(int[],int[],int[],int[],int&);
- bool checkflush(int[]);
- bool checkstraight(int[]);
- void same(int [],bool &,bool &,bool &,bool &,bool &);
- void winner(int[],int[],int&);
- int main()
- {
- int i,j,flushes,decks[52],deckn[52],high=0,highowner=0;
- int hands[5],handn[5],scored,count,cards=0,score[4],winners[4],numwin=0;
- bool flush,straight;
- bool pair,pair2,three,four,nothing;
- string suit[4]={"hearts","diamonds","spades","clubs"};
- string card[13]={"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"};
- int deal;
- srand(time(0));
- scored=0;
- shuffle(decks,deckn);
- for(i=0;i<4;i++)
- {
- dealcards(hands,handn,decks,deckn,cards);
- cout<<"\nHand: "<<i+1<<"\n";
- for(j=0;j<5;j++)
- cout<<card[handn[j]]<<"\t"<<suit[hands[j]]<<endl;
- flush=checkflush(hands);
- straight=checkstraight(handn);
- same(handn,nothing,pair,pair2,three,four);
- if(handn[5]>high)
- {high=handn[i];
- highowner=i;
- }
- cout<<"scoring:\n";
- if(straight&&flush)
- {cout<<"straight flush\n";
- score[i]=1;
- }
- else if(pair&&three)
- {cout<<"full house\n";
- score[i]=3;
- }
- else if(straight)
- {cout<<"straight\n";
- score[i]=5;
- }
- else if(flush)
- {cout<<"flush\n";
- score[i]=4;
- }
- else if(four)
- {cout<<"four of a kind\n";
- score[i]=2;
- }
- else if(three)
- {cout<<"Three of a kind\n";
- score[i]=6;
- }
- else if(pair2)
- {cout<<"2 pair\n";
- score[i]=7;
- }
- else if(pair)
- {cout<<"one pair\n";
- score[i]=8;
- }
- else
- {cout<<"nothing\n";
- score[i]=10;
- }
- }
- for(i=0;i<4;i++)
- if(highowner==i&&score[i]==10)
- score[i]=9;
- cout<<"\nThe winner is player(s) ";
- winner(score,winners,numwin);
- for(i=0;i<numwin;i++)
- cout<<winners[i]+1<<" ";
- cout<<endl;
- system("pause");
- return 0;
- }
- void winner(int score[],int win[],int &w)
- {int i,winning=0;
- for(i=1;i<4;i++)
- if(score[i]<score[winning])
- {w=0;
- win[w++]=i;
- winning=i;
- }
- else if(score[i]==score[winning])
- win[w++]=i;
- }
- bool checkstraight(int handn[])
- {int i,j,t;
- for(i=0;i<4;i++)
- for(j=i+1;j<5;j++)
- if(handn[i]>handn[j])
- {t=handn[i];
- handn[i]=handn[j];
- handn[j]=t;
- }
- for(i=1;i<5;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 < 5; i++)
- {if(!used[handn[i]])
- {int count = 1;
- for(int j = i+1; j < 5; j++)
- {
- if(handn[j] == handn[i])
- {
- count++;
- }
- }
- used[handn[i]]=true;
- b[handn[i]]=count;
- }
- }
- cout<<endl;
- for(i=0;i<13;i++)
- if(b[i]==4)
- {four=true;
- return;
- }
- else if(b[i]==3)
- {three=true;
- for(j=0;j<13;j++)
- if(b[j]==2)
- pair=true;
- return;
- }
- else if(b[i]==2)
- {pair=true;
- for(j=0;j<13;j++)
- if(b[j]==3)
- three=true;
- for(j=i+1;j<13;j++)
- if(b[j]==2)
- {pair2=true;
- return;
- }
- return;
- }
- nothing=true;
- return;
- }
- void dealcards(int hands[],int handn[],int decks[],int deckn[],int & cards)
- {int i,j,t;
- int rank[]={13,2,3,4,5,6,7,8,9,10,11,12};
- for(i=0;i<5;i++)
- {hands[i]=decks[cards];
- handn[i]=deckn[cards];
- cards++;
- }
- for(i=0;i<4;i++)
- for(j=i+1;j<5;j++)
- if(rank[handn[i]]>rank[handn[j]])
- {t=hands[i];
- hands[i]=hands[j];
- hands[j]=t;
- t=handn[i];
- handn[i]=handn[j];
- handn[j]=t;
- }
- return;
- }
- bool checkflush(int hands[])
- {int j,count=0,type,handcount;
- type=hands[0];
- handcount=1;
- for(j=1;j<5;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<4;i++)
- for(j=0;j<13;j++)
- cards[i][j]=false;
- for(j=0;j<52;j++)
- {do
- {
- num=rand()%13;
- type=rand()%4;
- }while(cards[type][num]);
- decks[j]=type;
- deckn[j]=num;
- cards[type][num]=true;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement