Jump to content

C++ Poker Logic


Recommended Posts

So for my c++ class, we have to write a program that creates a deck of cards, shuffles it, deals a hand, and determine your highest hand. I've decided to use a vector (deck) of class object pointers (Card). I can create it, shuffle it, and deal it, and am not 'stuck' persay, but am 'stuck' at the logic part. I can obviously program it all from scratch, but was wondering if anyone has ever written any, or seen any poker logic code. I'm still googling as I post this, so don't just suggest that. Also, whether I get help or not, I'll complete this project, so please don't go all `nO homewurk halp nOOb!!11!`.

 

Thanks!

 

Josh.

 

(ps, here is the source, which apparently can't be attatched. I was gonna rename it to like poker.cpp.php... but didn't wanna annoy the mods :) ).

 

also, I'm compiling with g++ thru terminal on snow leopard.

 


#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Card{
    
    private:
        
        //holder variables
        
        string  suit;
        int     number;
        bool    dealt;
        
        //print variables
        string  word;
        
    public:
        
        //setters
        
        void setSuit(string input){     suit=input;     }//end setSuit
        
        void setNumber(int input){      number=input;   }//end setNumber
        
        void setDealt(bool input){      dealt=input;    }//end setDealt
        
        //getters
        
        string getSuit(){               return suit;    }//end getSuit
        
        int getNumber(){                return number;  }//end getNumber
        
        bool getDealt(){                return dealt;   }//end getDealt
        
        void printCard(){
            
            switch(number){
                
                case 1:     word="Ace";   break;
                case 2:     word="One";   break; //!! OOPS, long night, killed later...
                case 3:     word="Two";   break;
                case 4:     word="Three";   break;
                case 5:     word="Four";   break;
                case 6:     word="Five";   break;
                case 7:     word="Six";   break;
                case 8:     word="Seven";   break;
                case 9:     word="Eight";   break;
                case 10:    word="Nine";   break;
                case 11:    word="Ten";   break;
                case 12:    word="Jack";   break;
                case 13:    word="Queen";   break;
                case 14:    word="King";   break;
                    
            }//end switch
            
            cout << word << " of " << suit << "\t";
            
        }//end printCard
    
};//end Card

bool checkHiCard(vector<Card>& hand);
    
bool checkPair(vector<Card>& hand);

bool checkTwoPair(vector<Card>& hand);
   
bool checkThreeKind(vector<Card>& hand);
   
bool checkStraight(vector<Card>& hand);
   
bool checkFlush(vector<Card>& hand);
   
bool checkFullHouse(vector<Card>& hand);
   
bool checkFourKind(vector<Card>& hand);
   
bool checkStraightFlush(vector<Card>& hand);
   
bool checkRoyalFlush(vector<Card>& hand);

int main(){
    
    vector<Card> deck;
      
    //cout << deck[0].getNumber();
    
    string  buildSuit;
    int     buildNumber;
    bool    buildDealt;
    
    for(int i=1,j=1,k=1; i<57; i++,k++){
        
        switch(i){
            
            case 15:    j=2;    k=1;    break;  //make suit spades
            
            case 29:    j=3;    k=1;    break;  //make suit diamonds
                
            case 43:    j=4;    k=1;    break;  //make suit clubs
            
        }//end switch
        
        switch(j){
            
            case 1: buildSuit="Hearts";     break;
                
            case 2: buildSuit="Spades";     break;
                
            case 3: buildSuit="Diamonds";   break;
                
            case 4: buildSuit="Clubs";      break;
            
        }//end switch
        
        buildNumber=    k;
        buildDealt=     false;
        
        Card *card;
    
        card= new Card;
        
        card->setSuit(buildSuit);
        card->setNumber(buildNumber);
        card->setDealt(buildDealt);
        
        if(k!=2){ //OMG, 1 of anything doesn't exist T_T
            
            deck.push_back(*card);
            
        }//end if
        
    }//end for
    
    srand(time(NULL));
    
    random_shuffle(deck.begin(), deck.end()); //shuffle the deck 
    
    cout << endl;
    
    for(int l=0,n=1; l<52; l++,n++){
        
        cout << l+1 << ". ";
        
        deck[l].printCard();
        
        
        if(n==3){
            
            n=0;
            
            cout << endl;
            
        }//end if
        
    }//end for
    
    cout << endl;
    
    vector<Card> yourHand;
    
    cout << "\nYou've Been Dealt:\n\n";
    
    for(int m=15,o=1; m<20; m++,o++){
        
        //process hand
        
        buildSuit=      deck[m].getSuit();
        buildNumber=    deck[m].getNumber();
        buildDealt=     true;
        
        Card *card;
    
        card= new Card;
        
        card->setSuit(buildSuit);
        card->setNumber(buildNumber);
        card->setDealt(buildDealt);
        
        yourHand.push_back(*card);
        
        //display hand
        
        cout << o << ". ";
        
        deck[m].printCard();
        
        cout << endl;
        
    }//end for
    
    string yourResult;
    
    if(checkHiCard(yourHand)){          yourResult="A Hi Card";         }//end if
    
    if(checkPair(yourHand)){            yourResult="A Pair";            }//end if
    
    if(checkTwoPair(yourHand)){         yourResult="Two Pair";          }//end if
       
    if(checkThreeKind(yourHand)){       yourResult="Three Of A Kind";   }//end if
       
    if(checkStraight(yourHand)){        yourResult="A Straight";        }//end if
       
    if(checkFlush(yourHand)){           yourResult="A Flush";           }//end if
       
    if(checkFullHouse(yourHand)){       yourResult="A Full House";      }//end if
       
    if(checkFourKind(yourHand)){        yourResult="Four Of A Kind";    }//end if
       
    if(checkStraightFlush(yourHand)){   yourResult="A Flush";           }//end if
       
    if(checkRoyalFlush(yourHand)){      yourResult="A Royal Flush!!";   }//end if
    
    cout << "\nYou Have... " << yourResult << "!!!\n";
    
    cout << endl;
    
    return 0;
    
}//end main

//process hand vector

bool checkHiCard(vector<Card>& hand){
    
    return true;
        
}//end function

bool checkPair(vector<Card>& hand){
    
    return true;
        
}//end function

bool checkTwoPair(vector<Card>& hand){
    
    return true;
        
}//end function
   
bool checkThreeKind(vector<Card>& hand){
    
    return true;
        
}//end function
   
bool checkStraight(vector<Card>& hand){
    
    return true;
        
}//end function
   
bool checkFlush(vector<Card>& hand){
    
    if((hand[0].getSuit()==hand[1].getSuit())
       &&
       (hand[0].getSuit()==hand[2].getSuit())
       &&
       (hand[0].getSuit()==hand[3].getSuit())
       &&
       (hand[0].getSuit()==hand[4].getSuit())
      ){
       
        return true;
    
    }else{
        
        return false;
    
    }//end if
    
}//end function
   
bool checkFullHouse(vector<Card>& hand){
    
    return true;
        
}//end function
   
bool checkFourKind(vector<Card>& hand){
    
    return true;
        
}//end function
   
bool checkStraightFlush(vector<Card>& hand){
    
    return true;
        
}//end function
   
bool checkRoyalFlush(vector<Card>& hand){
    
    return true;
        
}//end function


Link to comment
Share on other sites

Update: So I now have all but the full house and 2 pair logic done, and my brain is tired. So I figured I'd see if anyone had any thoughts for direction on these two.

 

Thanks, Josh

 


#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Card{
    
    private:
        
        //holder variables
        
        string  suit;
        int     number;
        bool    dealt;
        
        //print variables
        string  word;
        
    public:
        
        //setters
        
        void setSuit(string input){     suit=input;     }//end setSuit
        
        void setNumber(int input){      number=input;   }//end setNumber
        
        void setDealt(bool input){      dealt=input;    }//end setDealt
        
        //getters
        
        string getSuit(){               return suit;    }//end getSuit
        
        int getNumber(){                return number;  }//end getNumber
        
        bool getDealt(){                return dealt;   }//end getDealt
        
        void printCard(){
            
            switch(number){
                
                case 1:     word="Ace";   break;
                case 2:     word="One";   break; //!! OOPS, long night, killed later...
                case 3:     word="Two";   break;
                case 4:     word="Three";   break;
                case 5:     word="Four";   break;
                case 6:     word="Five";   break;
                case 7:     word="Six";   break;
                case 8:     word="Seven";   break;
                case 9:     word="Eight";   break;
                case 10:    word="Nine";   break;
                case 11:    word="Ten";   break;
                case 12:    word="Jack";   break;
                case 13:    word="Queen";   break;
                case 14:    word="King";   break;
                    
            }//end switch
            
            cout << word << " of " << suit << "\t";
            
        }//end printCard
    
};//end Card

bool checkHiCard(vector<Card>& hand);
    
bool checkPair(vector<Card>& hand);

bool checkTwoPair(vector<Card>& hand);
   
bool checkThreeKind(vector<Card>& hand);
   
bool checkStraight(vector<Card>& hand);
   
bool checkFlush(vector<Card>& hand);
   
bool checkFullHouse(vector<Card>& hand);
   
bool checkFourKind(vector<Card>& hand);
   
bool checkStraightFlush(vector<Card>& hand);
   
bool checkRoyalFlush(vector<Card>& hand);

int findLeast(int a[], int start, int end);
void swap(int a[], int pos1, int pos2);
void selectionSort(int a[], int s);

int main(){
    
    vector<Card> deck;
      
    //cout << deck[0].getNumber();
    
    string  buildSuit;
    int     buildNumber;
    bool    buildDealt;
    
    for(int i=1,j=1,k=1; i<57; i++,k++){
        
        switch(i){
            
            case 15:    j=2;    k=1;    break;  //make suit spades
            
            case 29:    j=3;    k=1;    break;  //make suit diamonds
                
            case 43:    j=4;    k=1;    break;  //make suit clubs
            
        }//end switch
        
        switch(j){
            
            case 1: buildSuit="Hearts";     break;
                
            case 2: buildSuit="Spades";     break;
                
            case 3: buildSuit="Diamonds";   break;
                
            case 4: buildSuit="Clubs";      break;
            
        }//end switch
        
        buildNumber=    k;
        buildDealt=     false;
        
        Card *card;
    
        card= new Card;
        
        card->setSuit(buildSuit);
        card->setNumber(buildNumber);
        card->setDealt(buildDealt);
        
        if(k!=2){ //OMG, 1 of anything doesn't exist T_T
            
            deck.push_back(*card);
            
        }//end if
        
    }//end for
    
    srand(time(NULL));
    
    random_shuffle(deck.begin(), deck.end()); //shuffle the deck 
    
    /*******************/
    /*  FOR DEBUGGING  */
    /*******************/
     
     
     deck[15].setNumber(11);
     deck[16].setNumber(12);
     deck[17].setNumber(13);
     deck[18].setNumber(14);
     deck[19].setNumber(2);
     
     
     
     deck[15].setSuit("Spades");
     deck[16].setSuit("Spades");
     deck[17].setSuit("Spades");
     deck[18].setSuit("Spades");
     deck[19].setSuit("Spades");
     
     
    /*******************/
    
    cout << endl;
    
    for(int l=0,n=1; l<52; l++,n++){
        
        cout << l+1 << ". ";
        
        deck[l].printCard();
        
        
        if(n==3){
            
            n=0;
            
            cout << endl;
            
        }//end if
        
    }//end for
    
    cout << endl;
    
    vector<Card> yourHand;
    
    cout << "\nYou've Been Dealt:\n\n";
    
    for(int m=15,o=1; m<20; m++,o++){
        
        //process hand
        
        buildSuit=      deck[m].getSuit();
        buildNumber=    deck[m].getNumber();
        buildDealt=     true;
        
        Card *card;
    
        card= new Card;
        
        card->setSuit(buildSuit);
        card->setNumber(buildNumber);
        card->setDealt(buildDealt);
        
        yourHand.push_back(*card);
        
        //display hand
        
        cout << o << ". ";
        
        deck[m].printCard();
        
        cout << endl;
        
    }//end for
    
    string yourResult;
    
    if(checkHiCard(yourHand)){          yourResult="A Hi Card";         }//end if
    
    if(checkPair(yourHand)){            yourResult="A Pair";            }//end if
    
    if(checkTwoPair(yourHand)){         yourResult="Two Pair";          }//end if
       
    if(checkThreeKind(yourHand)){       yourResult="Three Of A Kind";   }//end if
       
    if(checkStraight(yourHand)){        yourResult="A Straight";        }//end if
       
    if(checkFlush(yourHand)){           yourResult="A Flush";           }//end if
       
    if(checkFullHouse(yourHand)){       yourResult="A Full House";      }//end if
       
    if(checkFourKind(yourHand)){        yourResult="Four Of A Kind";    }//end if
       
    if(checkStraightFlush(yourHand)){   yourResult="A Flush";           }//end if
       
    if(checkRoyalFlush(yourHand)){      yourResult="A Royal Flush!!";   }//end if
    
    cout << "\nYou Have... " << yourResult << "!!!\n";
    
    cout << endl;
    
    return 0;
    
}//end main

//search

int findLeast(int a[], int start, int end) {
     int posOfLeastSoFar = start;
     int i;
     for (i = start+1; i<=end; i++) {
         if (a[i] < a[posOfLeastSoFar]) {
            posOfLeastSoFar = i;
         }
     }
     return posOfLeastSoFar;
}

void swap(int a[], int pos1, int pos2) {
     int temp = a[pos1];
     a[pos1] = a[pos2];
     a[pos2] = temp;
}

void selectionSort(int a[], int s) {
    for (int pos = 0; pos < s; pos++) {
        swap(a, pos, findLeast(a, pos, s-1));
    }
}

//process hand vector

bool checkHiCard(vector<Card>& hand){
    
    return true;
    
}//end function

bool checkPair(vector<Card>& hand){
    
    bool pair=false;
    
    int temp1[5],temp2[5];
    
    for(int x=0;x<5;x++){
        
        temp1[x]=hand[x].getNumber();
        temp2[x]=hand[x].getNumber();
        
    }//end for
    
    for(int y=0;y<5;y++){
        
        for(int z=0;z<5;z++){
                 
            if((temp1[y]==temp2[z])&&(z!=y)){
                    
                pair=true;
                
            }//end if
            
        }//end for
        
    }//end for
    
    return pair;
        
}//end function

bool checkTwoPair(vector<Card>& hand){
    
    return false;
        
}//end function
   
bool checkThreeKind(vector<Card>& hand){
    
    bool three=false;
    
    int temp1[5],temp2[5],temp3[5];
    
    for(int x=0;x<5;x++){
        
        temp1[x]=hand[x].getNumber();
        temp2[x]=hand[x].getNumber();
        temp3[x]=hand[x].getNumber();
        
    }//end for
    
    for(int y=0;y<5;y++){
        
        for(int z=0;z<5;z++){
                 
            for(int a=0;a<5;a++){
                    
                if(
                   (temp1[y]==temp2[z] && temp1[y]==temp3[a])
                   &&
                   (    (y!=z)&&(z!=a)&&(y!=a)  )
                   ){
                    
                    three=true;
                
                }//end if
                
            }//end for
            
        }//end for
        
    }//end for
    
    return three;
        
}//end function
   
bool checkStraight(vector<Card>& hand){
    
    int a[5];
    
    a[0]=hand[0].getNumber();
    a[1]=hand[1].getNumber();
    a[2]=hand[2].getNumber();
    a[3]=hand[3].getNumber();
    a[4]=hand[4].getNumber();
    
    //check for low straight A-5 to 10-K
    
    for(int b=0;b<5;b++){
        
        if(a[b]==1){
            
            a[b]=2;//since there is no 'One' card T_T
            
        }//end if
        
    }//end for
    
    selectionSort(a,5);
    
    if(
        ((a[0]+1)==a[1])&&
        ((a[0]+2)==a[2])&&
        ((a[0]+3)==a[3])&&
        ((a[0]+4)==a[4])
       ){
    
        return true;
    
    }else{
        
        a[0]=hand[0].getNumber();
        a[1]=hand[1].getNumber();
        a[2]=hand[2].getNumber();
        a[3]=hand[3].getNumber();
        a[4]=hand[4].getNumber();
        
        //check for hi straight 2-6 to J-A
        
        for(int b=0;b<5;b++){
            
            if(a[b]==1){
                
                a[b]=15;
                
            }//end if
            
        }//end for
        
        selectionSort(a,5);
        
        if(
            ((a[0]+1)==a[1])&&
            ((a[0]+2)==a[2])&&
            ((a[0]+3)==a[3])&&
            ((a[0]+4)==a[4])
           ){
        
            return true;
        
        }else{
            
            return false;
            
        }//end if
        
    }//end if
    
}//end function
   
bool checkFlush(vector<Card>& hand){
    
    if((hand[0].getSuit()==hand[1].getSuit())
       &&
       (hand[0].getSuit()==hand[2].getSuit())
       &&
       (hand[0].getSuit()==hand[3].getSuit())
       &&
       (hand[0].getSuit()==hand[4].getSuit())
      ){
       
        return true;
    
    }else{
        
        return false;
    
    }//end if
    
}//end function
   
bool checkFullHouse(vector<Card>& hand){
    
    return false;
        
}//end function
   
bool checkFourKind(vector<Card>& hand){
    
    bool four=false;
    
    int temp1[5],temp2[5],temp3[5],temp4[5];
    
    for(int x=0;x<5;x++){
        
        temp1[x]=hand[x].getNumber();
        temp2[x]=hand[x].getNumber();
        temp3[x]=hand[x].getNumber();
        temp4[x]=hand[x].getNumber();
        
    }//end for
    
    for(int y=0;y<5;y++){
        
        for(int z=0;z<5;z++){
                 
            for(int a=0;a<5;a++){
                
                for(int b=0;b<5;b++){
                    
                    if(
                       (temp1[y]==temp2[z] && temp1[y]==temp3[a] && temp1[y]==temp4[b])
                       &&
                       (    (y!=z)&&(z!=a)&&(y!=a)&&(y!=b)&&(z!=b)&&(a!=b)  )
                       ){
                        
                        four=true;
                    
                    }//end if
                    
                }//end for
                
            }//end for
            
        }//end for
        
    }//end for
    
    return four;

}//end function
   
bool checkStraightFlush(vector<Card>& hand){
    
    if(checkFlush(hand)&&checkStraight(hand)){
        
        return true;
        
      }else{
        
        return false;
    
      }//end if
      
}//end function
   
bool checkRoyalFlush(vector<Card>& hand){
    
    bool hasKing=false;
    bool hasAce=false;
    
    for(int x=0;x<5;x++){
        
        if(hand[x].getNumber()==1){
            
            hasAce=true;
            
        }//end if
        
        if(hand[x].getNumber()==14){
            
            hasKing=true;
            
        }//end if
        
    }//end for
    
    if(hasKing&&hasAce&&checkStraightFlush(hand)){
        
        return true;
    
    }else{
         
    return false;
        
    }//end if
        
}//end function

Link to comment
Share on other sites

Just thought of Full House, 2 Pair anyone?

 


#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Card{
    
    private:
        
        //holder variables
        
        string  suit;
        int     number;
        bool    dealt;
        
        //print variables
        string  word;
        
    public:
        
        //setters
        
        void setSuit(string input){     suit=input;     }//end setSuit
        
        void setNumber(int input){      number=input;   }//end setNumber
        
        void setDealt(bool input){      dealt=input;    }//end setDealt
        
        //getters
        
        string getSuit(){               return suit;    }//end getSuit
        
        int getNumber(){                return number;  }//end getNumber
        
        bool getDealt(){                return dealt;   }//end getDealt
        
        void printCard(){
            
            switch(number){
                
                case 1:     word="Ace";   break;
                case 2:     word="One";   break; //!! OOPS, long night, killed later...
                case 3:     word="Two";   break;
                case 4:     word="Three";   break;
                case 5:     word="Four";   break;
                case 6:     word="Five";   break;
                case 7:     word="Six";   break;
                case 8:     word="Seven";   break;
                case 9:     word="Eight";   break;
                case 10:    word="Nine";   break;
                case 11:    word="Ten";   break;
                case 12:    word="Jack";   break;
                case 13:    word="Queen";   break;
                case 14:    word="King";   break;
                    
            }//end switch
            
            cout << word << " of " << suit << "\t";
            
        }//end printCard
    
};//end Card

bool checkHiCard(vector<Card>& hand);
    
bool checkPair(vector<Card>& hand);

bool checkTwoPair(vector<Card>& hand);
   
bool checkThreeKind(vector<Card>& hand);
   
bool checkStraight(vector<Card>& hand);
   
bool checkFlush(vector<Card>& hand);
   
bool checkFullHouse(vector<Card>& hand);
   
bool checkFourKind(vector<Card>& hand);
   
bool checkStraightFlush(vector<Card>& hand);
   
bool checkRoyalFlush(vector<Card>& hand);

int findLeast(int a[], int start, int end);
void swap(int a[], int pos1, int pos2);
void selectionSort(int a[], int s);

int main(){
    
    vector<Card> deck;
      
    //cout << deck[0].getNumber();
    
    string  buildSuit;
    int     buildNumber;
    bool    buildDealt;
    
    for(int i=1,j=1,k=1; i<57; i++,k++){
        
        switch(i){
            
            case 15:    j=2;    k=1;    break;  //make suit spades
            
            case 29:    j=3;    k=1;    break;  //make suit diamonds
                
            case 43:    j=4;    k=1;    break;  //make suit clubs
            
        }//end switch
        
        switch(j){
            
            case 1: buildSuit="Hearts";     break;
                
            case 2: buildSuit="Spades";     break;
                
            case 3: buildSuit="Diamonds";   break;
                
            case 4: buildSuit="Clubs";      break;
            
        }//end switch
        
        buildNumber=    k;
        buildDealt=     false;
        
        Card *card;
    
        card= new Card;
        
        card->setSuit(buildSuit);
        card->setNumber(buildNumber);
        card->setDealt(buildDealt);
        
        if(k!=2){ //OMG, 1 of anything doesn't exist T_T
            
            deck.push_back(*card);
            
        }//end if
        
    }//end for
    
    srand(time(NULL));
    
    random_shuffle(deck.begin(), deck.end()); //shuffle the deck 
    
    /*******************/
    /*  FOR DEBUGGING  */
    /*******************/
     
     
     deck[15].setNumber(11);
     deck[16].setNumber(11);
     deck[17].setNumber(12);
     deck[18].setNumber(3);
     deck[19].setNumber(2);
     
     
     
     deck[15].setSuit("Diamonds");
     deck[16].setSuit("Spades");
     deck[17].setSuit("Spades");
     deck[18].setSuit("Spades");
     deck[19].setSuit("Spades");
     
     
    /*******************/
    
    cout << endl;
    
    for(int l=0,n=1; l<52; l++,n++){
        
        cout << l+1 << ". ";
        
        deck[l].printCard();
        
        
        if(n==3){
            
            n=0;
            
            cout << endl;
            
        }//end if
        
    }//end for
    
    cout << endl;
    
    vector<Card> yourHand;
    
    cout << "\nYou've Been Dealt:\n\n";
    
    for(int m=15,o=1; m<20; m++,o++){
        
        //process hand
        
        buildSuit=      deck[m].getSuit();
        buildNumber=    deck[m].getNumber();
        buildDealt=     true;
        
        Card *card;
    
        card= new Card;
        
        card->setSuit(buildSuit);
        card->setNumber(buildNumber);
        card->setDealt(buildDealt);
        
        yourHand.push_back(*card);
        
        //display hand
        
        cout << o << ". ";
        
        deck[m].printCard();
        
        cout << endl;
        
    }//end for
    
    string yourResult;
    
    if(checkHiCard(yourHand)){          yourResult="A Hi Card";         }//end if
    
    if(checkPair(yourHand)){            yourResult="A Pair";            }//end if
    
    if(checkTwoPair(yourHand)){         yourResult="Two Pair";          }//end if
       
    if(checkThreeKind(yourHand)){       yourResult="Three Of A Kind";   }//end if
       
    if(checkStraight(yourHand)){        yourResult="A Straight";        }//end if
       
    if(checkFlush(yourHand)){           yourResult="A Flush";           }//end if
       
    if(checkFullHouse(yourHand)){       yourResult="A Full House";      }//end if
       
    if(checkFourKind(yourHand)){        yourResult="Four Of A Kind";    }//end if
       
    if(checkStraightFlush(yourHand)){   yourResult="A Flush";           }//end if
       
    if(checkRoyalFlush(yourHand)){      yourResult="A Royal Flush!!";   }//end if
    
    cout << "\nYou Have... " << yourResult << "!!!\n";
    
    cout << endl;
    
    return 0;
    
}//end main

//search

int findLeast(int a[], int start, int end) {
     int posOfLeastSoFar = start;
     int i;
     for (i = start+1; i<=end; i++) {
         if (a[i] < a[posOfLeastSoFar]) {
            posOfLeastSoFar = i;
         }
     }
     return posOfLeastSoFar;
}

void swap(int a[], int pos1, int pos2) {
     int temp = a[pos1];
     a[pos1] = a[pos2];
     a[pos2] = temp;
}

void selectionSort(int a[], int s) {
    for (int pos = 0; pos < s; pos++) {
        swap(a, pos, findLeast(a, pos, s-1));
    }
}

//process hand vector

bool checkHiCard(vector<Card>& hand){
    
    return true;
    
}//end function

bool checkPair(vector<Card>& hand){
    
    bool pair=false;
    
    int temp1[5],temp2[5];
    
    for(int x=0;x<5;x++){
        
        temp1[x]=hand[x].getNumber();
        temp2[x]=hand[x].getNumber();
        
    }//end for
    
    for(int y=0;y<5;y++){
        
        for(int z=0;z<5;z++){
                 
            if((temp1[y]==temp2[z])&&(z!=y)){
                    
                pair=true;
                
            }//end if
            
        }//end for
        
    }//end for
    
    return pair;
        
}//end function

bool checkTwoPair(vector<Card>& hand){
    
    return false;
        
}//end function
   
bool checkThreeKind(vector<Card>& hand){
    
    bool three=false;
    
    int temp1[5],temp2[5],temp3[5];
    
    for(int x=0;x<5;x++){
        
        temp1[x]=hand[x].getNumber();
        temp2[x]=hand[x].getNumber();
        temp3[x]=hand[x].getNumber();
        
    }//end for
    
    for(int y=0;y<5;y++){
        
        for(int z=0;z<5;z++){
                 
            for(int a=0;a<5;a++){
                    
                if(
                   (temp1[y]==temp2[z] && temp1[y]==temp3[a])
                   &&
                   (    (y!=z)&&(z!=a)&&(y!=a)  )
                   ){
                    
                    three=true;
                
                }//end if
                
            }//end for
            
        }//end for
        
    }//end for
    
    return three;
        
}//end function
   
bool checkStraight(vector<Card>& hand){
    
    int a[5];
    
    a[0]=hand[0].getNumber();
    a[1]=hand[1].getNumber();
    a[2]=hand[2].getNumber();
    a[3]=hand[3].getNumber();
    a[4]=hand[4].getNumber();
    
    //check for low straight A-5 to 10-K
    
    for(int b=0;b<5;b++){
        
        if(a[b]==1){
            
            a[b]=2;//since there is no 'One' card T_T
            
        }//end if
        
    }//end for
    
    selectionSort(a,5);
    
    if(
        ((a[0]+1)==a[1])&&
        ((a[0]+2)==a[2])&&
        ((a[0]+3)==a[3])&&
        ((a[0]+4)==a[4])
       ){
    
        return true;
    
    }else{
        
        a[0]=hand[0].getNumber();
        a[1]=hand[1].getNumber();
        a[2]=hand[2].getNumber();
        a[3]=hand[3].getNumber();
        a[4]=hand[4].getNumber();
        
        //check for hi straight 2-6 to J-A
        
        for(int b=0;b<5;b++){
            
            if(a[b]==1){
                
                a[b]=15;
                
            }//end if
            
        }//end for
        
        selectionSort(a,5);
        
        if(
            ((a[0]+1)==a[1])&&
            ((a[0]+2)==a[2])&&
            ((a[0]+3)==a[3])&&
            ((a[0]+4)==a[4])
           ){
        
            return true;
        
        }else{
            
            return false;
            
        }//end if
        
    }//end if
    
}//end function
   
bool checkFlush(vector<Card>& hand){
    
    if((hand[0].getSuit()==hand[1].getSuit())
       &&
       (hand[0].getSuit()==hand[2].getSuit())
       &&
       (hand[0].getSuit()==hand[3].getSuit())
       &&
       (hand[0].getSuit()==hand[4].getSuit())
      ){
       
        return true;
    
    }else{
        
        return false;
    
    }//end if
    
}//end function
   
bool checkFullHouse(vector<Card>& hand){
    
    if(checkThreeKind(hand)){
        
        int a[5];
    
        a[0]=hand[0].getNumber();
        a[1]=hand[1].getNumber();
        a[2]=hand[2].getNumber();
        a[3]=hand[3].getNumber();
        a[4]=hand[4].getNumber();
    
        selectionSort(a,5);
        
        int threeTriggerCard=a[2]; // middle card has to be part of the 3 Kind, sorted
        
        int holderArray[2];
        
        for(int b=0,c=0;b<5;b++){
            
            if(a[b]!=threeTriggerCard){ // get out the two others
                
                holderArray[c]=a[b];
                c++; // lol
                
            }//end if
            
        }//end for
        
        if(holderArray[0]==holderArray[1]){
           
            return true; 
        
        }else{
            
            return false;
            
        }//end if
        
    }else{
        
        return false;
    
    }//end if
    
}//end function
   
bool checkFourKind(vector<Card>& hand){
    
    bool four=false;
    
    int temp1[5],temp2[5],temp3[5],temp4[5];
    
    for(int x=0;x<5;x++){
        
        temp1[x]=hand[x].getNumber();
        temp2[x]=hand[x].getNumber();
        temp3[x]=hand[x].getNumber();
        temp4[x]=hand[x].getNumber();
        
    }//end for
    
    for(int y=0;y<5;y++){
        
        for(int z=0;z<5;z++){
                 
            for(int a=0;a<5;a++){
                
                for(int b=0;b<5;b++){
                    
                    if(
                       (temp1[y]==temp2[z] && temp1[y]==temp3[a] && temp1[y]==temp4[b])
                       &&
                       (    (y!=z)&&(z!=a)&&(y!=a)&&(y!=b)&&(z!=b)&&(a!=b)  )
                       ){
                        
                        four=true;
                    
                    }//end if
                    
                }//end for
                
            }//end for
            
        }//end for
        
    }//end for
    
    return four;

}//end function
   
bool checkStraightFlush(vector<Card>& hand){
    
    if(checkFlush(hand)&&checkStraight(hand)){
        
        return true;
        
      }else{
        
        return false;
    
      }//end if
      
}//end function
   
bool checkRoyalFlush(vector<Card>& hand){
    
    bool hasKing=false;
    bool hasAce=false;
    
    for(int x=0;x<5;x++){
        
        if(hand[x].getNumber()==1){
            
            hasAce=true;
            
        }//end if
        
        if(hand[x].getNumber()==14){
            
            hasKing=true;
            
        }//end if
        
    }//end for
    
    if(hasKing&&hasAce&&checkStraightFlush(hand)){
        
        return true;
    
    }else{
         
    return false;
        
    }//end if
        
}//end function


Link to comment
Share on other sites

Nevermind again :P

 

I guess posting here kickstarts the old noodle.

 

/final

 

Josh

 


#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Card{
    
    private:
        
        //holder variables
        
        string  suit;
        int     number;
        bool    dealt;
        
        //print variables
        string  word;
        
    public:
        
        //setters
        
        void setSuit(string input){     suit=input;     }//end setSuit
        
        void setNumber(int input){      number=input;   }//end setNumber
        
        void setDealt(bool input){      dealt=input;    }//end setDealt
        
        //getters
        
        string getSuit(){               return suit;    }//end getSuit
        
        int getNumber(){                return number;  }//end getNumber
        
        bool getDealt(){                return dealt;   }//end getDealt
        
        void printCard(){
            
            switch(number){
                
                case 1:     word="Ace";   break;
                case 2:     word="One";   break; //!! OOPS, long night, killed later...
                case 3:     word="Two";   break;
                case 4:     word="Three";   break;
                case 5:     word="Four";   break;
                case 6:     word="Five";   break;
                case 7:     word="Six";   break;
                case 8:     word="Seven";   break;
                case 9:     word="Eight";   break;
                case 10:    word="Nine";   break;
                case 11:    word="Ten";   break;
                case 12:    word="Jack";   break;
                case 13:    word="Queen";   break;
                case 14:    word="King";   break;
                    
            }//end switch
            
            cout << word << " of " << suit << "\t";
            
        }//end printCard
    
};//end Card

bool checkHiCard(vector<Card>& hand);
    
bool checkPair(vector<Card>& hand);

bool checkTwoPair(vector<Card>& hand);
   
bool checkThreeKind(vector<Card>& hand);
   
bool checkStraight(vector<Card>& hand);
   
bool checkFlush(vector<Card>& hand);
   
bool checkFullHouse(vector<Card>& hand);
   
bool checkFourKind(vector<Card>& hand);
   
bool checkStraightFlush(vector<Card>& hand);
   
bool checkRoyalFlush(vector<Card>& hand);

int findLeast(int a[], int start, int end);
void swap(int a[], int pos1, int pos2);
void selectionSort(int a[], int s);

int main(){
    
    vector<Card> deck;
      
    //cout << deck[0].getNumber();
    
    string  buildSuit;
    int     buildNumber;
    bool    buildDealt;
    
    for(int i=1,j=1,k=1; i<57; i++,k++){
        
        switch(i){
            
            case 15:    j=2;    k=1;    break;  //make suit spades
            
            case 29:    j=3;    k=1;    break;  //make suit diamonds
                
            case 43:    j=4;    k=1;    break;  //make suit clubs
            
        }//end switch
        
        switch(j){
            
            case 1: buildSuit="Hearts";     break;
                
            case 2: buildSuit="Spades";     break;
                
            case 3: buildSuit="Diamonds";   break;
                
            case 4: buildSuit="Clubs";      break;
            
        }//end switch
        
        buildNumber=    k;
        buildDealt=     false;
        
        Card *card;
    
        card= new Card;
        
        card->setSuit(buildSuit);
        card->setNumber(buildNumber);
        card->setDealt(buildDealt);
        
        if(k!=2){ //OMG, 1 of anything doesn't exist T_T
            
            deck.push_back(*card);
            
        }//end if
        
    }//end for
    
    srand(time(NULL));
    
    random_shuffle(deck.begin(), deck.end()); //shuffle the deck 
    
    /*******************/
    /*  FOR DEBUGGING  */
    /*******************/
     
     
     deck[15].setNumber(11);
     deck[16].setNumber(11);
     deck[17].setNumber(12);
     deck[18].setNumber(3);
     deck[19].setNumber(12);
     
     
     
     deck[15].setSuit("Diamonds");
     deck[16].setSuit("Spades");
     deck[17].setSuit("Spades");
     deck[18].setSuit("Spades");
     deck[19].setSuit("Spades");
     
     
    /*******************/
    
    cout << endl;
    
    for(int l=0,n=1; l<52; l++,n++){
        
        cout << l+1 << ". ";
        
        deck[l].printCard();
        
        
        if(n==3){
            
            n=0;
            
            cout << endl;
            
        }//end if
        
    }//end for
    
    cout << endl;
    
    vector<Card> yourHand;
    
    cout << "\nYou've Been Dealt:\n\n";
    
    for(int m=15,o=1; m<20; m++,o++){
        
        //process hand
        
        buildSuit=      deck[m].getSuit();
        buildNumber=    deck[m].getNumber();
        buildDealt=     true;
        
        Card *card;
    
        card= new Card;
        
        card->setSuit(buildSuit);
        card->setNumber(buildNumber);
        card->setDealt(buildDealt);
        
        yourHand.push_back(*card);
        
        //display hand
        
        cout << o << ". ";
        
        deck[m].printCard();
        
        cout << endl;
        
    }//end for
    
    string yourResult;
    
    if(checkHiCard(yourHand)){          yourResult="A Hi Card";         }//end if
    
    if(checkPair(yourHand)){            yourResult="A Pair";            }//end if
    
    if(checkTwoPair(yourHand)){         yourResult="Two Pair";          }//end if
       
    if(checkThreeKind(yourHand)){       yourResult="Three Of A Kind";   }//end if
       
    if(checkStraight(yourHand)){        yourResult="A Straight";        }//end if
       
    if(checkFlush(yourHand)){           yourResult="A Flush";           }//end if
       
    if(checkFullHouse(yourHand)){       yourResult="A Full House";      }//end if
       
    if(checkFourKind(yourHand)){        yourResult="Four Of A Kind";    }//end if
       
    if(checkStraightFlush(yourHand)){   yourResult="A Flush";           }//end if
       
    if(checkRoyalFlush(yourHand)){      yourResult="A Royal Flush!!";   }//end if
    
    cout << "\nYou Have... " << yourResult << "!!!\n";
    
    cout << endl;
    
    return 0;
    
}//end main

//search

int findLeast(int a[], int start, int end) {
     int posOfLeastSoFar = start;
     int i;
     for (i = start+1; i<=end; i++) {
         if (a[i] < a[posOfLeastSoFar]) {
            posOfLeastSoFar = i;
         }
     }
     return posOfLeastSoFar;
}

void swap(int a[], int pos1, int pos2) {
     int temp = a[pos1];
     a[pos1] = a[pos2];
     a[pos2] = temp;
}

void selectionSort(int a[], int s) {
    for (int pos = 0; pos < s; pos++) {
        swap(a, pos, findLeast(a, pos, s-1));
    }
}

//process hand vector

bool checkHiCard(vector<Card>& hand){
    
    return true;
    
}//end function

bool checkPair(vector<Card>& hand){
    
    bool pair=false;
    
    int temp1[5],temp2[5];
    
    for(int x=0;x<5;x++){
        
        temp1[x]=hand[x].getNumber();
        temp2[x]=hand[x].getNumber();
        
    }//end for
    
    for(int y=0;y<5;y++){
        
        for(int z=0;z<5;z++){
                 
            if((temp1[y]==temp2[z])&&(z!=y)){
                    
                pair=true;
                
            }//end if
            
        }//end for
        
    }//end for
    
    return pair;
        
}//end function

bool checkTwoPair(vector<Card>& hand){
    
    if(checkPair(hand)&&!checkThreeKind(hand)){
        
        int a[5];
    
        a[0]=hand[0].getNumber();
        a[1]=hand[1].getNumber();
        a[2]=hand[2].getNumber();
        a[3]=hand[3].getNumber();
        a[4]=hand[4].getNumber();
        
        int temp;
        
        bool twoPair=false;
        
        for(int x=0;x<5;x++){
            
            for(int y=0;y<5;y++){
                
                if(x!=y&&(a[x]==a[y])){
                    
                    temp=a[x];
                    
                }//end if
                
            }//end for
            
        }//end for
        
        for(int x=0;x<5;x++){
            
            for(int y=0;y<5;y++){
                
                if(
                   ((a[x]!=temp && a[y]!=temp))
                   &&
                   (x!=y&&(a[x]==a[y]))
                   ){
                     
                    twoPair=true;
                    
                }//end if
                
            }//end for
            
        }//end for
        
        return twoPair;
        
    }else{
        
        return false;
    
    }//end if
        
}//end function
   
bool checkThreeKind(vector<Card>& hand){
    
    bool three=false;
    
    int temp1[5],temp2[5],temp3[5];
    
    for(int x=0;x<5;x++){
        
        temp1[x]=hand[x].getNumber();
        temp2[x]=hand[x].getNumber();
        temp3[x]=hand[x].getNumber();
        
    }//end for
    
    for(int y=0;y<5;y++){
        
        for(int z=0;z<5;z++){
                 
            for(int a=0;a<5;a++){
                    
                if(
                   (temp1[y]==temp2[z] && temp1[y]==temp3[a])
                   &&
                   (    (y!=z)&&(z!=a)&&(y!=a)  )
                   ){
                    
                    three=true;
                
                }//end if
                
            }//end for
            
        }//end for
        
    }//end for
    
    return three;
        
}//end function
   
bool checkStraight(vector<Card>& hand){
    
    int a[5];
    
    a[0]=hand[0].getNumber();
    a[1]=hand[1].getNumber();
    a[2]=hand[2].getNumber();
    a[3]=hand[3].getNumber();
    a[4]=hand[4].getNumber();
    
    //check for low straight A-5 to 10-K
    
    for(int b=0;b<5;b++){
        
        if(a[b]==1){
            
            a[b]=2;//since there is no 'One' card T_T
            
        }//end if
        
    }//end for
    
    selectionSort(a,5);
    
    if(
        ((a[0]+1)==a[1])&&
        ((a[0]+2)==a[2])&&
        ((a[0]+3)==a[3])&&
        ((a[0]+4)==a[4])
       ){
    
        return true;
    
    }else{
        
        a[0]=hand[0].getNumber();
        a[1]=hand[1].getNumber();
        a[2]=hand[2].getNumber();
        a[3]=hand[3].getNumber();
        a[4]=hand[4].getNumber();
        
        //check for hi straight 2-6 to J-A
        
        for(int b=0;b<5;b++){
            
            if(a[b]==1){
                
                a[b]=15;
                
            }//end if
            
        }//end for
        
        selectionSort(a,5);
        
        if(
            ((a[0]+1)==a[1])&&
            ((a[0]+2)==a[2])&&
            ((a[0]+3)==a[3])&&
            ((a[0]+4)==a[4])
           ){
        
            return true;
        
        }else{
            
            return false;
            
        }//end if
        
    }//end if
    
}//end function
   
bool checkFlush(vector<Card>& hand){
    
    if((hand[0].getSuit()==hand[1].getSuit())
       &&
       (hand[0].getSuit()==hand[2].getSuit())
       &&
       (hand[0].getSuit()==hand[3].getSuit())
       &&
       (hand[0].getSuit()==hand[4].getSuit())
      ){
       
        return true;
    
    }else{
        
        return false;
    
    }//end if
    
}//end function
   
bool checkFullHouse(vector<Card>& hand){
    
    if(checkThreeKind(hand)){
        
        int a[5];
    
        a[0]=hand[0].getNumber();
        a[1]=hand[1].getNumber();
        a[2]=hand[2].getNumber();
        a[3]=hand[3].getNumber();
        a[4]=hand[4].getNumber();
    
        selectionSort(a,5);
        
        int threeTriggerCard=a[2]; // middle card has to be part of the 3 Kind, sorted
        
        int holderArray[2];
        
        for(int b=0,c=0;b<5;b++){
            
            if(a[b]!=threeTriggerCard){ // get out the two others
                
                holderArray[c]=a[b];
                c++; // lol
                
            }//end if
            
        }//end for
        
        if(holderArray[0]==holderArray[1]){
           
            return true; 
        
        }else{
            
            return false;
            
        }//end if
        
    }else{
        
        return false;
    
    }//end if
    
}//end function
   
bool checkFourKind(vector<Card>& hand){
    
    bool four=false;
    
    int temp1[5],temp2[5],temp3[5],temp4[5];
    
    for(int x=0;x<5;x++){
        
        temp1[x]=hand[x].getNumber();
        temp2[x]=hand[x].getNumber();
        temp3[x]=hand[x].getNumber();
        temp4[x]=hand[x].getNumber();
        
    }//end for
    
    for(int y=0;y<5;y++){
        
        for(int z=0;z<5;z++){
                 
            for(int a=0;a<5;a++){
                
                for(int b=0;b<5;b++){
                    
                    if(
                       (temp1[y]==temp2[z] && temp1[y]==temp3[a] && temp1[y]==temp4[b])
                       &&
                       (    (y!=z)&&(z!=a)&&(y!=a)&&(y!=b)&&(z!=b)&&(a!=b)  )
                       ){
                        
                        four=true;
                    
                    }//end if
                    
                }//end for
                
            }//end for
            
        }//end for
        
    }//end for
    
    return four;

}//end function
   
bool checkStraightFlush(vector<Card>& hand){
    
    if(checkFlush(hand)&&checkStraight(hand)){
        
        return true;
        
      }else{
        
        return false;
    
      }//end if
      
}//end function
   
bool checkRoyalFlush(vector<Card>& hand){
    
    bool hasKing=false;
    bool hasAce=false;
    
    for(int x=0;x<5;x++){
        
        if(hand[x].getNumber()==1){
            
            hasAce=true;
            
        }//end if
        
        if(hand[x].getNumber()==14){
            
            hasKing=true;
            
        }//end if
        
    }//end for
    
    if(hasKing&&hasAce&&checkStraightFlush(hand)){
        
        return true;
    
    }else{
         
    return false;
        
    }//end if
        
}//end function


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.