Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, i have a code that currently runs through hands of poker until it receives a hand that is a royal straight flush. iv'e been thinking about this for a while and iv'e wandered if you guys have any idea how i could make this program more effective using methods. The current code is:

C#
import java.util.Random;
public class Poker {

    public static void main(String[] args) {
        Random rand=new Random();
        String hand="nothing";
        long count=0;
        while(hand!="Royal Straight Flush"){
            count++;
        //for(int x=1; x<10000; x++){
        // Generate cards and suits
        // nextInt(x) generates a # between [0, x),
        int card1=rand.nextInt(13)+1;
        int card2=rand.nextInt(13)+1;
        int card3=rand.nextInt(13)+1;
        int card4=rand.nextInt(13)+1;
        int card5=rand.nextInt(13)+1;
        int suit1=rand.nextInt(4)+1;
        int suit2=rand.nextInt(4)+1;
        int suit3=rand.nextInt(4)+1;
        int suit4=rand.nextInt(4)+1;
        int suit5=rand.nextInt(4)+1;

        hand="nothing";
        boolean straight=false;
        boolean flush=false;
        boolean pair=false;

        //check for a flush
        if(suit1==suit2&&suit2==suit3&&suit3==suit4&&suit4==suit5)
            flush=true;

        //check for a pair
        //one pair
        //5C2==10
        //1,2 || 1,3|| 1,4||1,5||2,3||2,4||2,5||3,4||3,5||4,5
        if((card1==card2)||(card1==card3)||(card1==card4)||(card1==card5)||
                (card2==card3)||(card2==card4)||(card2==card5)||
                (card3==card4)||(card3==card5)||
                (card4==card5))
                    pair=true;

        //find a straight
        int min=Math.min(card1, Math.min(card2, Math.min(card3, Math.min(card4, card5))));
        int max=Math.max(card1, Math.max(card2, Math.max(card3, Math.max(card4, card5))));
        int sum=card1+card2+card3+card4+card5;
        if(!pair)
        {
            if( (min==1 && max==5 && sum==15)||(min==2 && max==6 && sum==20)||
                (min==3 && max==7 && sum==25)||(min==4 && max==8 && sum==30)||
                (min==5 && max==9 && sum==35)||(min==6 && max==10 && sum==40)||
                (min==7 && max==11 && sum==45)||(min==8 && max==12 && sum==50)||
                (min==9 && max==13 && sum==55)||(min==1 && max==13 && sum==47)) //last is RSF
                    straight=true;
        }
        //Royal Straight Flush

        if(flush&&straight&&sum==47)
            hand="Royal Straight Flush";

        //straight flush
        else if(flush && straight)
            hand="Straight Flush";

        //check for four of a kind
        //options are 1,2,3,4 || 1,2,3,5 || 1,2,4,5 || 1,3,4,5 || 2,3,4,5
        else if(    (card1==card2&&card2==card3&&card3==card4)||
            (card1==card2&&card2==card3&&card3==card5)||
            (card1==card2&&card2==card4&&card4==card5)||
            (card1==card3&&card3==card4&&card4==card5)||
            (card2==card3&&card3==card4&&card4==card5))
                hand="Four of a Kind";

        //full house
        //5C2 by mathematics is 5!/(3!*2!) = 10 options
        //1,2,3 - 4,5 || 1,2,4 - 3,5 || 1,2,5 - 3,4 || 1,3,4 - 2,5 || 1,3,5 - 2,4 ||
        //1,4,5 - 2,3 || 2,3,4 - 1,5 || 2,3,5 - 1,4 || 2,4,5 - 1,3 || 3,4,5 - 1,2
        else if((card1==card2&&card2==card3&&card3!=card4&&card4==card5)||
                (card1==card2&&card2==card4&&card4!=card3&&card3==card5)||
                (card1==card2&&card2==card5&&card5!=card3&&card3==card4)||
                (card1==card3&&card3==card4&&card4!=card2&&card2==card5)||
                (card1==card3&&card3==card5&&card5!=card2&&card2==card4)||
                (card1==card4&&card4==card5&&card5!=card2&&card2==card3)||
                (card2==card3&&card3==card4&&card4!=card1&&card1==card5)||
                (card2==card3&&card3==card5&&card5!=card1&&card1==card4)||
                (card2==card4&&card4==card5&&card5!=card1&&card1==card3)||
                (card3==card4&&card4==card5&&card5!=card1&&card1==card2))
                    hand="Full House";

        //flush
        //based on previous calculation
        else if(flush)
            hand="Flush";

        //straight
        //based on previous calculation
        else if(straight)
            hand="Straight";

        //three of a kind
        //5C3 is 5!/(3!*2!) = 10 options
        //use the triples from the full house above
        else if((card1==card2&&card2==card3)||
                (card1==card2&&card2==card4)||
                (card1==card2&&card2==card5)||
                (card1==card3&&card3==card4)||
                (card1==card3&&card3==card5)||
                (card1==card4&&card4==card5)||
                (card2==card3&&card3==card4)||
                (card2==card3&&card3==card5)||
                (card2==card4&&card4==card5)||
                (card3==card4&&card4==card5))
                    hand="Three of a kind";

        //two pair
        //5C2*3C2/2 to avoid repeats = 15
        //1,2-3,4 || 1,2-3,5 || 1,2-4,5
        //1,3-2,4 || 1,3-2,5 || 1,3-4,5
        //1,4-2,3 || 1,4-2,5 || 1,4-3,5
        //1,5-2,3 || 1,5-2,4 || 1,5-3,4
        //2,3-4,5 || 2,4-3,5 || 2,5-3,4
        else if((card1==card2&&card3==card4)||(card1==card2&&card3==card5)||
                (card1==card2&&card4==card5)||(card1==card3&&card2==card4)||
                (card1==card3&&card2==card5)||(card1==card3&&card4==card5)||
                (card1==card4&&card2==card3)||(card1==card4&&card2==card5)||
                (card1==card4&&card3==card5)||(card1==card5&&card2==card3)||
                (card1==card5&&card2==card4)||(card1==card5&&card3==card4)||
                (card2==card3&&card4==card5)||(card2==card4&&card3==card5)||
                (card2==card5&&card3==card4))
                    hand="Two of a kind";

        //one pair
        //based on previous calculation
        else if(pair)
            hand="Pair";

        //convert hand for output
        String showCards="";
        switch(card1){
        case 1: showCards+="A";break;
        case 11:showCards+="J";break;
        case 12:showCards+="Q";break;
        case 13:showCards+="K";break;
        default:showCards+=card1;  //simply adds the number to the string
        }

        switch(suit1){
        case 1: showCards+="H";break;
        case 2: showCards+="C";break;
        case 3: showCards+="D";break;
        default:showCards+="S";
        }
        showCards+=" ";
        switch(card2){
        case 1: showCards+="A";break;
        case 11:showCards+="J";break;
        case 12:showCards+="Q";break;
        case 13:showCards+="K";break;
        default:showCards+=card2;  //simply adds the number to the string
        }

        switch(suit2){
        case 1: showCards+="H";break;
        case 2: showCards+="C";break;
        case 3: showCards+="D";break;
        default:showCards+="S";
        }
        showCards+=" ";
        switch(card3){
        case 1: showCards+="A";break;
        case 11:showCards+="J";break;
        case 12:showCards+="Q";break;
        case 13:showCards+="K";break;
        default:showCards+=card3;  //simply adds the number to the string
        }

        switch(suit3){
        case 1: showCards+="H";break;
        case 2: showCards+="C";break;
        case 3: showCards+="D";break;
        default:showCards+="S";
        }
        showCards+=" ";
        switch(card4){
        case 1: showCards+="A";break;
        case 11:showCards+="J";break;
        case 12:showCards+="Q";break;
        case 13:showCards+="K";break;
        default:showCards+=card4;  //simply adds the number to the string
        }

        switch(suit4){
        case 1: showCards+="H";break;
        case 2: showCards+="C";break;
        case 3: showCards+="D";break;
        default:showCards+="S";
        }
        showCards+=" ";
        switch(card5){
        case 1: showCards+="A";break;
        case 11:showCards+="J";break;
        case 12:showCards+="Q";break;
        case 13:showCards+="K";break;
        default:showCards+=card5;  //simply adds the number to the string
        }

        switch(suit5){
        case 1: showCards+="H";break;
        case 2: showCards+="C";break;
        case 3: showCards+="D";break;
        default:showCards+="S";
        }



        System.out.print("Your hand:"+showCards);
        System.out.println(":::::::"+hand);
        }//end loop
        System.out.println(count);
    }

}
Posted
Comments
Richard MacCutchan 16-Jan-15 4:09am    
You need to think about the different classes that would make the program easier to understand. A Card could have properties of face value and suit. A Hand could contain a number of Cards with a calculated score, which is updated every time the hand changes. Think about what the program requirements are, and build from there, rather than writing lots of repeated inline code.

1 solution

Java
public class Poker {

    public class Poker(){
     
    this.init();    

    }

    public static void main(String[] args) {
        Poker poker = new Poker();
    }

    private void init(){
    // here the party begins!
    }


That's where it all starts.

Read Richards comment (better read it multiple times ;) ).

When creating methods think about which member (aka class) should be able to do this task.

Have fun!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900