ask a favor to correct this project

Requirement :
Create a standard deck of 52 playing cards and list those cards on the screen, columns makes my life easier :-) Pause or "press Enter" after the unshuffled deck is displayed. The display might look something like.
"Shuffle" the deck and list the shuffled deck on the screen. random_shuffle from <algorithm> will work on a vector of Cards
Sort (by rank) and evaluate evaluate the first 5 cards of the deck as a 'hand'. Print the unsorted, then the sorted hand.
a pair (say the card rank, like Pair of Queens)
two pair (say something like Pair of Queens and Pair of Twos)
three of a kind (say something like Three Kings)
full house (say something like Full house, Jacks over fours)
straight (say Ace-high straight) A straight is all 5 cards in rank order, ace can be high or low
four of a kind
flush (say something like spades flush)
================================================
My works:


main.cpp

#include <iostream>
#include "Card.h"
#include <vector>
#include <algorithm>
#include <ctime>

using namespace std;

int main()
{
vector<int> all;
vector<int> hand;
vector<int> handrank;
vector<int> handsuit;
Card p;
int i,a,b,h;
for(i=0; i<52; i++)
{
p.setRank(i);
p.getRank();
p.setSuit(i);
p.getSuit();
p.toStringrank();
p.toStringsuit();
if(i%13==12)
{
p.print();
cout << endl;
}
else p.print();
}
cout << endl << endl;
for(a=0; a<52; a++)
{
all.push_back(a);
}
srand ( time(0) );
random_shuffle ( all.begin(), all.end() );
for(b=0; b<52; b++)
{
p.setRank(all[b]);
p.getRank();
p.setSuit(all[b]);
p.getSuit();
p.toStringrank();
p.toStringsuit();
if(b%13==12)
{
p.print();
cout << endl;
}
else p.print();
}
cout << endl << endl;
for(b=0; b<5; b++)
{
hand.push_back(all[b]);
p.setRank(all[b]);
p.getRank();
p.setSuit(all[b]);
p.getSuit();
p.toStringrank();
p.toStringsuit();
p.print();
cout << " ";
handrank.push_back((hand[b])%13+2);
}
cout << endl << endl;
for(int m=0; m<4; m++)
{
for(int n=m+1; n<5; n++)
{
if(handrank.at(m)>handrank[n])
{
int f =handrank[m];
handrank[m]=handrank[n];
handrank[n]=f;
int k =hand[m];
hand[m]=hand[n];
hand[n]=k;
}

}
}
for(b=0; b<5; b++)
{
p.setRank(hand[b]);
p.getRank();
p.setSuit(hand[b]);
p.getSuit();
p.toStringrank();
p.toStringsuit();
p.print();
cout << " ";
handsuit.push_back(hand[b]/13);
}
cout << endl << endl;

if((handrank[0]==handrank[1]&&handrank[1]!=handrank[2]&&handrank[2]!=handrank[3]&&handrank[3]!=handrank[4])||
(handrank[1]==handrank[2]&&handrank[0]!=handrank[1]&&handrank[2]!=handrank[3]&&handrank[3]!=handrank[4])||
(handrank[2]==handrank[3]&&handrank[0]!=handrank[1]&&handrank[1]!=handrank[2]&&handrank[3]!=handrank[4])||
(handrank[3]==handrank[4]&&handrank[0]!=handrank[1]&&handrank[1]!=handrank[2]&&handrank[2]!=handrank[3]))
cout << "a pair" << endl;
if((handrank[0]==handrank[1]&&handrank[2]==handrank[3]&&handrank[1]!=handrank[2]&&handrank[3]!=handrank[4])||
(handrank[0]==handrank[1]&&handrank[3]==handrank[4]&&handrank[1]!=handrank[2]&&handrank[2]!=handrank[3])||
(handrank[1]==handrank[2]&&handrank[3]==handrank[4]&&handrank[0]!=handrank[1]&&handrank[2]!=handrank[3]))
cout << "two pair" << endl;
if((handrank[0]==handrank[1]&&handrank[1]==handrank[2]&&handrank[2]!=handrank[3]&&handrank[3]!=handrank[4])||
(handrank[1]==handrank[2]&&handrank[2]==handrank[3]&&handrank[0]!=handrank[1]&&handrank[3]!=handrank[4])||
(handrank[2]==handrank[3]&&handrank[3]==handrank[4]&&handrank[0]!=handrank[1]&&handrank[1]!=handrank[2]))
cout << "three of a kind" << endl;
if((handrank[0]==handrank[1]&&handrank[1]==handrank[2]&&handrank[2]!=handrank[3]&&handrank[3]==handrank[4])||
(handrank[2]==handrank[3]&&handrank[3]==handrank[4]&&handrank[0]==handrank[1]&&handrank[1]!=handrank[2]))
cout << "full house" << endl;
if((handrank[0]+1==handrank[1]&&handrank[1]+1==handrank[2]&&handrank[2]+1==handrank[3]&&handrank[3]+1==handrank[4])||
(handrank[0]==14&&handrank[1]==2&&handrank[2]==3&&handrank[3]==4&&handrank[3]==5))
cout << "straight" << endl;
if((handrank[0]==handrank[1]&&handrank[1]==handrank[2]&&handrank[2]==handrank[3]&&handrank[3]!=handrank[4])||
(handrank[1]==handrank[2]&&handrank[2]==handrank[3]&&handrank[3]==handrank[4]&&handrank[0]!=handrank[1]))
cout << "four of a kind" << endl;
if(handsuit[0]==handsuit[1]&&handrank[1]==handsuit[2]&&handrank[2]==handsuit[3]&&handrank[3]==handsuit[4])
cout << "flush" << endl;

return 0;
}
================

card.h

#include <iostream>

using namespace std;

class Card
{
public:
Card();

// set and get the rank, 2=2, 3=3, ... 10=10, 11=J, 12=Q, 13=K, 14=A
void setRank(int r);
int getRank(){return myRank;}

// set and get the suit, 1=Spades, 2=Hearts, ...
void setSuit(int r);
int getSuit(){return mySuit;}

// for debugging, print the suit and rand as integers
void print();
void toVector();
// return the rank and suit as a 'nice' looking string
string toStringrank();
string toStringsuit();

// if you want to use sort() in the STL
bool operator<(const Card &c) const {return myRank < c.myRank;}

private:
int myRank;
int mySuit;
string ranks;
string suits;
};

#endif // CARD_H

===========================
card.cpp

#include "Card.h"
#include <string>
#include <iostream>

using namespace std;

Card::Card()
{
//ctor
myRank;
mySuit;
}
void Card::setRank(int r)
{
myRank = r%13;
}
void Card::setSuit(int r)
{
mySuit = r/13;
}
string Card::toStringrank()
{
if(myRank==0)
ranks= "2";
else if(myRank==1)
ranks= "3";
else if(myRank==2)
ranks= "4";
else if(myRank==3)
ranks= "5";
else if(myRank==4)
ranks= "6";
else if(myRank==5)
ranks= "7";
else if(myRank==6)
ranks= "8";
else if(myRank==7)
ranks= "9";
else if(myRank==8)
ranks= "10";
else if(myRank==9)
ranks= "J";
else if(myRank==10)
ranks= "Q";
else if(myRank==11)
ranks= "K";
else if(myRank==12)
ranks= "A";
return ranks;
}
string Card::toStringsuit()
{
if (mySuit==0)
suits= "C";
else if (mySuit==1)
suits= "H";
else if (mySuit==2)
suits= "D";
else if (mySuit==3)
suits= "S";
return suits;
}
void Card::print()
{
cout<< "[" << suits << "]" << ranks << " ";
}


Topic archived. No new replies allowed.