Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Straight and Straight Flush?!?!

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Straight and Straight Flush?!?!

    I am trying to make a program that deals hands in poker. I am trying to make the straight and straight flush, but can't fix the little bugs. If anyone could help me I would greatly appreciate it. Thanks in advance!


    This is a class I am calling upon:
    import java.lang.Math.*;
     /**
    4: * Card.java<br>
    5: * CS 110 Sample Class<br>
    6: * Models a Playing Card such as the ace of spades
    7: */
     public class Card {
     public enum Suits{spades,hearts,clubs,diamonds}
     
    public final static int ACE =1;
    public final static int JACK =11;
    public final static int QUEEN =12;
    public final static int KING =13;
     
    private int face;
    private Suits suit;
     
    //Constructors
     /**null constructor randomly generates a card<br>
    23: * Pre-Conditions: none <br>
    24: * Post-conditions: a single legitimate card is
    generated, no test for uniqueness*/
    public Card ()
     {
     face = (int)(Math.random() * 13 +1);
     switch((int)(Math.random()*4))
       {
      case 0: suit = Suits.spades;
    break;
    case 1: suit = Suits.hearts;
    break;
    case 2: suit = Suits.clubs;
    break;
    case 3: suit = Suits.diamonds;
     break;
     }
     }
     
     /**initializes suit_type=>s, face=>f<br>
    43: * Pre-Conditions: legimitmate face and suit value<br>
    44: * Post-Condition: a speciific card face=f, suit=s is
    generated<br>
    45: * responses: illegitimate Card
    46: * @param s = suit value specified
    47: * @param f = face value specified*/
     public Card (Suits s, int f)
     {
     suit = s;
     face = f;
     }
     
     //accessors
     /**returns the suit value of the specified card<br>
    56: * pre-conditions: none<br>
    57: * post-conditions: card is unchanged
    58: * @return suit value of card specified*/
     public Suits suit ()
     {
     return suit;
     }
     /**returns the face value of the specified card<br>
    65: * pre-conditions: none<br>
    66: * post-conditions: card is unchanged
    67: * @return face value of card specified*/
     public int face ()
     {
     return face;
       }
     /**Function to display card face of suit<br>
    74: * pre-conditions: none<br>
    75: * post-conditions: the card value as face of suit is
    display on the screen*/
    public void printcard()
    {
    System.out.print("The card is ");
    if (face > 1 && face <= 10)
    System.out.print(face);
    else
    {
    if (face == ACE) System.out.print("Ace");
    else if (face == JACK) System.out.print("Jack");
    else if (face == QUEEN) System.out.print("Queen");
    else System.out.print("King");
     }
     System.out.print(" of " + suit);
     }
     
     //mutators
     /**Function to modify the face value of a card<br>
    94: * pre-conditions: face value specified is legitimate<br>
    95: * post-conditions: card recieveing the message's face
    value is set to the paramater<br>
    96: * responses: an illegitimate Card
    97: * @param f = face value specified*/
     public void setFace (int f)
     {
     face = f;
     }
    /**Function to modify the suit value of a card<br>
    104: * pre-conditions: suit value specified is legitimate<br>
    105: * post-conditions: card recieveing the message's suit
    value is set to the paramater<br>
    106: * repsonses: an illegitimate Card
    107: * @param s = suit value specified*/
    public void setSuit (Suits s)
    {
     suit = s;
     }
     
     /**function to test equality of two cards<br>
    114: * pre-conditions: card c1 is a valid card<br>
    115: * post-conditions: none the object recieveing the
    message remains unchanged<br>
    116: * responses: error terminate
    117: * @param c1 = valid Card
    118: * @return true if the cards are equivalent, false if
    different in either face or suit*/
     public boolean equals (Card c1)
     {
     return (c1.face == face && c1.suit == suit);
     
     }
     
     /**function to determine if same face value<br>
    125: * pre-conditions: card c1 is a valid card<br>
    126: * post-conditions: none the object recieveing the
    message remains unchanged<br>
    127: * responses: error terminate
    128: * @param c1 = valid Card
    129: * @return true if the cards face values are are
    equivalent, false if different in face*/
     public boolean sameFace (Card c1)
     {
     return (c1.face == face);
     }
    public boolean sameSuit (Card c1)
     {
      return (c1.suit == suit); }
     public String toString()
     {
     return face + " of " + suit;
     }
     }
     
     
    This is my class I created:
     
    [CODE]
    import java.util.Scanner;
     
     public class Poker
     {
      public static void main (String [] agrs)
     {
        Card [] hand = new Card [5];
     
     /*//generate a hand of 5 random cards
     for (int i =0;i<hand.length;i++)
     hand[i] = new Card();
     */
    hand[0] = new Card(Card.Suits.hearts,1);
    hand[1] = new Card(Card.Suits.spades,1);
    hand[2] = new Card(Card.Suits.clubs,1);
    hand[3] = new Card(Card.Suits.hearts,11);
    hand[4] = new Card(Card.Suits.hearts,2);
     
    //display the hand
     System.out.println("the hand dealt is: ");
     for (int i=0;i<hand.length;i++)
     System.out.println(hand[i]);
     
     if (flush(hand)) 
       System.out.print("Great hand a flush!!!!");
     else if (fullHouse (hand)) 
       System.out.print("Great handfull house");
     else if (fourKind(hand)) System.out.print("Greathand, four of a kind");
     else if (threeKind(hand))
       System.out.print("Good hand three of akind");
     else if (twoKind(hand))
       System.out.print("two of a kind");
     else 
       System.out.print ("Trade them all in");
     System.out.println();
     System.out.print ("thanks for playing");
     }
     
     public static boolean flush (Card [] hand)
     { boolean isFlush = true;
      Card firstOne = new Card(hand[0].suit(), hand[0].face());
     for (int i = 1; isFlush && i<hand.length;i++)
     if (!firstOne.sameSuit(hand[i])) isFlush=false;
     
     return isFlush;
     }
     
     public static boolean fullHouse (Card [] hand)
     {
     System.out.println("in full house");
     
     return (threeKind(hand) && twoKind(hand));
     }
     
     public static boolean fourKind(Card [] hand)
     {
     int count =0;
     
     for (int i=0; count < 3 && i<hand.length;i++)
     { count =0;
     for (int j=i+1;j<hand.length;j++)
     if (hand[i].sameFace(hand[j])) count++;
     }
     
     return (count==3);
     }
     
     public static boolean threeKind(Card [] hand)
     {
     int count =0;
     
     for (int i=0; count < 2 && i<hand.length;i++)
     { count =0;
     for (int j=i+1;j<hand.length;j++)
     if (hand[i].sameFace(hand[j])) count++;
     }
     
     return (count==2);
     }
     
    public static boolean twoKind(Card [] hand)
     {
     int count =0;
     for (int i=0; count < 1 && i<hand.length;i++)
     { count =0;
     for (int j=i+1;j<hand.length;j++)
     if (hand[i].sameFace(hand[j])) count++;
     }
     
     return (count==1);
     }
     
     
    public boolean isStraight(Card [] hand){
        int[] values = new int[5];
        int pos;
        int temp;
     
        //Set values in array
        for(int i = 0; i < hand.length; i++){
            values[i] = hand[i].sameFace(hand[i]);
            //If Ace
            if(values[i] == 1)
                values[i] = 14;
        }
     
        //Sort Numerically
        for(int i = 1; i < values.length; i++){
            pos = i;
            while(pos != 0){
                if(values[pos] < values[pos-1]){
                    temp = values[pos];
                    values[pos] = values[pos-1];
                    values[pos-1]= temp;
                }
                pos--;
            }
        }
        for(int i = 0; i < values.length - 1; i++){
            if(values[i] != values[i+1] - 1)
                return false;
        }
     
        return true;
    }
     
    public boolean StraightFlush(){
        //If theres a straight and a flush present
        if(Straight(hand[]) == true && flush(hand[]) == true)
            return true;
     
        return false;
    }
     
     }
    [/CODE]


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Straight and Straight Flush?!?!

    It's never a good idea, usually, to ask it to print out an object of a class.

    System.out.println(hand[i]);

    That will likely create

    a bunch of weird things including the name of the class and likely some weird numbers and maybe letters.

    Also, you don't have a method called

    flush(hand)

    in your Poker class so it won't be able to find it.

    Second, I can't seem to find a method called flush(hand)
    even in your Card class.

    I can say that if you have a certain value assigned to each card, I'm not sure if 4 of a kind means that you could have

    1 spade Queen, 1 heart Queen, 1 club Queen, and 1 diamond Queen

    or if you need

    4 spade Queens for it to work.

    I'm not sure about this, never played poker, but if your face value of 4 cards of your 5 have the same face value, then that might mean that you have 4 of a kind.

    for instance

    // I'm hoping that there'll always be 5 cards or else this won't work

    however, another way that, might, work is:

    public boolean isSameFaceValue(Card c, Card d)
    {
    if (c.getFaceValue() == d.getFaceValue())
    return true;
    else
    return false;
    }

    Still thinking about how to do comparison for 4ofAKind, etc.